From a356c01ca71ace7a0e3d47eb73acacd7b831271a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Thu, 6 Oct 2016 22:18:33 +0200 Subject: [PATCH] Version 16.15.5 --- swad_account.c | 6 +++- swad_centre.c | 2 +- swad_changelog.h | 4 ++- swad_course.c | 4 +-- swad_degree.c | 2 +- swad_file.c | 65 ++++++++++++++++++++++++++++++++++- swad_file.h | 3 ++ swad_file_browser.c | 82 +++++---------------------------------------- swad_file_browser.h | 1 - swad_info.c | 4 +-- swad_institution.c | 2 +- swad_logo.c | 2 +- swad_report.c | 69 ++++++++++++++++++++++++++++++++++++-- swad_report.h | 2 ++ swad_zip.c | 4 +-- 15 files changed, 163 insertions(+), 89 deletions(-) diff --git a/swad_account.c b/swad_account.c index 627609a3..67a50e7f 100644 --- a/swad_account.c +++ b/swad_account.c @@ -40,6 +40,7 @@ #include "swad_parameter.h" #include "swad_preference.h" #include "swad_profile.h" +#include "swad_report.h" #include "swad_social.h" /*****************************************************************************/ @@ -994,6 +995,9 @@ void Acc_CompletelyEliminateAccount (struct UsrData *UsrDat, /***** Remove user from table of seen announcements *****/ Ann_RemoveUsrFromSeenAnnouncements (UsrDat->UsrCod); + /***** Remove user's usage reports *****/ + Rep_RemoveUsrUsageReports (UsrDat->UsrCod); + /***** Remove user from table of connected users *****/ sprintf (Query,"DELETE FROM connected WHERE UsrCod='%ld'", UsrDat->UsrCod); @@ -1045,7 +1049,7 @@ static void Acc_RemoveUsrBriefcase (struct UsrData *UsrDat) /***** Remove files of the user's briefcase from disc *****/ Usr_ConstructPathUsr (UsrDat->UsrCod,PathRelUsr); - Brw_RemoveTree (PathRelUsr); + Fil_RemoveTree (PathRelUsr); } /*****************************************************************************/ diff --git a/swad_centre.c b/swad_centre.c index 8d487532..73a335d4 100644 --- a/swad_centre.c +++ b/swad_centre.c @@ -1609,7 +1609,7 @@ void Ctr_RemoveCentre (void) Cfg_PATH_SWAD_PUBLIC,Cfg_FOLDER_CTR, (unsigned) (Ctr.CtrCod % 100), (unsigned) Ctr.CtrCod); - Brw_RemoveTree (PathCtr); + Fil_RemoveTree (PathCtr); /***** Remove centre *****/ sprintf (Query,"DELETE FROM centres WHERE CtrCod='%ld'", diff --git a/swad_changelog.h b/swad_changelog.h index f76cedcf..4d1580ec 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -146,13 +146,15 @@ /****************************** Public constants *****************************/ /*****************************************************************************/ -#define Log_PLATFORM_VERSION "SWAD 16.15.3 (2016-10-06)" +#define Log_PLATFORM_VERSION "SWAD 16.15.5 (2016-10-06)" #define CSS_FILE "swad15.229.css" #define JS_FILE "swad15.238.1.js" // Number of lines (includes comments but not blank lines) has been got with the following command: // nl swad*.c swad*.h css/swad*.css py/swad*.py js/swad*.js soap/swad*.h sql/swad*.sql | tail -1 /* + Version 16.15.5: Oct 06, 2016 Fix bug removing user's usage reports. (206023 lines) + Version 16.15.4: Oct 06, 2016 When a user is removed, all his/her user's usage reports are removed. (206022 lines) Version 16.15.3: Oct 06, 2016 Do not write date when requesting a user's usage report. (205965 lines) Version 16.15.2: Oct 06, 2016 Code refactoring in user's usage report. (205966 lines) 1 change necessary in database: diff --git a/swad_course.c b/swad_course.c index 05cd05a9..d27d66a3 100644 --- a/swad_course.c +++ b/swad_course.c @@ -2303,10 +2303,10 @@ static void Crs_EmptyCourseCompletely (long CrsCod) /***** Remove directories of the course *****/ sprintf (PathRelCrs,"%s/%s/%ld", Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_CRS,CrsCod); - Brw_RemoveTree (PathRelCrs); + Fil_RemoveTree (PathRelCrs); sprintf (PathRelCrs,"%s/%s/%ld", Cfg_PATH_SWAD_PUBLIC,Cfg_FOLDER_CRS,CrsCod); - Brw_RemoveTree (PathRelCrs); + Fil_RemoveTree (PathRelCrs); } /*****************************************************************************/ diff --git a/swad_degree.c b/swad_degree.c index df79aa02..4b1f8270 100644 --- a/swad_degree.c +++ b/swad_degree.c @@ -2387,7 +2387,7 @@ void Deg_RemoveDegreeCompletely (long DegCod) Cfg_PATH_SWAD_PUBLIC,Cfg_FOLDER_DEG, (unsigned) (DegCod % 100), (unsigned) DegCod); - Brw_RemoveTree (PathDeg); + Fil_RemoveTree (PathDeg); /***** Remove administrators of this degree *****/ sprintf (Query,"DELETE FROM admin WHERE Scope='Deg' AND Cod='%ld'", diff --git a/swad_file.c b/swad_file.c index 740e8857..c1767cc2 100644 --- a/swad_file.c +++ b/swad_file.c @@ -425,6 +425,70 @@ void Fil_CreateDirIfNotExists (const char *Path) } } +/*****************************************************************************/ +/************************ Remove a directory recursively *********************/ +/*****************************************************************************/ +// If the tree of directories and files exists, remove it + +void Fil_RemoveTree (const char *Path) + { + struct stat FileStatus; + struct dirent **FileList; + int NumFile,NumFiles; + char PathFileRel[PATH_MAX+1]; + bool Error; + + if (Fil_CheckIfPathExists (Path)) + { + lstat (Path,&FileStatus); + if (S_ISDIR (FileStatus.st_mode)) // It's a directory + { + if (rmdir (Path)) + { + Error = false; + if (errno == ENOTEMPTY) + { + /***** Remove each directory and file under this directory *****/ + /* Scan the directory */ + if ((NumFiles = scandir (Path,&FileList,NULL,NULL)) >= 0) + { + /* Remove recursively all the directories and files */ + for (NumFile = 0; + NumFile < NumFiles; + NumFile++) + { + if (strcmp (FileList[NumFile]->d_name,".") && + strcmp (FileList[NumFile]->d_name,"..")) // Skip directories "." and ".." + { + sprintf (PathFileRel,"%s/%s",Path,FileList[NumFile]->d_name); + Fil_RemoveTree (PathFileRel); + } + free ((void *) FileList[NumFile]); + } + free ((void *) FileList); + } + else + Lay_ShowErrorAndExit ("Error while scanning directory."); + + /***** Remove of new the directory, now empty *****/ + if (rmdir (Path)) + Error = true; + } + else + Error = true; + if (Error) + { + sprintf (Gbl.Message,"Can not remove folder %s.",Path); + Lay_ShowErrorAndExit (Gbl.Message); + } + } + } + else // It's a file + if (unlink (Path)) + Lay_ShowErrorAndExit ("Can not remove file."); + } + } + /*****************************************************************************/ /********************* Remove old temporary directories **********************/ /*****************************************************************************/ @@ -573,4 +637,3 @@ void Fil_WriteFileSizeFull (double SizeInBytes, else sprintf (FileSizeStr,"%.1f TiB",SizeInBytes / Ti); } - diff --git a/swad_file.h b/swad_file.h index c754b2c1..b4ca5ccf 100644 --- a/swad_file.h +++ b/swad_file.h @@ -68,9 +68,12 @@ struct Param *Fil_StartReceptionOfFile (const char *ParamFile, bool Fil_EndReceptionOfFile (char *FileNameDataTmp,struct Param *Param); void Fil_CreateUpdateFile (const char *CurrentName,const char *ExtensionOldName,char *OldName,char *NewName,FILE **NewFile); void Fil_CloseUpdateFile (const char *CurrentName,const char *OldName,const char *NewName,FILE *NewFile); + bool Fil_RenameFileOrDir (const char *PathOld,const char *PathNew); bool Fil_CheckIfPathExists (const char *Path); void Fil_CreateDirIfNotExists (const char *Path); +void Fil_RemoveTree (const char *Path); + void Fil_RemoveOldTmpFiles (const char *Path,time_t TimeToRemove,bool RemoveDirectory); void Fil_FastCopyOfFiles (const char *PathSrc,const char *PathTgt); void Fil_FastCopyOfOpenFiles (FILE *FileSrc,FILE *FileTgt); diff --git a/swad_file_browser.c b/swad_file_browser.c index a9352233..45a61f1f 100644 --- a/swad_file_browser.c +++ b/swad_file_browser.c @@ -2897,7 +2897,7 @@ void Brw_RemoveFoldersAssignmentsIfExistForAllUsrs (const char *FolderName) UsrCod, // User's code Brw_INTERNAL_NAME_ROOT_FOLDER_ASSIGNMENTS, FolderName); - Brw_RemoveTree (PathFolder); + Fil_RemoveTree (PathFolder); } /***** Free structure that stores the query result *****/ @@ -6411,7 +6411,7 @@ void Brw_RemSubtreeInFileBrowser (void) sprintf (Path,"%s/%s",Gbl.FileBrowser.Priv.PathAboveRootFolder,Gbl.FileBrowser.Priv.FullPathInTree); /***** Remove the whole tree *****/ - Brw_RemoveTree (Path); + Fil_RemoveTree (Path); /* If a folder is removed, it is necessary to remove it from the database and all the files o folders under that folder */ @@ -6434,70 +6434,6 @@ void Brw_RemSubtreeInFileBrowser (void) Brw_ShowAgainFileBrowserOrWorks (); } -/*****************************************************************************/ -/************************* Remove a folder recursively ***********************/ -/*****************************************************************************/ -// If the tree of directories and files exists, remove it - -void Brw_RemoveTree (const char *Path) - { - struct stat FileStatus; - struct dirent **FileList; - int NumFile,NumFiles; - char PathFileRel[PATH_MAX+1]; - bool Error; - - if (Fil_CheckIfPathExists (Path)) - { - lstat (Path,&FileStatus); - if (S_ISDIR (FileStatus.st_mode)) // It's a directory - { - if (rmdir (Path)) - { - Error = false; - if (errno == ENOTEMPTY) - { - /***** Remove each directory and file under this directory *****/ - /* Scan the directory */ - if ((NumFiles = scandir (Path,&FileList,NULL,NULL)) >= 0) - { - /* Remove recursively all the directories and files */ - for (NumFile = 0; - NumFile < NumFiles; - NumFile++) - { - if (strcmp (FileList[NumFile]->d_name,".") && - strcmp (FileList[NumFile]->d_name,"..")) // Skip directories "." and ".." - { - sprintf (PathFileRel,"%s/%s",Path,FileList[NumFile]->d_name); - Brw_RemoveTree (PathFileRel); - } - free ((void *) FileList[NumFile]); - } - free ((void *) FileList); - } - else - Lay_ShowErrorAndExit ("Error while scanning directory."); - - /***** Remove of new the directory, now empty *****/ - if (rmdir (Path)) - Error = true; - } - else - Error = true; - if (Error) - { - sprintf (Gbl.Message,"Can not remove folder %s.",Path); - Lay_ShowErrorAndExit (Gbl.Message); - } - } - } - else // It's a file - if (unlink (Path)) - Lay_ShowErrorAndExit ("Can not remove file."); - } - } - /*****************************************************************************/ /********************* Expand a folder in a file browser *********************/ /*****************************************************************************/ @@ -8378,7 +8314,7 @@ void Brw_RecFolderFileBrowser (void) Brw_SetMaxQuota (); if (Brw_CheckIfQuotaExceded ()) { - Brw_RemoveTree (Path); + Fil_RemoveTree (Path); sprintf (Gbl.Message,Txt_Can_not_create_the_folder_X_because_it_would_exceed_the_disk_quota, Gbl.FileBrowser.NewFilFolLnkName); Lay_ShowAlert (Lay_WARNING,Gbl.Message); @@ -8666,7 +8602,7 @@ static bool Brw_RcvFileInFileBrw (Brw_UploadType_t UploadType) /* Rename the temporary */ if (rename (PathTmp,Path)) // Fail { - Brw_RemoveTree (PathTmp); + Fil_RemoveTree (PathTmp); sprintf (Gbl.Message,Txt_UPLOAD_FILE_could_not_create_file_NO_HTML, Gbl.FileBrowser.NewFilFolLnkName); } @@ -8677,7 +8613,7 @@ static bool Brw_RcvFileInFileBrw (Brw_UploadType_t UploadType) Brw_SetMaxQuota (); if (Brw_CheckIfQuotaExceded ()) { - Brw_RemoveTree (Path); + Fil_RemoveTree (Path); sprintf (Gbl.Message,Txt_UPLOAD_FILE_X_quota_exceeded_NO_HTML, Gbl.FileBrowser.NewFilFolLnkName); } @@ -8748,7 +8684,7 @@ static bool Brw_RcvFileInFileBrw (Brw_UploadType_t UploadType) } } else // Error in file reception. File probably too big - Brw_RemoveTree (PathTmp); + Fil_RemoveTree (PathTmp); } } } @@ -8858,7 +8794,7 @@ void Brw_RecLinkFileBrowser (void) Brw_SetMaxQuota (); if (Brw_CheckIfQuotaExceded ()) { - Brw_RemoveTree (Path); + Fil_RemoveTree (Path); sprintf (Gbl.Message,Txt_Can_not_create_the_link_X_because_it_would_exceed_the_disk_quota, FileName); Lay_ShowAlert (Lay_WARNING,Gbl.Message); @@ -11254,7 +11190,7 @@ void Brw_RemoveGrpZones (long CrsCod,long GrpCod) /***** Remove group zones *****/ sprintf (PathGrpFileZones,"%s/%s/%ld/grp/%ld", Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_CRS,CrsCod,GrpCod); - Brw_RemoveTree (PathGrpFileZones); + Fil_RemoveTree (PathGrpFileZones); } /*****************************************************************************/ @@ -11273,7 +11209,7 @@ void Brw_RemoveUsrWorksInCrs (struct UsrData *UsrDat,struct Course *Crs,Cns_Quie sprintf (PathUsrInCrs,"%s/%s/%ld/usr/%02u/%ld", Cfg_PATH_SWAD_PRIVATE,Cfg_FOLDER_CRS,Crs->CrsCod, (unsigned) (UsrDat->UsrCod % 100),UsrDat->UsrCod); - Brw_RemoveTree (PathUsrInCrs); + Fil_RemoveTree (PathUsrInCrs); // If this was the last user in his/her subfolder ==> the subfolder will be empty /***** Write message *****/ diff --git a/swad_file_browser.h b/swad_file_browser.h index 1785a79c..3b85f6ec 100644 --- a/swad_file_browser.h +++ b/swad_file_browser.h @@ -217,7 +217,6 @@ long Brw_AddPathToDB (long PublisherUsrCod,Brw_FileType_t FileType, void Brw_RemoveExpiredExpandedFolders (void); -void Brw_RemoveTree (const char *Path); void Brw_CalcSizeOfDir (char *Path); void Brw_SetFullPathInTree (const char *PathInTreeUntilFileOrFolder,const char *FilFolLnkName); diff --git a/swad_info.c b/swad_info.c index 1b2c274a..8127590a 100644 --- a/swad_info.c +++ b/swad_info.c @@ -2224,7 +2224,7 @@ void Inf_ReceivePagInfo (void) if (Str_FileIs (SourceFileName,"html") || Str_FileIs (SourceFileName,"htm" )) // .html or .htm file { - Brw_RemoveTree (PathRelDirHTML); + Fil_RemoveTree (PathRelDirHTML); Fil_CreateDirIfNotExists (PathRelDirHTML); sprintf (PathRelFileHTML,"%s/index.html",PathRelDirHTML); if (Fil_EndReceptionOfFile (PathRelFileHTML,Param)) @@ -2237,7 +2237,7 @@ void Inf_ReceivePagInfo (void) } else if (Str_FileIs (SourceFileName,"zip")) // .zip file { - Brw_RemoveTree (PathRelDirHTML); + Fil_RemoveTree (PathRelDirHTML); Fil_CreateDirIfNotExists (PathRelDirHTML); sprintf (PathRelFileZIP,"%s/%s.zip", Gbl.CurrentCrs.PathPriv, diff --git a/swad_institution.c b/swad_institution.c index c1f28010..d9f6b024 100644 --- a/swad_institution.c +++ b/swad_institution.c @@ -1497,7 +1497,7 @@ void Ins_RemoveInstitution (void) Cfg_PATH_SWAD_PUBLIC,Cfg_FOLDER_INS, (unsigned) (Ins.InsCod % 100), (unsigned) Ins.InsCod); - Brw_RemoveTree (PathIns); + Fil_RemoveTree (PathIns); /***** Remove institution *****/ sprintf (Query,"DELETE FROM institutions WHERE InsCod='%ld'", diff --git a/swad_logo.c b/swad_logo.c index 0392824b..48657d03 100644 --- a/swad_logo.c +++ b/swad_logo.c @@ -426,5 +426,5 @@ void Log_RemoveLogo (Sco_Scope_t Scope) (unsigned) (Cod % 100), (unsigned) Cod, (unsigned) Cod); - Brw_RemoveTree (FileNameLogo); + Fil_RemoveTree (FileNameLogo); } diff --git a/swad_report.c b/swad_report.c index cc84b89f..c15cb333 100644 --- a/swad_report.c +++ b/swad_report.c @@ -27,7 +27,6 @@ #include // For mkdir #include // For mkdir -#include // For unlink #include "swad_database.h" #include "swad_global.h" @@ -137,6 +136,9 @@ static void Rep_ShowMyHitsPerYear (bool AnyCourse,long CrsCod,Rol_Role_t Role, static void Rep_DrawBarNumHits (float HitsNum,float HitsMax, unsigned MaxBarWidth); +static void Rep_RemoveUsrReportsFiles (long UsrCod); +static void Rep_RemoveUsrReportsFromDB (long UsrCod); + /*****************************************************************************/ /******* Request my usage report (report on my use of the platform) **********/ /*****************************************************************************/ @@ -428,7 +430,7 @@ static void Rep_CreateNewReportEntryIntoDB (const struct tm *tm_CurrentTime, Gbl.UniqueNameEncrypted[1], &Gbl.UniqueNameEncrypted[2], // 41 rightmost chars from a unique 43 chars base64url codified from a unique SHA-256 string FilenameReport,Permalink); - DB_QueryINSERT (Query,"can not create new report"); + DB_QueryINSERT (Query,"can not create new user's usage report"); } /*****************************************************************************/ @@ -1205,3 +1207,66 @@ static void Rep_DrawBarNumHits (float HitsNum,float HitsMax, } fprintf (Gbl.F.Rep,"
"); } + +/*****************************************************************************/ +/********** Remove all user's usage report of a user from database ***********/ +/*****************************************************************************/ + +void Rep_RemoveUsrUsageReports (long UsrCod) + { + /***** Remove all user's usage report files of a user *****/ + Rep_RemoveUsrReportsFiles (UsrCod); + + /***** Remove all user's usage reports of a user from database *****/ + Rep_RemoveUsrReportsFromDB (UsrCod); + } + +/*****************************************************************************/ +/********** Remove all user's usage reports of a user from database **********/ +/*****************************************************************************/ + +static void Rep_RemoveUsrReportsFiles (long UsrCod) + { + char Query[128]; + MYSQL_RES *mysql_res; + MYSQL_ROW row; + unsigned NumReports; + unsigned NumReport; + char PathUniqueDirReport[PATH_MAX+1]; + + /***** Get directories for the reports *****/ + sprintf (Query,"SELECT UniqueDirL,UniqueDirR FROM usr_report" + " WHERE UsrCod='%ld'", + UsrCod); + NumReports = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get user's usage reports"); + + /***** Remove the reports *****/ + for (NumReport = 0; + NumReport < NumReports; + NumReport++) + { + /* Get next report */ + row = mysql_fetch_row (mysql_res); + + /* Remove report directory and file */ + sprintf (PathUniqueDirReport,"%s/%s/%s/%s", + Cfg_PATH_SWAD_PUBLIC,Cfg_FOLDER_REP,row[0],row[1]); + Fil_RemoveTree (PathUniqueDirReport); + } + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); + } + +/*****************************************************************************/ +/********** Remove all user's usage reports of a user from database **********/ +/*****************************************************************************/ + +static void Rep_RemoveUsrReportsFromDB (long UsrCod) + { + char Query[128]; + + /***** Insert a new user's usage report into database *****/ + sprintf (Query,"DELETE FROM usr_report WHERE UsrCod='%ld'",UsrCod); + DB_QueryDELETE (Query,"can not remove user's usage reports"); + } diff --git a/swad_report.h b/swad_report.h index 19d5648f..524908d3 100644 --- a/swad_report.h +++ b/swad_report.h @@ -43,4 +43,6 @@ void Rep_ReqMyUsageReport (void); void Rep_ShowMyUsageReport (void); void Rep_PrintMyUsageReport (void); +void Rep_RemoveUsrUsageReports (long UsrCod); + #endif diff --git a/swad_zip.c b/swad_zip.c index 9aeda8cf..692e44f6 100644 --- a/swad_zip.c +++ b/swad_zip.c @@ -213,7 +213,7 @@ void ZIP_CreateZIPAsgWrk (void) Lay_ShowErrorAndExit ("Can not compress files into zip file."); /***** Remove the directory of compression *****/ - Brw_RemoveTree (Path); + Fil_RemoveTree (Path); } /*****************************************************************************/ @@ -457,7 +457,7 @@ static void ZIP_CompressFolderIntoZIP (void) } /***** Remove the directory of compression *****/ - Brw_RemoveTree (PathCompression); + Fil_RemoveTree (PathCompression); } /*****************************************************************************/