improved folder deleting

This commit is contained in:
Steffen Schröder 2014-06-20 18:51:25 +02:00
parent 4a122927dc
commit 88a8a83fae
3 changed files with 49 additions and 9 deletions

View File

@ -107,17 +107,44 @@ abstract class FileUtil {
} }
/** /**
* Delete the Temporary Folder if it's empty * Delete the temporary folder if it's empty
* *
* @return bool * @return bool
*/ */
public static function removeTempFolder() { public static function deleteTempFolder() {
$tempFolder = self::getTempFolder(); return self::deleteFolder(self::getTempFolder());
return @rmdir($tempFolder);
} }
/** /**
* Get the Temporary Folder and create it if necessary * Delete the given folder if it's empty
*
* @param string $folderPath
* @param bool $onlyIfEmpty
* @return bool
*/
public static function deleteFolder($folderPath, $onlyIfEmpty = true) {
if ($onlyIfEmpty && !self::isFolderEmpty($folderPath)) {
return false;
}
return rmdir($folderPath);
}
/**
* Check if the given folder is empty
*
* @param string $folderPath
* @return bool
*/
public static function isFolderEmpty($folderPath) {
if (!is_readable($folderPath) || !is_dir($folderPath)) {
return false;
}
$files = scandir($folderPath);
return (count($files) <= 2);
}
/**
* Get the temporary folder and create it if necessary
* *
* @return string|bool * @return string|bool
*/ */
@ -156,7 +183,6 @@ abstract class FileUtil {
} }
} }
} }
return true; return true;
} }
@ -176,6 +202,7 @@ abstract class FileUtil {
if (!is_resource($dirHandle)) { if (!is_resource($dirHandle)) {
return false; return false;
} }
$directory = self::appendDirectorySeparator($directory);
$time = time(); $time = time();
while ($fileName = readdir($dirHandle)) { while ($fileName = readdir($dirHandle)) {
$filePath = $directory . $fileName; $filePath = $directory . $fileName;
@ -200,4 +227,17 @@ abstract class FileUtil {
closedir($dirHandle); closedir($dirHandle);
return true; return true;
} }
/**
* Append the directory separator to the given path if necessary
*
* @param string $path
* @return string
*/
public static function appendDirectorySeparator($path) {
if (substr($path, -1, 1) !== DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
}
return $path;
}
} }

View File

@ -281,7 +281,7 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$zip->extractTo(ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR); $zip->extractTo(ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR);
$zip->close(); $zip->close();
unlink($updateFileName); unlink($updateFileName);
FileUtil::removeTempFolder(); FileUtil::deleteTempFolder();
$messageExtra = ''; $messageExtra = '';
if ($update) { if ($update) {

View File

@ -353,7 +353,7 @@ class UpdateManager implements CallbackListener, CommandListener, TimerListener
$zip->extractTo(ManiaControlDir); $zip->extractTo(ManiaControlDir);
$zip->close(); $zip->close();
unlink($updateFileName); unlink($updateFileName);
FileUtil::removeTempFolder(); FileUtil::deleteTempFolder();
// Set the Nightly Build Date // Set the Nightly Build Date
$this->setNightlyBuildDate($updateData->releaseDate); $this->setNightlyBuildDate($updateData->releaseDate);