Skip to content

Commit

Permalink
#5869 Clean up error handling (#5870)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher authored May 19, 2020
1 parent a138a37 commit 51bf8bf
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 312 deletions.
36 changes: 16 additions & 20 deletions classes/file/FileManager.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,23 +326,25 @@ function rmdir($dirPath) {
/**
* Delete all contents including directory (equivalent to "rm -r")
* @param $file string the full path of the directory to be removed
* @return boolean true iff success, otherwise false
*/
function rmtree($file) {
if (file_exists($file)) {
if (is_dir($file)) {
$handle = opendir($file);
while (($filename = readdir($handle)) !== false) {
if ($filename != '.' && $filename != '..') {
$this->rmtree($file . '/' . $filename);
if (!$this->rmtree($file . '/' . $filename)) return false;
}
}
closedir($handle);
rmdir($file);
if (!rmdir($file)) return false;

} else {
unlink($file);
if (!unlink($file)) return false;
}
}
return true;
}

/**
Expand Down Expand Up @@ -566,21 +568,19 @@ function parseFileExtension($fileName) {
/**
* Decompress passed gziped file.
* @param $filePath string
* @param $errorMsg string
* @return boolean|string
* @return string The file path that was created.
*/
function decompressFile($filePath, &$errorMsg) {
return $this->_executeGzip($filePath, true, $errorMsg);
function decompressFile($filePath) {
return $this->_executeGzip($filePath, true);
}

/**
* Compress passed file.
* @param $filePath string The file to be compressed.
* @param $errorMsg string
* @return boolean|string
* @return string The file path that was created.
*/
function compressFile($filePath, &$errorMsg) {
return $this->_executeGzip($filePath, false, $errorMsg);
function compressFile($filePath) {
return $this->_executeGzip($filePath, false);
}


Expand All @@ -592,31 +592,27 @@ function compressFile($filePath, &$errorMsg) {
* @param $filePath string file to be compressed or uncompressed.
* @param $decompress boolean optional Set true if the passed file
* needs to be decompressed.
* @param $errorMsg string
* @return false|string The file path that was created with the operation
* or false in case of fail.
* @return string The file path that was created with the operation
*/
private function _executeGzip($filePath, $decompress = false, &$errorMsg) {
private function _executeGzip($filePath, $decompress = false) {
PKPLocale::requireComponents(LOCALE_COMPONENT_PKP_ADMIN);
$gzipPath = Config::getVar('cli', 'gzip');
if (!is_executable($gzipPath)) {
$errorMsg = __('admin.error.executingUtil', array('utilPath' => $gzipPath, 'utilVar' => 'gzip'));
return false;
throw new Exception(__('admin.error.executingUtil', array('utilPath' => $gzipPath, 'utilVar' => 'gzip')));
}
$gzipCmd = escapeshellarg($gzipPath);
if ($decompress) $gzipCmd .= ' -d';
// Make sure any output message will mention the file path.
$output = array($filePath);
$returnValue = 0;
$gzipCmd .= ' ' . $filePath;
$gzipCmd .= ' ' . escapeshellarg($filePath);
if (!Core::isWindows()) {
// Get the output, redirecting stderr to stdout.
$gzipCmd .= ' 2>&1';
}
exec($gzipCmd, $output, $returnValue);
if ($returnValue > 0) {
$errorMsg = __('admin.error.utilExecutionProblem', array('utilPath' => $gzipPath, 'output' => implode(PHP_EOL, $output)));
return false;
throw new Exception(__('admin.error.utilExecutionProblem', array('utilPath' => $gzipPath, 'output' => implode(PHP_EOL, $output))));
}
if ($decompress) {
return substr($filePath, 0, -3);
Expand Down
Loading

0 comments on commit 51bf8bf

Please sign in to comment.