From 72e8023d493a7d99af2e84878f40cd8eeef07be7 Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Wed, 15 Dec 2010 03:52:05 +0000 Subject: #605318 by hass, dww, bendly, 1V: Add garbage collection to the update manager --- modules/update/update.manager.inc | 4 ++-- modules/update/update.module | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) (limited to 'modules/update') diff --git a/modules/update/update.manager.inc b/modules/update/update.manager.inc index 7649852bf..c11014af0 100644 --- a/modules/update/update.manager.inc +++ b/modules/update/update.manager.inc @@ -723,8 +723,8 @@ function update_manager_file_get($url) { mkdir($cache_directory); } - if (!file_exists($local)) { - return system_retrieve_file($url, $local); + if (!file_exists($local) || update_delete_file_if_stale($local)) { + return system_retrieve_file($url, $local, FALSE, FILE_EXISTS_REPLACE); } else { return $local; diff --git a/modules/update/update.module b/modules/update/update.module index 9f2764c28..6ff35f5c1 100644 --- a/modules/update/update.module +++ b/modules/update/update.module @@ -301,6 +301,9 @@ function update_cron() { // missing data, and if so, try to fetch the data. update_get_available(TRUE); } + + // Clear garbage from disk. + update_clear_update_disk_cache(); } /** @@ -861,3 +864,46 @@ function update_flush_caches() { /** * @} End of "defgroup update_status_cache". */ + +/** + * Clear the temporary files and directories based on file age from disk. + */ +function update_clear_update_disk_cache() { + // List of update module cache directories. + $directories = array( + 'temporary://update-cache', + 'temporary://update-extraction', + ); + + // Search for files and directories in base folder only without recursion. + foreach ($directories as $directory) { + file_scan_directory($directory, '/.*/', array('callback' => 'update_delete_file_if_stale', 'recurse' => FALSE)); + } +} + +/** + * Delete stale files and directories from the Update manager disk cache. + * + * Files and directories older than 6 hours and development snapshots older + * than 5 minutes are considered stale. We only cache development snapshots + * for 5 minutes since otherwise updated snapshots might not be downloaded as + * expected. + * + * When checking file ages, we need to use the ctime, not the mtime + * (modification time) since many (all?) tar implementations go out of their + * way to set the mtime on the files they create to the timestamps recorded + * in the tarball. We want to see the last time the file was changed on disk, + * which is left alone by tar and correctly set to the time the archive file + * was unpacked. + * + * @param $path + * A string containing a file path or (streamwrapper) URI. + */ +function update_delete_file_if_stale($path) { + if (file_exists($path)) { + $filectime = filectime($path); + if (REQUEST_TIME - $filectime > DRUPAL_MAXIMUM_TEMP_FILE_AGE || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) { + file_unmanaged_delete_recursive($path); + } + } +} -- cgit v1.2.3