X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FTools.pm;h=9ab82f5c5316c2d5b61cfec5a26ce328f22eb716;hp=12b5ba291045829eda74f9af967bb462dd188668;hb=7eb283fbd220dec417b7ce761a98dc572ac0f421;hpb=66bda4e09d2d7de880b1fda8b54c72bd68adf555 diff --git a/data/PVE/Tools.pm b/data/PVE/Tools.pm index 12b5ba2..9ab82f5 100644 --- a/data/PVE/Tools.pm +++ b/data/PVE/Tools.pm @@ -7,6 +7,7 @@ use IO::Select; use File::Basename; use File::Path qw(make_path); use IO::File; +use IO::Dir; use IPC::Open3; use Fcntl qw(:DEFAULT :flock); use base 'Exporter'; @@ -22,6 +23,8 @@ run_command file_set_contents file_get_contents file_read_firstline +dir_glob_regex +dir_glob_foreach split_list template_replace safe_print @@ -826,4 +829,34 @@ sub dump_logfile { return ($count, $lines); } +sub dir_glob_regex { + my ($dir, $regex) = @_; + + my $dh = IO::Dir->new ($dir); + return wantarray ? () : undef if !$dh; + + while (defined(my $tmp = $dh->read)) { + if (my @res = $tmp =~ m/^($regex)$/) { + $dh->close; + return wantarray ? @res : $tmp; + } + } + $dh->close; + + return wantarray ? () : undef; +} + +sub dir_glob_foreach { + my ($dir, $regex, $func) = @_; + + my $dh = IO::Dir->new ($dir); + if (defined $dh) { + while (defined(my $tmp = $dh->read)) { + if (my @res = $tmp =~ m/^($regex)$/) { + &$func (@res); + } + } + } +} + 1;