From: Vincent Blavet Date: Thu, 9 May 2002 13:00:12 +0000 (+0000) Subject: * Better handling of Windows paths (\php\any_thing or c:\php\any_thing) X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~172 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=59071dc42e9ac38812f331edef3f123b3ba3237e;p=php * Better handling of Windows paths (\php\any_thing or c:\php\any_thing) * Remember that inside the archive path are all the time in unix format /php/any_thing * When necessary, '\' are replaced by '/' and 'c:' are removed * Synchro with cvs:pear/Archive_Tar/Tar.php version 1.20 --- diff --git a/pear/Archive/Tar.php b/pear/Archive/Tar.php index 84ceb11079..3400a87764 100644 --- a/pear/Archive/Tar.php +++ b/pear/Archive/Tar.php @@ -540,6 +540,10 @@ class Archive_Tar extends PEAR $v_result=true; $v_header = array(); + // ----- Remove potential windows directory separator + $p_add_dir = $this->_translateWinPath($p_add_dir); + $p_remove_dir = $this->_translateWinPath($p_remove_dir, false); + if (!$this->_file) { $this->_error('Invalid file descriptor'); return false; @@ -576,7 +580,7 @@ class Archive_Tar extends PEAR $p_hitem = readdir($p_hdir); // '..' directory while ($p_hitem = readdir($p_hdir)) { if ($v_filename != ".") - $p_temp_list[0] = $v_filename.DIRECTORY_SEPARATOR.$p_hitem; + $p_temp_list[0] = $v_filename.'/'.$p_hitem; else $p_temp_list[0] = $p_hitem; @@ -607,24 +611,28 @@ class Archive_Tar extends PEAR } // ----- Calculate the stored filename + $p_filename = $this->_translateWinPath($p_filename, false);; $v_stored_filename = $p_filename; if (strcmp($p_filename, $p_remove_dir) == 0) { return true; } if ($p_remove_dir != '') { - if (substr($p_remove_dir, -1) != DIRECTORY_SEPARATOR) - $p_remove_dir .= DIRECTORY_SEPARATOR; + if (substr($p_remove_dir, -1) != '/') + $p_remove_dir .= '/'; if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir) $v_stored_filename = substr($p_filename, strlen($p_remove_dir)); } + $v_stored_filename = $this->_translateWinPath($v_stored_filename); if ($p_add_dir != '') { - if (substr($p_add_dir, -1) == DIRECTORY_SEPARATOR) + if (substr($p_add_dir, -1) == '/') $v_stored_filename = $p_add_dir.$v_stored_filename; else - $v_stored_filename = $p_add_dir.DIRECTORY_SEPARATOR.$v_stored_filename; + $v_stored_filename = $p_add_dir.'/'.$v_stored_filename; } + $v_stored_filename = $this->_pathReduction($v_stored_filename); + if (strlen($v_stored_filename) > 99) { $this->_warning("Stored file name is too long (max. 99) : '$v_stored_filename'"); fclose($v_file); @@ -811,18 +819,15 @@ class Archive_Tar extends PEAR $v_extract_all = true; $v_listing = false; - // ----- Look for removing the WINDOW '\' - if (OS_WINDOWS && strpos($p_path, '\\')) { - str_replace('\\', DIRECTORY_SEPARATOR, $p_path); - } - - if ($p_path == '' || (substr($p_path, 0, 1) != DIRECTORY_SEPARATOR && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) { + $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) != DIRECTORY_SEPARATOR)) - $p_remove_path .= DIRECTORY_SEPARATOR; + if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/')) + $p_remove_path .= '/'; $p_remove_path_size = strlen($p_remove_path); switch ($p_mode) { @@ -867,7 +872,7 @@ class Archive_Tar extends PEAR for ($i=0; $i strlen($p_file_list[$i])) && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i])) { $v_extract_file = TRUE; @@ -891,14 +896,14 @@ class Archive_Tar extends PEAR 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 != DIRECTORY_SEPARATOR)) { - while (substr($p_path, -1) == DIRECTORY_SEPARATOR) + 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) == DIRECTORY_SEPARATOR) + if (substr($v_header['filename'], 0, 1) == '/') $v_header['filename'] = $p_path.$v_header['filename']; else - $v_header['filename'] = $p_path.DIRECTORY_SEPARATOR.$v_header['filename']; + $v_header['filename'] = $p_path.'/'.$v_header['filename']; } if (file_exists($v_header['filename'])) { if ((@is_dir($v_header['filename'])) && ($v_header['typeflag'] == '')) { @@ -992,8 +997,8 @@ class Archive_Tar extends PEAR // ----- Log extracted files if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) $v_file_dir = ''; - if ((substr($v_header['filename'], 0, 1) == DIRECTORY_SEPARATOR) && ($v_file_dir == '')) - $v_file_dir = DIRECTORY_SEPARATOR; + if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == '')) + $v_file_dir = '/'; $p_list_detail[$v_nb++] = $v_header; } @@ -1097,7 +1102,7 @@ class Archive_Tar extends PEAR // ----- Look for not empty path if ($p_dir != '') { // ----- Explode path by directory names - $v_list = explode(DIRECTORY_SEPARATOR, $p_dir); + $v_list = explode('/', $p_dir); // ----- Study directories from last to first for ($i=sizeof($v_list)-1; $i>=0; $i--) { @@ -1112,16 +1117,33 @@ class Archive_Tar extends PEAR } else if (($v_list[$i] == '') && ($i!=(sizeof($v_list)-1)) && ($i!=0)) { // ----- Ignore only the double '//' in path, - // but not the first and last DIRECTORY_SEPARATOR + // but not the first and last / } else { - $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?DIRECTORY_SEPARATOR.$v_result:''); + $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?'/'.$v_result:''); } } } - $v_result = strtr($v_result, '\\', DIRECTORY_SEPARATOR); + $v_result = strtr($v_result, '\\', '/'); return $v_result; } // }}} + // {{{ _translateWinPath() + function _translateWinPath($p_path, $p_remove_disk_letter=true) + { + if (OS_WINDOWS) { + // ----- Look for potential disk letter + if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) { + $p_path = substr($p_path, $v_position+1); + } + // ----- Change potential windows directory separator + if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) { + $p_path = strtr($p_path, '\\', '/'); + } + } + return $p_path; + } + // }}} + } -?> +?> \ No newline at end of file