function Archive_Tar::_extractList

7.x system.tar.inc Archive_Tar::_extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path)
3 calls to Archive_Tar::_extractList()
Archive_Tar::extractList in drupal-7.x/modules/system/system.tar.inc
This method extract from the archive only the files indicated in the $p_filelist. These files are extracted in the current directory or in the directory indicated by the optional $p_path parameter. If indicated the $p_remove_path can be used in the…
Archive_Tar::extractModify in drupal-7.x/modules/system/system.tar.inc
This method extract all the content of the archive in the directory indicated by $p_path. When relevant the memorized path of the files/dir can be modified by removing the $p_remove_path path at the beginning of the file/dir path. While extracting a…
Archive_Tar::listContent in drupal-7.x/modules/system/system.tar.inc

File

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

Class

Archive_Tar
Creates a (compressed) Tar archive *

Code

function _extractList($p_path, &$p_list_detail, $p_mode, 
$p_file_list, $p_remove_path) 
 {
  $v_result = true;
  $v_nb = 0;
  $v_extract_all = true;
  $v_listing = false;

  $p_path = $this->_translateWinPath($p_path, false);
  if ($p_path == '' || (substr($p_path, 0, 1) != '/'
      && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) {
    $p_path = "./" . $p_path;
  }
  $p_remove_path = $this->_translateWinPath($p_remove_path);

  // ----- Look for path to remove format (should end by /)
  if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) {
    $p_remove_path .= '/';
  }
  $p_remove_path_size = strlen($p_remove_path);

  switch ($p_mode) {
    case "complete":
      $v_extract_all = TRUE;
      $v_listing = FALSE;
      break;
    case "partial":
      $v_extract_all = FALSE;
      $v_listing = FALSE;
      break;
    case "list":
      $v_extract_all = FALSE;
      $v_listing = TRUE;
      break;
    default :
      $this->_error('Invalid extract mode (' . $p_mode . ')');
      return false;
  }

  clearstatcache();

  while (strlen($v_binary_data = $this->_readBlock()) != 0) 
   {
    $v_extract_file = FALSE;
    $v_extraction_stopped = 0;

    if (!$this->_readHeader($v_binary_data, $v_header)) {
      return false;
    }

    if ($v_header['filename'] == '') {
      continue;
    }

    // ----- Look for long filename
    if ($v_header['typeflag'] == 'L') {
      if (!$this->_readLongHeader($v_header)) {
        return false;
      }
    }

    if ((!$v_extract_all) && (is_array($p_file_list))) {
      // ----- By default no unzip if the file is not found
      $v_extract_file = false;

      for ($i = 0; $i < sizeof($p_file_list); $i++) {
        // ----- Look if it is a directory
        if (substr($p_file_list[$i], -1) == '/') {
          // ----- Look if the directory is in the filename path
          if ((strlen($v_header['filename']) > strlen($p_file_list[$i]))
           && (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
              == $p_file_list[$i])) {
            $v_extract_file = TRUE;
            break;
          }
        }

        // ----- It is a file, so compare the file names
        elseif ($p_file_list[$i] == $v_header['filename']) {
          $v_extract_file = TRUE;
          break;
        }
      }
    }
    else {
      $v_extract_file = TRUE;
    }

    // ----- Look if this file need to be extracted
    if (($v_extract_file) && (!$v_listing)) 
     {
      if (($p_remove_path != '')
       && (substr($v_header['filename'], 0, $p_remove_path_size)
          == $p_remove_path)) {
        $v_header['filename'] = substr($v_header['filename'], 
        $p_remove_path_size);
      }
      if (($p_path != './') && ($p_path != '/')) {
        while (substr($p_path, -1) == '/') {
          $p_path = substr($p_path, 0, strlen($p_path) -1);
        }

        if (substr($v_header['filename'], 0, 1) == '/') {
          $v_header['filename'] = $p_path . $v_header['filename'];
        }
        else {
          $v_header['filename'] = $p_path . '/' . $v_header['filename'];
        }
      }
      if (file_exists($v_header['filename'])) {
        if ((@is_dir($v_header['filename']))
         && ($v_header['typeflag'] == '')) {
          $this->_error('File ' . $v_header['filename']
            . ' already exists as a directory');
          return false;
        }
        if (($this->_isArchive($v_header['filename']))
         && ($v_header['typeflag'] == "5")) {
          $this->_error('Directory ' . $v_header['filename']
            . ' already exists as a file');
          return false;
        }
        if (!is_writeable($v_header['filename'])) {
          $this->_error('File ' . $v_header['filename']
            . ' already exists and is write protected');
          return false;
        }
        if (filemtime($v_header['filename']) > $v_header['mtime']) {
          // To be completed : An error or silent no replace ?
        }
      }

      // ----- Check the directory availability and create it if necessary
      elseif (($v_result
        = $this->_dirCheck(($v_header['typeflag'] == "5"
          ? $v_header['filename']
          : dirname($v_header['filename'])))) != 1) {
        $this->_error('Unable to create path for ' . $v_header['filename']);
        return false;
      }

      if ($v_extract_file) {
        if ($v_header['typeflag'] == "5") {
          if (!@file_exists($v_header['filename'])) {
            // Drupal integration.
            // Changed the code to use drupal_mkdir() instead of mkdir().
            if (!@drupal_mkdir($v_header['filename'], 0777)) {
              $this->_error('Unable to create directory {'
                . $v_header['filename'] . '}');
              return false;
            }
          }
        }
        elseif ($v_header['typeflag'] == "2") {
          if (@file_exists($v_header['filename'])) {
            @drupal_unlink($v_header['filename']);
          }
          if (!@symlink($v_header['link'], $v_header['filename'])) {
            $this->_error('Unable to extract symbolic link {'
              . $v_header['filename'] . '}');
            return false;
          }
        }
        else {
          if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
            $this->_error('Error while opening {' . $v_header['filename']
              . '} in write binary mode');
            return false;
          }
          else {
            $n = floor($v_header['size'] / 512);
            for ($i = 0; $i < $n; $i++) {
              $v_content = $this->_readBlock();
              fwrite($v_dest_file, $v_content, 512);
            }
            if (($v_header['size'] % 512) != 0) {
              $v_content = $this->_readBlock();
              fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
            }

            @fclose($v_dest_file);

            // ----- Change the file mode, mtime
            @touch($v_header['filename'], $v_header['mtime']);
            if ($v_header['mode'] & 0111) {
              // make file executable, obey umask
              $mode = fileperms($v_header['filename']) | (~umask() & 0111);
              @chmod($v_header['filename'], $mode);
            }
          }

          // ----- Check the file size
          clearstatcache();
          if (filesize($v_header['filename']) != $v_header['size']) {
            $this->_error('Extracted file ' . $v_header['filename']
              . ' does not have the correct file size \''
              . filesize($v_header['filename'])
              . '\' (' . $v_header['size']
              . ' expected). Archive may be corrupted.');
            return false;
          }
        }
      }
      else {
        $this->_jumpBlock(ceil(($v_header['size'] / 512)));
      }
    }
    else {
      $this->_jumpBlock(ceil(($v_header['size'] / 512)));
    }

    /* TBC : Seems to be unused ...
      if ($this->_compress)
        $v_end_of_file = @gzeof($this->_file);
      else
        $v_end_of_file = @feof($this->_file);
        */

    if ($v_listing || $v_extract_file || $v_extraction_stopped) {
      // ----- Log extracted files
      if (($v_file_dir = dirname($v_header['filename']))
        == $v_header['filename']) {
        $v_file_dir = '';
      }
      if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) {
        $v_file_dir = '/';
      }

      $p_list_detail[$v_nb++] = $v_header;
      if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
        return true;
      }
    }
  }

  return true;
}