function PclTarExtractIndex($p_tarname, $p_index, $p_path = "./", $p_remove_path = "", $p_mode = "") { TrFctStart(__FILE__, __LINE__, "PclTarExtractIndex", "tar={$p_tarname}, index='{$p_index}', path={$p_path}, remove_path='{$p_remove_path}', mode='{$p_mode}'"); $v_result = 1; // ----- Extract the tar format from the extension if ($p_mode == "" || $p_mode != "tar" && $p_mode != "tgz") { if (($p_mode = PclTarHandleExtension($p_tarname)) == "") { // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } // ----- Look if the $p_index is really an integer if (is_integer($p_index)) { // ----- Call the extracting fct if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "{$p_index}", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1) { TrFctEnd(__FILE__, __LINE__, 0, PclErrorString()); return 0; } } else { if (is_string($p_index)) { // ----- Call the extracting fct if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1) { TrFctEnd(__FILE__, __LINE__, 0, PclErrorString()); return 0; } } else { // ----- Error log PclErrorLog(-3, "Invalid variable type {$p_index}"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } // ----- Return TrFctEnd(__FILE__, __LINE__, $p_list); return $p_list; }
function PclTarMerge($p_tarname, $p_tarname_add, $p_mode = "", $p_mode_add = "") { TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='{$p_tarname}', tar_add='{$p_tarname_add}', mode='{$p_mode}', mode_add='{$p_mode_add}'"); $v_result = 1; // ----- Check the parameters if ($p_tarname == "" || $p_tarname_add == "") { // ----- Error log PclErrorLog(-3, "Invalid empty archive name"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Extract the tar format from the extension if ($p_mode == "" || $p_mode != "tar" && $p_mode != "tgz") { if (($p_mode = PclTarHandleExtension($p_tarname)) == "") { // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } if ($p_mode_add == "" || $p_mode_add != "tar" && $p_mode_add != "tgz") { if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "") { // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } // ----- Clear filecache clearstatcache(); // ----- Check the file size if (!is_file($p_tarname) || ($v_size = filesize($p_tarname)) % 512 != 0 && $p_mode == "tar") { // ----- Error log if (!is_file($p_tarname)) { PclErrorLog(-4, "Archive '{$p_tarname}' does not exist"); } else { PclErrorLog(-6, "Archive '{$p_tarname}' has invalid size " . filesize($p_tarname) . "(not a 512 block multiple)"); } // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } if (!is_file($p_tarname_add) || ($v_size_add = filesize($p_tarname_add)) % 512 != 0 && $p_mode_add == "tar") { // ----- Error log if (!is_file($p_tarname_add)) { PclErrorLog(-4, "Archive '{$p_tarname_add}' does not exist"); } else { PclErrorLog(-6, "Archive '{$p_tarname_add}' has invalid size " . filesize($p_tarname_add) . "(not a 512 block multiple)"); } // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Look for compressed archive if ($p_mode == "tgz") { // ----- Open the file in read mode if (($p_tar = @gzopen($p_tarname, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '{$p_tarname}' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Open a temporary file in write mode $v_temp_tarname = uniqid("pcltar-") . ".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file {$v_temp_tarname}"); if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0) { // ----- Close tar file gzclose($p_tar); // ----- Error log PclErrorLog(-1, "Unable to open file '{$v_temp_tarname}' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = gzread($p_tar, 512); // ----- Read the following blocks but not the last one if (!gzeof($p_tar)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; // ----- Read new 512 block and write the already read do { // ----- Write the already read block $v_binary_data = pack("a512", "{$v_buffer}"); gzputs($v_temp_tar, $v_binary_data); $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); // ----- Read next block $v_buffer = gzread($p_tar, 512); } while (!gzeof($p_tar)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } } else { if ($p_mode == "tar") { // ----- Open the tar file if (($p_tar = fopen($p_tarname, "r+b")) == 0) { // ----- Error log PclErrorLog(-1, "Unable to open file '{$p_tarname}' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Go to the beginning of last block TrFctMessage(__FILE__, __LINE__, 4, "Position before :" . ($p_mode == "tar" ? ftell($p_tar) : gztell($p_tar))); fseek($p_tar, $v_size - 512); TrFctMessage(__FILE__, __LINE__, 4, "Position after :" . ($p_mode == "tar" ? ftell($p_tar) : gztell($p_tar))); } else { // ----- Error log PclErrorLog(-3, "Invalid tar mode {$p_mode}"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } } // ----- Look for type of archive to add if ($p_mode_add == "tgz") { TrFctMessage(__FILE__, __LINE__, 4, "Opening file {$p_tarname_add}"); // ----- Open the file in read mode if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '{$p_tarname_add}' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = gzread($p_tar_add, 512); // ----- Read the following blocks but not the last one if (!gzeof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; // ----- Read new 512 block and write the already read do { // ----- Write the already read block $v_binary_data = pack("a512", "{$v_buffer}"); if ($p_mode == "tar") { fputs($p_tar, $v_binary_data); } else { gzputs($v_temp_tar, $v_binary_data); } $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); // ----- Read next block $v_buffer = gzread($p_tar_add, 512); } while (!gzeof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } // ----- Close the files gzclose($p_tar_add); } else { if ($p_mode == "tar") { // ----- Open the file in read mode if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '{$p_tarname_add}' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = fread($p_tar_add, 512); // ----- Read the following blocks but not the last one if (!feof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; // ----- Read new 512 block and write the already read do { // ----- Write the already read block $v_binary_data = pack("a512", "{$v_buffer}"); if ($p_mode == "tar") { fputs($p_tar, $v_binary_data); } else { gzputs($v_temp_tar, $v_binary_data); } $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); // ----- Read next block $v_buffer = fread($p_tar_add, 512); } while (!feof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } // ----- Close the files fclose($p_tar_add); } } // ----- Call the footer of the tar archive $v_result = PclTarHandleFooter($p_tar, $p_mode); // ----- Look for closing compressed archive if ($p_mode == "tgz") { // ----- Close the files gzclose($p_tar); gzclose($v_temp_tar); // ----- Unlink tar file if (!@unlink($p_tarname)) { // ----- Error log PclErrorLog(-11, "Error while deleting archive name {$p_tarname}"); } // ----- Rename tar file if (!@rename($v_temp_tarname, $p_tarname)) { // ----- Error log PclErrorLog(-12, "Error while renaming temporary file {$v_temp_tarname} to archive name {$p_tarname}"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } else { if ($p_mode == "tar") { // ----- Close the tarfile fclose($p_tar); } } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
function PclTarMerge($p_tarname, $p_tarname_add, $p_mode = "", $p_mode_add = "") { TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='{$p_tarname}', tar_add='{$p_tarname_add}', mode='{$p_mode}', mode_add='{$p_mode_add}'"); $v_result = 1; if ($p_tarname == "" || $p_tarname_add == "") { PclErrorLog(-3, "Invalid empty archive name"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } if ($p_mode == "" || $p_mode != "tar" && $p_mode != "tgz") { if (($p_mode = PclTarHandleExtension($p_tarname)) == "") { TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } if ($p_mode_add == "" || $p_mode_add != "tar" && $p_mode_add != "tgz") { if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "") { TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return 0; } } clearstatcache(); if (!is_file($p_tarname) || ($v_size = filesize($p_tarname)) % 512 != 0 && $p_mode == "tar") { if (!is_file($p_tarname)) { PclErrorLog(-4, "Archive '{$p_tarname}' does not exist"); } else { PclErrorLog(-6, "Archive '{$p_tarname}' has invalid size " . filesize($p_tarname) . "(not a 512 block multiple)"); } TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } if (!is_file($p_tarname_add) || ($v_size_add = filesize($p_tarname_add)) % 512 != 0 && $p_mode_add == "tar") { if (!is_file($p_tarname_add)) { PclErrorLog(-4, "Archive '{$p_tarname_add}' does not exist"); } else { PclErrorLog(-6, "Archive '{$p_tarname_add}' has invalid size " . filesize($p_tarname_add) . "(not a 512 block multiple)"); } TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } if ($p_mode == "tgz") { if (($p_tar = @gzopen($p_tarname, "rb")) == 0) { PclErrorLog(-2, "Unable to open file '{$p_tarname}' in binary read mode"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_temp_tarname = uniqid("pcltar-") . ".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file {$v_temp_tarname}"); if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0) { gzclose($p_tar); PclErrorLog(-1, "Unable to open file '{$v_temp_tarname}' in binary write mode"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_buffer = gzread($p_tar, 512); if (!gzeof($p_tar)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; do { $v_binary_data = pack("a512", "{$v_buffer}"); gzputs($v_temp_tar, $v_binary_data); $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); $v_buffer = gzread($p_tar, 512); } while (!gzeof($p_tar)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } } else { if ($p_mode == "tar") { if (($p_tar = fopen($p_tarname, "r+b")) == 0) { PclErrorLog(-1, "Unable to open file '{$p_tarname}' in binary write mode"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctMessage(__FILE__, __LINE__, 4, "Position before :" . ($p_mode == "tar" ? ftell($p_tar) : gztell($p_tar))); fseek($p_tar, $v_size - 512); TrFctMessage(__FILE__, __LINE__, 4, "Position after :" . ($p_mode == "tar" ? ftell($p_tar) : gztell($p_tar))); } else { PclErrorLog(-3, "Invalid tar mode {$p_mode}"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } } if ($p_mode_add == "tgz") { TrFctMessage(__FILE__, __LINE__, 4, "Opening file {$p_tarname_add}"); if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0) { PclErrorLog(-2, "Unable to open file '{$p_tarname_add}' in binary read mode"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_buffer = gzread($p_tar_add, 512); if (!gzeof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; do { $v_binary_data = pack("a512", "{$v_buffer}"); if ($p_mode == "tar") { fputs($p_tar, $v_binary_data); } else { gzputs($v_temp_tar, $v_binary_data); } $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); $v_buffer = gzread($p_tar_add, 512); } while (!gzeof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } gzclose($p_tar_add); } else { if ($p_mode == "tar") { if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0) { PclErrorLog(-2, "Unable to open file '{$p_tarname_add}' in binary read mode"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_buffer = fread($p_tar_add, 512); if (!feof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i = 1; do { $v_binary_data = pack("a512", "{$v_buffer}"); if ($p_mode == "tar") { fputs($p_tar, $v_binary_data); } else { gzputs($v_temp_tar, $v_binary_data); } $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block {$i}"); $v_buffer = fread($p_tar_add, 512); } while (!feof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "{$i} 512 bytes blocks"); } fclose($p_tar_add); } } $v_result = PclTarHandleFooter($p_tar, $p_mode); if ($p_mode == "tgz") { gzclose($p_tar); gzclose($v_temp_tar); if (!@unlink($p_tarname)) { PclErrorLog(-11, "Error while deleting archive name {$p_tarname}"); } if (!@rename($v_temp_tarname, $p_tarname)) { PclErrorLog(-12, "Error while renaming temporary file {$v_temp_tarname} to archive name {$p_tarname}"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } else { if ($p_mode == "tar") { fclose($p_tar); } } TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }