You are here

function boost_cache_write in Boost 6

Writes data to filename in an atomic operation thats compatible with older versions of php (php < 5.2.4 file_put_contents() doesn't lock correctly).

Parameters

$filename: Name of file to be written

$buffer: Contents of file

1 call to boost_cache_write()
boost_cache_set in ./boost.module
Replaces/Sets the cached contents of the specified page, if stale.

File

./boost.module, line 4426
Provides static file caching for Drupal text output. Pages, Feeds, ect...

Code

function boost_cache_write($filename, $buffer) {
  $filenames = boost_get_all_filenames($filename);
  foreach ($filenames as $key => $values) {
    if ($key == 'gzip') {
      $data = gzencode($buffer, 9);
    }
    else {
      $data = $buffer;
    }
    foreach ($values as $filename) {
      if (!_boost_mkdir_p(dirname($filename))) {
        if (BOOST_VERBOSE >= 3) {
          watchdog('boost', 'Unable to create directory: %dir<br /> Group ID: %gid<br /> User ID: %uid<br /> Current script owner: %user<br />', array(
            '%dir' => dirname($filename),
            '%gid' => getmygid(),
            '%uid' => getmyuid(),
            '%user' => get_current_user(),
          ), WATCHDOG_WARNING);
        }
      }
      $tempfile = $filename . getmypid();
      $oldfile = $tempfile . 'old';
      if (@file_put_contents($tempfile, $data) === FALSE) {
        if (BOOST_VERBOSE >= 3) {
          watchdog('boost', 'Unable to write temp file: %file<br /> Group ID: %gid<br /> User ID: %uid<br /> Current script owner: %user<br />', array(
            '%file' => $tempfile,
            '%gid' => getmygid(),
            '%uid' => getmyuid(),
            '%user' => get_current_user(),
          ), WATCHDOG_WARNING);
        }
        return FALSE;
      }
      else {
        if (is_numeric(BOOST_PERMISSIONS_FILE)) {
          @chmod($tempfile, octdec(BOOST_PERMISSIONS_FILE));
        }

        // Erase old file
        if (BOOST_OVERWRITE_FILE) {

          // Keep old file around just in case rename fails
          @rename($filename, $oldfile);
        }

        // Put temp file in its final location
        if (@rename($tempfile, $filename) === FALSE) {

          // If rename failed then remove new file and put old file back
          @unlink($tempfile);
          @rename($oldfile, $filename);
          if (BOOST_VERBOSE >= 5) {
            watchdog('boost', 'Unable to rename file: %temp  to  %file<br /> Group ID: %gid<br /> User ID: %uid<br /> Current script owner: %user<br />', array(
              '%temp' => $tempfile,
              '%file' => $filename,
              '%gid' => getmygid(),
              '%uid' => getmyuid(),
              '%user' => get_current_user(),
            ), WATCHDOG_WARNING);
          }
          return FALSE;
        }
        elseif (BOOST_OVERWRITE_FILE) {

          // Rename is sucessful; remove old file
          @unlink($oldfile);
        }
      }
    }
  }
  return TRUE;
}