Merge pull request #686 from kijin/pr/delete-empty-folders

빈 폴더 자동 삭제 및 일괄 삭제
This commit is contained in:
Kijin Sung 2017-02-05 22:57:02 +09:00 committed by GitHub
commit 607e9357c2
13 changed files with 395 additions and 52 deletions

View file

@ -208,15 +208,7 @@ class FileHandler
*/
public static function removeBlankDir($path)
{
$path = self::getRealPath($path);
if (Rhymix\Framework\Storage::isEmptyDirectory($path))
{
return Rhymix\Framework\Storage::deleteDirectory($path);
}
else
{
return false;
}
return Rhymix\Framework\Storage::deleteEmptyDirectory(self::getRealPath($path), false);
}
/**

View file

@ -714,6 +714,36 @@ class Storage
}
}
/**
* Delete a directory only if it is empty.
*
* @param string $dirname
* @param bool $delete_empty_parents (optional)
* @return bool
*/
public static function deleteEmptyDirectory($dirname, $delete_empty_parents = false)
{
$dirname = rtrim($dirname, '/\\');
if (!self::isDirectory($dirname) || !self::isEmptyDirectory($dirname))
{
return false;
}
$result = @rmdir($dirname);
if (!$result)
{
return false;
}
else
{
if ($delete_empty_parents)
{
self::deleteEmptyDirectory(dirname($dirname), true);
}
return true;
}
}
/**
* Get the current umask.
*
@ -742,7 +772,7 @@ class Storage
/**
* Determine the best umask for this installation of Rhymix.
*
* @return int
* @return string
*/
public static function recommendUmask()
{
@ -756,27 +786,7 @@ class Storage
$file_uid = fileowner(__FILE__);
// Get the UID of the current PHP process.
if (function_exists('posix_geteuid'))
{
$php_uid = posix_geteuid();
}
else
{
$testfile = \RX_BASEDIR . 'files/cache/uidcheck';
if (self::exists($testfile))
{
self::delete($testfile);
}
if (self::write($testfile, 'TEST'))
{
$php_uid = fileowner($testfile);
self::delete($testfile);
}
else
{
$php_uid = -1;
}
}
$php_uid = self::getServerUID();
// If both UIDs are the same, set the umask to 0022.
if ($file_uid == $php_uid)
@ -790,4 +800,36 @@ class Storage
return '0000';
}
}
/**
* Get the UID of the server process.
*
* @return int|false
*/
public static function getServerUID()
{
if (function_exists('posix_geteuid'))
{
return posix_geteuid();
}
else
{
$testfile = \RX_BASEDIR . 'files/cache/uidcheck_' . time();
if (self::exists($testfile))
{
self::delete($testfile);
}
if (self::write($testfile, 'TEST'))
{
$uid = fileowner($testfile);
self::delete($testfile);
return $uid;
}
else
{
return false;
}
}
}
}

View file

