X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FTools.pm;h=4f62cd7d2c137911a6241b33d605a09d357107c4;hp=d1dfa5d105afc36c35a9bc8f170bae347ef6a6e4;hb=eb9e24df41de7c3c80ab0df8422f0b88b8c834de;hpb=490aade412869e368650b6a2b9027770e41193d4 diff --git a/data/PVE/Tools.pm b/data/PVE/Tools.pm index d1dfa5d..4f62cd7 100644 --- a/data/PVE/Tools.pm +++ b/data/PVE/Tools.pm @@ -7,12 +7,13 @@ 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'; use URI::Escape; use Encode; -use Digest::SHA1; +use Digest::SHA; use Text::ParseWords; use String::ShellQuote; @@ -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 @@ -227,7 +230,7 @@ sub run_command { if ($p eq 'timeout') { $timeout = $param{$p}; } elsif ($p eq 'umask') { - umask($param{$p}); + $old_umask = umask($param{$p}); } elsif ($p eq 'errmsg') { $errmsg = $param{$p}; } elsif ($p eq 'input') { @@ -552,7 +555,7 @@ my $keymaphash = { 'si' => ['Slovenian', 'sl', 'qwertz/slovene.kmap.gz', 'si', undef], 'se' => ['Swedish', 'sv', 'qwerty/se-latin1.kmap.gz', 'se', 'nodeadkeys'], #'th' => [], - #'tr' => [], + 'tr' => ['Turkish', 'tr', 'qwerty/trq.kmap.gz', 'tr', undef], }; my $kvmkeymaparray = []; @@ -649,16 +652,16 @@ sub upid_decode { my $filename; # "UPID:$node:$pid:$pstart:$startime:$dtype:$id:$user" - if ($upid =~ m/^UPID:([A-Za-z][[:alnum:]\-]*[[:alnum:]]+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/) { + if ($upid =~ m/^UPID:([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/) { $res->{node} = $1; - $res->{pid} = hex($2); - $res->{pstart} = hex($3); - $res->{starttime} = hex($4); - $res->{type} = $5; - $res->{id} = $6; - $res->{user} = $7; - - my $subdir = substr($4, 7, 8); + $res->{pid} = hex($3); + $res->{pstart} = hex($4); + $res->{starttime} = hex($5); + $res->{type} = $6; + $res->{id} = $7; + $res->{user} = $8; + + my $subdir = substr($5, 7, 8); $filename = "$pvetaskdir/$subdir/$upid"; } else { @@ -733,7 +736,7 @@ sub decode_text { sub decode_utf8_parameters { my ($param) = @_; - foreach my $p (qw(name comment description firstname lastname)) { + foreach my $p (qw(comment description firstname lastname)) { $param->{$p} = decode('utf8', $param->{$p}) if $param->{$p}; } @@ -742,7 +745,7 @@ sub decode_utf8_parameters { sub random_ether_addr { - my $rand = Digest::SHA1::sha1_hex(rand(), time()); + my $rand = Digest::SHA::sha1_hex(rand(), time()); my $mac = ''; for (my $i = 0; $i < 6; $i++) { @@ -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;