]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Tools.pm
new helper dump_logfile
[pve-common.git] / data / PVE / Tools.pm
index 1daf1a272caefd8da26f86fe82896c097b2fcc33..b42a5bb5f603b4ff5b3d13a9503aabb9067f2a64 100644 (file)
@@ -14,6 +14,7 @@ use URI::Escape;
 use Encode;
 use Digest::SHA1;
 use Text::ParseWords;
+use String::ShellQuote;
 
 our @EXPORT_OK = qw(
 lock_file 
@@ -619,7 +620,7 @@ sub upid_open {
  
     my $outfh = IO::File->new ($filename, O_WRONLY|O_CREAT|O_EXCL, $perm) ||
        die "unable to create output file '$filename' - $!\n";
-    chown $wwwid, $outfh;
+    chown $wwwid, -1, $outfh;
 
     return $outfh;
 };
@@ -691,23 +692,7 @@ sub random_ether_addr {
 sub shellquote {
     my $str = shift;
 
-    return "''" if !defined ($str) || ($str eq '');
-    
-    die "unable to quote string containing null (\\000) bytes"
-       if $str =~ m/\x00/;
-
-    # from String::ShellQuote
-    if ($str =~ m|[^\w!%+,\-./:@^]|) {
-
-       # ' -> '\''
-       $str =~ s/'/'\\''/g;
-
-       $str = "'$str'";
-       $str =~ s/^''//;
-       $str =~ s/''$//;
-    }
-
-    return $str;
+    return String::ShellQuote::shell_quote($str);
 }
 
 # split an shell argument string into an array,
@@ -717,4 +702,41 @@ sub split_args {
     return $str ? [ Text::ParseWords::shellwords($str) ] : [];
 }
 
+sub dump_logfile {
+    my ($filename, $start, $limit) = @_;
+
+    my $lines = [];
+    my $count = 0;
+
+    my $fh = IO::File->new($filename, "r");
+    if (!$fh) { 
+       $count++;
+       push @$lines, { n => $count, t => "unable to open file - $!"};
+       return ($count, $lines);
+    }
+
+    $start = 0 if !$start;
+    $limit = 50 if !$limit;
+
+    my $line;
+    while (defined($line = <$fh>)) {
+       next if $count++ < $start;
+       next if $limit <= 0;
+       chomp $line;
+       push @$lines, { n => $count, t => $line};
+       $limit--;
+    }
+
+    close($fh);
+
+    # HACK: ExtJS store.guaranteeRange() does not like empty array
+    # so we add a line
+    if (!$count) {
+       $count++;
+       push @$lines, { n => $count, t => "no content"};
+    }
+
+    return ($count, $lines);
+}
+
 1;