@ -0,0 +1,58 @@
<?php
/**
* This script deletes empty directories under the 'files' directory.
*
* It may be useful when your web host imposes a hard limit on the number of
* inodes, or when your backups take too long due to the large number of
* unused directories.
*
* This script only works on Unix-like operating systems where the 'find'
* command is available.
*/
require_once __DIR__ . '/common.php';
// Initialize the exit status.
$exit_status = 0;
// Delete empty directories in the attachment directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/attach')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/attach.\n";
}
else
{
echo "Error while deleting empty directories under files/attach.\n";
$exit_status = $result;
}
// Delete empty directories in the member extra info directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/member_extra_info')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/member_extra_info.\n";
}
else
{
echo "Error while deleting empty directories under files/member_extra_info.\n";
$exit_status = $result;
}
// Delete empty directories in the thumbnails directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/thumbnails')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/thumbnails.\n";
}
else
{
echo "Error while deleting empty directories under files/thumbnails.\n";
$exit_status = $result;
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,98 @@
<?php
/**
* This script deletes files that were not properly uploaded.
*
* Files can remain in an invalid status for two reasons: 1) a user abandons
* a document or comment after uploading files; or 2) a chunked upload is
* aborted without the server having any opportunity to clean it up.
* These files can obviously take up a lot of disk space. In order to prevent
* them from accumulating too much, you should run this script at least once
* every few days.
*/
require_once __DIR__ . '/common.php';
// Delete garbage files older than this number of days.
$days = 10;
// Initialize the exit status.
$exit_status = 0;
// Initialize objects.
$oDB = DB::getInstance();
$oFileController = getController('file');
// Find and delete files where isvalid = N.
$args = new stdClass;
$args->isvalid = 'N';
$args->list_count = 50;
$args->regdate_before = date('YmdHis', time() - ($days * 86400));
while (true)
{
$output = executeQueryArray('file.getFileList', $args);
if ($output->toBool())
{
if ($output->data)
{
$oDB->begin();
foreach ($output->data as $file_info)
{
$oFileController->deleteFile($file_info->file_srl);
}
$oDB->commit();
if ($output->page_navigation && $output->page_navigation->total_count == count($output->data))
{
break;
}
}
else
{
break;
}
}
else
{
echo "Error while deleting garbage files older than $days days.\n";
echo $output->getMessage() . "\n";
$exit_status = 11;
break;
}
}
if ($exit_status == 0)
{
echo "Successfully deleted all garbage files older than $days days.\n";
}
// Find and delete temporary chunks.
$dirname = RX_BASEDIR . 'files/attach/chunks';
$threshold = time() - ($days * 86400);
$chunks = Rhymix\Framework\Storage::readDirectory($dirname);
if ($chunks)
{
foreach ($chunks as $chunk)
{
if (@filemtime($chunk) < $threshold)
{
$result = Rhymix\Framework\Storage::delete($chunk);
if (!$result)
{
$exit_status = 12;
}
}
}
}
if ($exit_status == 0)
{
echo "Successfully deleted aborted file chunks older than $days days.\n";
}
else
{
echo "Error while deleting aborted file chunks older than $days days.\n";
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,37 @@
<?php
/**
* This script deletes old notifications.
*
* Notifications must be dismissed as quickly as possible in order to prevent
* the ncenterlite_notify table from becoming too large. For best performance,
* you should run this script at least once every few days.
*/
require_once __DIR__ . '/common.php';
// Delete notifications older than this number of days.
$days = 30;
// Initialize the exit status.
$exit_status = 0;
// Execute the query.
$args = new stdClass;
$args->old_date = date('YmdHis', time() - ($days * 86400));
$output = executeQuery('ncenterlite.deleteNotifyAll', $args);
if ($output->toBool())
{
echo "Successfully deleted all notifications older than $days days.\n";
}
else
{
echo "Error while deleting notifications older than $days days.\n";
echo $output->getMessage() . "\n";
$exit_status = 11;
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,41 @@
<?php
/**
* This script deletes old thumbnails.
*
* Thumbnails can take up a large amount of disk space and inodes if they are
* allowed to accumulate. Since most websites only need thumbnails for recent
* posts, it is okay to delete old thumbnails.
*
* Do not run this script if you have a gallery-style module where visitors
* regularly view old posts. This will force thumbnails to be regenerated,
* increasing the server load and making your pages load slower.
*
* This script only works on Unix-like operating systems where the 'find'
* command is available.
*/
require_once __DIR__ . '/common.php';
// Delete thumbnails older than this number of days.
$days = 90;
// Initialize the exit status.
$exit_status = 0;
// Delete old thumbnails.
passthru(sprintf('find %s -type f -mtime +%d -delete', escapeshellarg(RX_BASEDIR . 'files/thumbnails'), abs($days)), $result);
if ($result == 0)
{
echo "Successfully deleted all thumbnails older than $days days.\n";
}
else
{
echo "Error while deleting thumbnails older than $days days.\n";
$exit_status = $result;
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

42
common/scripts/common.php Normal file
View file

@ -0,0 +1,42 @@
<?php
/**
* This file must be included at the top of all shell scripts (cron jobs).
*
* HERE BE DRAGONS.
*
* Failure to perform the checks listed in this file at the top of a cron job,
* or any attempt to work around the limitations deliberately placed in this
* file, may result in errors or degradation of service.
*
* Please be warned that errors may not show up immediately, especially if you
* screw up the permissions inside deeply nested directory trees. You may find
* it difficult and/or costly to undo the damages when errors begin to show up
* several months later.
*/
// Abort if not CLI.
if (PHP_SAPI !== 'cli')
{
echo "This script must be executed on the command line interface.\n";
exit(1);
}
// Load Rhymix.
chdir(dirname(dirname(__DIR__)));
require_once dirname(__DIR__) . '/autoload.php';
// Abort if the UID does not match.
$uid = Rhymix\Framework\Storage::getServerUID();
if ($uid === 0)
{
echo "This script must not be executed by the root user.\n";
exit(2);
}
$web_server_uid = fileowner(RX_BASEDIR . 'files/config/config.php');
if ($uid !== $web_server_uid)
{
$web_server_uid = posix_getpwuid($web_server_uid);
echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n";
exit(3);
}

View file

@ -1129,6 +1129,9 @@ class commentController extends comment
$output = executeQuery('file.updateFileValid', $args);
}
// Remove the thumbnail file
Rhymix\Framework\Storage::deleteEmptyDirectory(RX_BASEDIR . sprintf('files/thumbnails/%s', getNumberingPath($comment_srl, 3)), true);
// commit
$oDB->commit();

View file

@ -919,7 +919,7 @@ class documentController extends document
$this->_deleteDocumentUpdateLog($args);
// Remove the thumbnail file
FileHandler::removeDir(sprintf('files/thumbnails/%s',getNumberingPath($document_srl, 3)));
Rhymix\Framework\Storage::deleteEmptyDirectory(RX_BASEDIR . sprintf('files/thumbnails/%s', getNumberingPath($document_srl, 3)), true);
// commit
$oDB->commit();

View file

@ -987,10 +987,13 @@ class fileController extends file
if(!$output->toBool()) return $output;
// If successfully deleted, remove the file
FileHandler::removeFile($uploaded_filename);
Rhymix\Framework\Storage::delete(FileHandler::getRealPath($uploaded_filename));
// Call a trigger (after)
ModuleHandler::triggerCall('file.deleteFile', 'after', $trigger_obj);
// Remove empty directories
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($uploaded_filename)), true);
}
$oDocumentController->updateUploaedCount($documentSrlList);
@ -1014,15 +1017,9 @@ class fileController extends file
if(!is_array($file_list)||!count($file_list)) return new Object();
// Delete the file
$path = array();
$file_count = count($file_list);
for($i=0;$i<$file_count;$i++)
foreach ($file_list as $file)
{
$this->deleteFile($file_list[$i]->file_srl);
$uploaded_filename = $file_list[$i]->uploaded_filename;
$path_info = pathinfo($uploaded_filename);
if(!in_array($path_info['dirname'], $path)) $path[] = $path_info['dirname'];
$this->deleteFile($file->file_srl);
}
// Remove from the DB
@ -1030,12 +1027,6 @@ class fileController extends file
$args->upload_target_srl = $upload_target_srl;
$output = executeQuery('file.deleteFiles', $args);
if(!$output->toBool()) return $output;
// Remove a file directory of the document
for($i=0, $c=count($path); $i<$c; $i++)
{
FileHandler::removeBlankDir($path[$i]);
}
return $output;
}
@ -1066,12 +1057,12 @@ class fileController extends file
// Determine the file path by checking if the file is an image or other kinds
if(preg_match("/\.(jpg|jpeg|gif|png|wmv|wma|mpg|mpeg|avi|swf|flv|mp1|mp2|mp3|mp4|asf|wav|asx|mid|midi|asf|mov|moov|qt|rm|ram|ra|rmm|m4v)$/i", $file_info->source_filename))
{
$path = sprintf("./files/attach/images/%s/%s/", $target_module_srl,$target_srl);
$path = sprintf("./files/attach/images/%s/%s", $target_module_srl, getNumberingPath($target_srl, 3));
$new_file = $path . $file_info->source_filename;
}
else
{
$path = sprintf("./files/attach/binaries/%s/%s/", $target_module_srl, $target_srl);
$path = sprintf("./files/attach/binaries/%s/%s", $target_module_srl, getNumberingPath($target_srl, 3));
$new_file = $path . Rhymix\Framework\Security::getRandom(32, 'hex');
}
// Pass if a target document to move is same
@ -1080,6 +1071,8 @@ class fileController extends file
FileHandler::makeDir($path);
// Move the file
FileHandler::rename($old_file, $new_file);
// Delete old path
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($old_file)), true);
// Update DB information
$args = new stdClass;
$args->file_srl = $file_info->file_srl;

