Fix purge of old files

This commit is contained in:
Laurent Destailleur 2019-06-25 20:02:14 +02:00
parent 4696d35bcb
commit d41850c7cd
2 changed files with 16 additions and 11 deletions

View File

@ -50,33 +50,36 @@ class Utils
* Purge files into directory of data files. * Purge files into directory of data files.
* CAN BE A CRON TASK * CAN BE A CRON TASK
* *
* @param string $choice Choice of purge mode ('tempfiles', '' or 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfile') * @param string $choice Choice of purge mode ('tempfiles', '' or 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'allfiles', 'logfile')
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) * @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold'
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/ */
function purgeFiles($choice='tempfilesold') function purgeFiles($choice = 'tempfilesold', $nbsecondsold = 86400)
{ {
global $conf, $langs, $dolibarr_main_data_root; global $conf, $langs, $dolibarr_main_data_root;
$langs->load("admin"); $langs->load("admin");
dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$filesarray=array(); $filesarray=array();
if (empty($choice)) $choice='tempfilesold'; if (empty($choice)) $choice='tempfilesold';
dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
if ($choice=='tempfiles' || $choice=='tempfilesold') if ($choice=='tempfiles' || $choice=='tempfilesold')
{ {
// Delete temporary files // Delete temporary files
if ($dolibarr_main_data_root) if ($dolibarr_main_data_root)
{ {
$filesarray=dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks $filesarray=dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks
if ($choice == 'tempfilesold')
if ($choice == 'tempfilesold')
{ {
$now = dol_now(); $now = dol_now();
foreach($filesarray as $key => $val) foreach($filesarray as $key => $val)
{ {
if ($val['date'] > ($now - (24 * 3600))) unset($filesarray[$key]); // Discard files not older than 24h if ($val['date'] > ($now - ($nbsecondsold))) unset($filesarray[$key]); // Discard temp dir not older than $nbsecondsold
} }
} }
} }
@ -119,13 +122,14 @@ class Utils
$counterror=0; $counterror=0;
if (count($filesarray)) if (count($filesarray))
{ {
foreach($filesarray as $key => $value) foreach($filesarray as $key => $value)
{ {
//print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n"; //print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n";
if ($filesarray[$key]['type'] == 'dir') if ($filesarray[$key]['type'] == 'dir')
{ {
$startcount=0; $startcount=0;
$tmpcountdeleted=0; $tmpcountdeleted=0;
$result=dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted); $result=dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted);
$count+=$result; $count+=$result;
$countdeleted+=$tmpcountdeleted; $countdeleted+=$tmpcountdeleted;
@ -165,11 +169,12 @@ class Utils
} }
else $this->output=$langs->trans("PurgeNothingToDelete").($choice == 'tempfilesold' ? ' (older than 24h)':''); else $this->output=$langs->trans("PurgeNothingToDelete").($choice == 'tempfilesold' ? ' (older than 24h)':'');
// Recreate temp dir that are not automatically recreated for performance purpose // Recreate temp dir that are not automatically recreated by core code for performance purpose, we need them
if (! empty($conf->api->enabled)) if (! empty($conf->api->enabled))
{ {
dol_mkdir($conf->api->dir_temp); dol_mkdir($conf->api->dir_temp);
} }
dol_mkdir($conf->user->dir_temp);
//return $count; //return $count;
return 0; // This function can be called by cron so must return 0 if OK return 0; // This function can be called by cron so must return 0 if OK

View File

@ -145,7 +145,7 @@ SystemToolsAreaDesc=This area provides administration features. Use the menu to
Purge=Purge Purge=Purge
PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server. PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server.
PurgeDeleteLogFile=Delete log files, including <b>%s</b> defined for Syslog module (no risk of losing data) PurgeDeleteLogFile=Delete log files, including <b>%s</b> defined for Syslog module (no risk of losing data)
PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data) PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data). Note: Deletion is done only if the temp directory was created 24 hours ago.
PurgeDeleteTemporaryFilesShort=Delete temporary files PurgeDeleteTemporaryFilesShort=Delete temporary files
PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory <b>%s</b>. Temporary files but also database backup dumps, files attached to elements (third parties, invoices, ...) and uploaded into the ECM module will be deleted. PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory <b>%s</b>. Temporary files but also database backup dumps, files attached to elements (third parties, invoices, ...) and uploaded into the ECM module will be deleted.
PurgeRunNow=Purge now PurgeRunNow=Purge now