diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-09 20:27:27 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-12 11:58:50 +0400 |
commit | 24e75d7cb99964a3886d3b33356806c210783b6f (patch) | |
tree | 1eaa7026e34d2e43a8672fb9d298487d6998252c /core | |
parent | 6320f5e35a766b8cf1bb618c87192943fbcef62b (diff) |
refs #5936 added many tests for the new filesystem methods to make sure they only delete the files they are supposed to do
Diffstat (limited to 'core')
-rw-r--r-- | core/Filesystem.php | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/core/Filesystem.php b/core/Filesystem.php index 5dde559999..cfa0c336db 100644 --- a/core/Filesystem.php +++ b/core/Filesystem.php @@ -204,8 +204,53 @@ class Filesystem return; } + /** + * Removes all files and directories that are present in the target directory but are not in the source directory. + * + * @param string $source Path to the source directory + * @param string $target Path to the target + */ public static function unlinkTargetFilesNotPresentInSource($source, $target) { + $diff = self::directoryDiff($source, $target); + $diff = self::sortFilesDescByPathLength($diff); + + foreach ($diff as $file) { + $remove = $target . $file; + + if (is_dir($remove)) { + rmdir($remove); + } else { + self::deleteFileIfExists($remove); + } + } + } + + public static function sortFilesDescByPathLength($files) + { + usort($files, function ($a, $b) { + // sort by filename length so we kinda make sure to remove files before its directories + if ($a == $b) { + return 0; + } + + return (strlen($a) > strlen($b) ? -1 : 1); + }); + + return $files; + } + + /** + * Computes the difference of directories. Compares $target against $source and returns a relative path to all files + * and directories in $target that are not present in $source. + * + * @param $source + * @param $target + * + * @return array + */ + public static function directoryDiff($source, $target) + { $sourceFiles = self::globr($source, '*'); $targetFiles = self::globr($target, '*'); @@ -219,27 +264,9 @@ class Filesystem $diff = array_diff($targetFiles, $sourceFiles); - usort($diff, function ($a,$b) { - // sort by filename length so we kinda make sure to remove files before its directories - if ($a == $b) { - return 0; - } - - return (strlen($a) > strlen($b) ? -1 : 1); - }); - - foreach ($diff as $file) { - $remove = $target . $file; - - if (is_dir($remove)) { - rmdir($remove); - } else { - unlink($remove); - } - } + return array_values($diff); } - /** * Copies a file from `$source` to `$dest`. * |