function Archive_Tar::_openAppend

7.x system.tar.inc Archive_Tar::_openAppend()
2 calls to Archive_Tar::_openAppend()
Archive_Tar::addString in drupal-7.x/modules/system/system.tar.inc
This method add a single string as a file at the end of the existing archive. If the archive does not yet exists it is created.
Archive_Tar::_append in drupal-7.x/modules/system/system.tar.inc

File

drupal-7.x/modules/system/system.tar.inc, line 1685

Class

Archive_Tar
Creates a (compressed) Tar archive *

Code

function _openAppend() 
 {
  if (filesize($this->_tarname) == 0) {
    return $this->_openWrite();
  }

  if ($this->_compress) {
    $this->_close();

    if (!@rename($this->_tarname, $this->_tarname . ".tmp")) {
      $this->_error('Error while renaming \'' . $this->_tarname
        . '\' to temporary file \'' . $this->_tarname
        . '.tmp\'');
      return false;
    }

    if ($this->_compress_type == 'gz') {
      $v_temp_tar = @gzopen($this->_tarname . ".tmp", "rb");
    }
    elseif ($this->_compress_type == 'bz2') {
      $v_temp_tar = @bzopen($this->_tarname . ".tmp", "r");
    }

    if ($v_temp_tar == 0) {
      $this->_error('Unable to open file \'' . $this->_tarname
        . '.tmp\' in binary read mode');
      @rename($this->_tarname . ".tmp", $this->_tarname);
      return false;
    }

    if (!$this->_openWrite()) {
      @rename($this->_tarname . ".tmp", $this->_tarname);
      return false;
    }

    if ($this->_compress_type == 'gz') {
      while (!@gzeof($v_temp_tar)) {
        $v_buffer = @gzread($v_temp_tar, 512);
        if ($v_buffer == ARCHIVE_TAR_END_BLOCK) {
          // do not copy end blocks, we will re-make them
          // after appending
          continue;
        }
        $v_binary_data = pack("a512", $v_buffer);
        $this->_writeBlock($v_binary_data);
      }

      @gzclose($v_temp_tar);
    }
    elseif ($this->_compress_type == 'bz2') {
      while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
        if ($v_buffer == ARCHIVE_TAR_END_BLOCK) {
          continue;
        }
        $v_binary_data = pack("a512", $v_buffer);
        $this->_writeBlock($v_binary_data);
      }

      @bzclose($v_temp_tar);
    }

    if (!@drupal_unlink($this->_tarname . ".tmp")) {
      $this->_error('Error while deleting temporary file \''
        . $this->_tarname . '.tmp\'');
    }

  }
  else {
    // ----- For not compressed tar, just add files before the last
    //       one or two 512 bytes block
    if (!$this->_openReadWrite()) {
      return false;
    }

    clearstatcache();
    $v_size = filesize($this->_tarname);

    // We might have zero, one or two end blocks.
    // The standard is two, but we should try to handle
    // other cases.
    fseek($this->_file, $v_size - 1024);
    if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
      fseek($this->_file, $v_size - 1024);
    }
    elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
      fseek($this->_file, $v_size - 512);
    }
  }

  return true;
}