Adding fnmatches argument to file copy routines which allow to specify fnmatch pattern for a file name

This commit is contained in:
Pavel Shevaev 2023-11-16 11:14:31 +03:00
parent 2d840c30f9
commit 9d74b977e4
1 changed files with 32 additions and 10 deletions

View File

@ -302,33 +302,43 @@ const COPY_MODE_BUILTIN = 1;
const COPY_MODE_SYSTEM = 2; const COPY_MODE_SYSTEM = 2;
const COPY_MODE_HARDLINK = 3; 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)) 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); 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); 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"); 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(($file != '.' ) && ($file != '..'))
{ {
if(is_dir($src . '/' . $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 else
{ {
$excluded = false; $excluded = false;
foreach($excludes as $exclude_pattern) foreach($excludes as $exclude_pattern)
$excluded = $excluded || (bool)preg_match("~$exclude_pattern~", $src . '/' . $file); $excluded = $excluded || (bool)preg_match("~$exclude_pattern~", $src . '/' . $file);
foreach($fnmatches as $fnmatch)
$excluded = $excluded || !fnmatch($fnmatch, $file);
if($excluded) if($excluded)
continue; continue;