You are here

function _agrcache_file_unmanaged_save_data in Aggregate cache 7

Helper to atomically write a file.

1 call to _agrcache_file_unmanaged_save_data()
agrcache_generate_aggregate in ./agrcache.module
Menu callback to generate a css aggregate.

File

./agrcache.module, line 471
Provides imagecache style generation of css/js aggregates.

Code

function _agrcache_file_unmanaged_save_data($data, $path, $uri) {

  // Create the file.
  file_prepare_directory($path, FILE_CREATE_DIRECTORY);

  // PHP file functions do not like null bytes, so as well as randomizing the
  // temporary filename, hash it too.
  $tmp_uri = $uri . drupal_hash_base64($uri . drupal_random_bytes(10));

  // file_unmanaged_save_data() uses copy, which is not atomic and could
  // result in empty files being served from other requests while the file
  // is being written. So write it to a temporary filename, then use
  // rename() afterwards, which is atomic.
  $write = file_unmanaged_save_data($data, $tmp_uri, FILE_EXISTS_REPLACE);
  if ($write) {

    // It's possible another process has already written the aggregate and
    // renamed it, so suppress errors.
    @rename($tmp_uri, $uri);
  }
  else {
    trigger_error('Agrcache failed to write aggregate . ' . $uri . ' successfully.', E_USER_WARNING);
  }
  return $write;
}