From 9d74b977e4038ec9eeea6a66e569b95256d90a12 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 16 Nov 2023 11:14:31 +0300 Subject: [PATCH] Adding fnmatches argument to file copy routines which allow to specify fnmatch pattern for a file name --- helpers.inc.php | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/helpers.inc.php b/helpers.inc.php index 089abf7..81c188a 100644 --- a/helpers.inc.php +++ b/helpers.inc.php @@ -302,33 +302,43 @@ const COPY_MODE_BUILTIN = 1; const COPY_MODE_SYSTEM = 2; const COPY_MODE_HARDLINK = 3; -function ensure_copy($src, $dst, $dir_perms = 0777, $excludes = array()) +function ensure_copy(string $src, string $dst, int $dir_perms = 0777, array $excludes = array(), array $fnmatches = array()) { - recurse_copy($src, $dst, $dir_perms, COPY_MODE_BUILTIN, false, $excludes); + recurse_copy($src, $dst, $dir_perms, COPY_MODE_BUILTIN, false, $excludes, $fnmatches); } -function ensure_copy_file_if_differs($src_file, $dst_file, $dir_perms = 0777) +function ensure_copy_file_if_differs(string $src_file, string $dst_file, $dir_perms = 0777) { if(!is_file($dst_file) || filesize($src_file) != filesize($dst_file) || crc32_file($src_file) !== crc32_file($dst_file)) ensure_copy($src_file, $dst_file, $dir_perms); } -function ensure_sync($src, $dst, $dir_perms = 0777, $excludes = array()) +function ensure_sync(string $src, string $dst, int $dir_perms = 0777, array $excludes = array(), array $fnmatches = array()) { - recurse_copy($src, $dst, $dir_perms, COPY_MODE_BUILTIN, true, $excludes); + recurse_copy($src, $dst, $dir_perms, COPY_MODE_BUILTIN, true, $excludes, $fnmatches); } -function ensure_hardlink($src, $dst, $dir_perms = 0777, $excludes = array()) +function ensure_hardlink(string $src, string $dst, int $dir_perms = 0777, array $excludes = array(), array $fnmatches = array()) { - recurse_copy($src, $dst, $dir_perms, COPY_MODE_HARDLINK, true, $excludes); + recurse_copy($src, $dst, $dir_perms, COPY_MODE_HARDLINK, true, $excludes, $fnmatches); } -function ensure_duplicate($src, $dst, $dir_perms = 0777) +function ensure_duplicate(string $src, string $dst, int $dir_perms = 0777) { recurse_copy($src, $dst, $dir_perms, COPY_MODE_SYSTEM); } -function recurse_copy($src, $dst, $dir_perms = 0777, $copy_mode = COPY_MODE_BUILTIN, $mtime_check = false, $excludes = array()) +function recurse_copy( + string $src, + string $dst, + int $dir_perms = 0777, + int $copy_mode = COPY_MODE_BUILTIN, + bool $mtime_check = false, + //regex expressions which check full paths + array $excludes = array(), + //fnmatch expressions which check file names only + array $fnmatches = array() +) { msg_dbg("copying $src => $dst ...\n"); @@ -354,12 +364,24 @@ function recurse_copy($src, $dst, $dir_perms = 0777, $copy_mode = COPY_MODE_BUIL if(($file != '.' ) && ($file != '..')) { if(is_dir($src . '/' . $file)) - recurse_copy($src . '/' . $file, $dst . '/' . $file, $dir_perms, $copy_mode, $mtime_check, $excludes); + { + recurse_copy( + $src . '/' . $file, + $dst . '/' . $file, + $dir_perms, + $copy_mode, + $mtime_check, + $excludes, + $fnmatches + ); + } else { $excluded = false; foreach($excludes as $exclude_pattern) $excluded = $excluded || (bool)preg_match("~$exclude_pattern~", $src . '/' . $file); + foreach($fnmatches as $fnmatch) + $excluded = $excluded || !fnmatch($fnmatch, $file); if($excluded) continue;