View file

@ -15,6 +15,7 @@
<condition operation="notin" column="files.module_srl" var="exclude_module_srl" pipe="and" />
<condition operation="equal" column="files.isvalid" var="isvalid" pipe="and" />
<condition operation="equal" column="files.direct_download" var="direct_download" pipe="and" />
<condition operation="below" column="files.regdate" var="regdate_before" pipe="and" />
<group pipe="and">
<condition operation="like" column="files.source_filename" var="s_filename" pipe="or" />
<condition operation="more" column="files.file_size" var="s_filesize_more" pipe="or" />

View file

@ -884,6 +884,7 @@ class memberController extends member
$oMemberModel = getModel('member');
$profile_image = $oMemberModel->getProfileImage($member_srl);
FileHandler::removeFile($profile_image->file);
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($profile_image->file)), true);
}
return new Object(0,'success');
}
@ -908,6 +909,7 @@ class memberController extends member
$oMemberModel = getModel('member');
$image_name = $oMemberModel->getImageName($member_srl);
FileHandler::removeFile($image_name->file);
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($image_name->file)), true);
}
return new Object(0,'success');
}
@ -990,6 +992,7 @@ class memberController extends member
$oMemberModel = getModel('member');
$image_mark = $oMemberModel->getImageMark($member_srl);
FileHandler::removeFile($image_mark->file);
Rhymix\Framework\Storage::deleteEmptyDirectory(dirname(FileHandler::getRealPath($image_mark->file)), true);
}
return new Object(0,'success');
}
@ -1543,8 +1546,9 @@ class memberController extends member
*/
function delSignature($member_srl)
{
$filename = sprintf('files/member_extra_info/signature/%s%d.gif', getNumberingPath($member_srl), $member_srl);
FileHandler::removeFile($filename);
$dirname = RX_BASEDIR . sprintf('files/member_extra_info/signature/%s', getNumberingPath($member_srl));
Rhymix\Framework\Storage::deleteDirectory($dirname, false);
Rhymix\Framework\Storage::deleteEmptyDirectory($dirname, true);
}
/**
@ -2579,13 +2583,22 @@ class memberController extends member
ModuleHandler::triggerCall('member.deleteMember', 'after', $trigger_obj);
$oDB->commit();
// Name, image, image, mark, sign, delete
$this->procMemberDeleteImageName($member_srl);
$this->procMemberDeleteImageMark($member_srl);
$this->procMemberDeleteProfileImage($member_srl);
$this->delSignature($member_srl);
$this->_clearMemberCache($member_srl);
// Delete all remaining extra info
$dirs = Rhymix\Framework\Storage::readDirectory(RX_BASEDIR . 'files/member_extra_info', true, true, false);
foreach ($dirs as $dir)
{
$member_dir = $dir . '/' . getNumberingPath($member_srl);
Rhymix\Framework\Storage::deleteDirectory($member_dir, false);
Rhymix\Framework\Storage::deleteEmptyDirectory($member_dir, true);
}
return $output;
}

View file

@ -356,6 +356,29 @@ class StorageTest extends \Codeception\TestCase\Test
$this->assertFalse(Rhymix\Framework\Storage::deleteDirectory($nonexistent));
}
public function testDeleteEmptyDirectory()
{
$sourcedir = \RX_BASEDIR . 'tests/_output/sourcedir';
mkdir($sourcedir);
file_put_contents($sourcedir . '/foo', 'bar');
mkdir($sourcedir . '/subdir');
mkdir($sourcedir . '/subdir/subsubdir');
mkdir($sourcedir . '/subdir/subsubdir/subsubdir');
file_put_contents($sourcedir . '/subdir/subsubdir/subsubdir/foo', 'bar');
$this->assertFalse(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir/subsubdir'));
Rhymix\Framework\Storage::delete($sourcedir . '/subdir/subsubdir/subsubdir/foo');
$this->assertTrue(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir/subsubdir'));
$this->assertFalse(file_exists($sourcedir . '/subdir/subsubdir/subsubdir'));
$this->assertTrue(file_exists($sourcedir . '/subdir/subsubdir'));
$this->assertTrue(file_exists($sourcedir . '/foo'));
$this->assertTrue(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir', true));
$this->assertFalse(file_exists($sourcedir . '/subdir/subsubdir'));
$this->assertFalse(file_exists($sourcedir . '/subdir'));
$this->assertTrue(file_exists($sourcedir));
$this->assertTrue(file_exists($sourcedir . '/foo'));
}
public function testRecommendUmask()
{
$umask = Rhymix\Framework\Storage::recommendUmask();