You are here

function hacked_scan_directory_generate_hashes in Hacked! 6

Same name and namespace in other branches
  1. 5 hacked.module \hacked_scan_directory_generate_hashes()

Hash the contents of a directory, optionally retrieving from cache.

Parameters

$directory The directory to hash.:

$cache Can I use a cache for the files in this directory?:

2 calls to hacked_scan_directory_generate_hashes()
hacked_hash_local in ./hacked.module
hacked_release_generate_hashes in ./hacked.module

File

./hacked.module, line 451
The Hacked! module, shows which project have been changed since download.

Code

function hacked_scan_directory_generate_hashes($directory, $cache = FALSE) {
  $timestamps = array();

  // Try to load some details from the cache:
  if ($cache) {
    $key = "hacked:directory:timestamps:{$directory}";

    // The key could get really long, guard against that:
    if (strlen($key) > 255) {
      $key = "hacked:directory:timestamps:" . sha1($directory);
    }
    $cache_ob = cache_get($key, HACKED_CACHE_TABLE);
    if ($cache_ob && isset($cache_ob->data)) {
      $timestamps = $cache_ob->data;
    }
  }
  $hashes = array();
  $files = hacked_file_scan_directory($directory, '/.*/', array(
    '.',
    '..',
    'CVS',
    '.svn',
    '.git',
  ));
  foreach ($files as $file) {
    $filename = str_replace($directory . '/', '', $file->filename);

    // Check the timestamp if available:
    if (isset($timestamps[$file->filename]) && filemtime($file->filename) == $timestamps[$file->filename]['timestamp']) {
      $hashes[$filename] = $timestamps[$file->filename]['hash'];
    }
    else {
      $timestamps[$file->filename]['hash'] = $hashes[$filename] = sha1_file($file->filename);
      $timestamps[$file->filename]['timestamp'] = filemtime($file->filename);
    }
  }
  if ($cache) {
    cache_set($key, $timestamps, HACKED_CACHE_TABLE);
  }
  return $hashes;
}