You are here

function update_delete_file_if_stale in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/update/update.module \update_delete_file_if_stale()
  2. 7 modules/update/update.module \update_delete_file_if_stale()
  3. 9 core/modules/update/update.module \update_delete_file_if_stale()

Deletes 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.

Parameters

$path: A string containing a file path or (streamwrapper) URI.

Return value

bool TRUE if the file is stale and deleted successfully, FALSE otherwise.

2 calls to update_delete_file_if_stale()
UpdateDeleteFileIfStaleTest::testUpdateDeleteFileIfStale in core/modules/update/tests/src/Kernel/UpdateDeleteFileIfStaleTest.php
Tests the deletion of stale files.
update_manager_file_get in core/modules/update/update.manager.inc
Copies a file from the specified URL to the temporary directory for updates.
1 string reference to 'update_delete_file_if_stale'
update_clear_update_disk_cache in core/modules/update/update.module
Clears the temporary files and directories based on file age from disk.

File

core/modules/update/update.module, line 702
Handles updates of Drupal core and contributed projects.

Code

function update_delete_file_if_stale($path) {
  if (file_exists($path)) {
    $filectime = filectime($path);
    $max_age = \Drupal::config('system.file')
      ->get('temporary_maximum_age');
    $request_time = \Drupal::time()
      ->getRequestTime();
    if ($request_time - $filectime > $max_age || preg_match('/.*-dev\\.(tar\\.gz|zip)/i', $path) && $request_time - $filectime > 300) {
      try {
        \Drupal::service('file_system')
          ->deleteRecursive($path);
        return TRUE;
      } catch (FileException $e) {

        // Ignore failed deletes.
      }
    }
  }
  return FALSE;
}