use URI::Escape;
use Encode;
use Digest::SHA1;
+use Text::ParseWords;
+use String::ShellQuote;
our @EXPORT_OK = qw(
lock_file
local $ENV{LVM_SUPPRESS_FD_WARNINGS} = "1";
$pid = open3($writer, $reader, $error, @$cmd) || die $!;
+
+ # if we pipe fron STDIN, open3 closes STDIN, so we we
+ # a perl warning "Filehandle STDIN reopened as GENXYZ .. "
+ # as soon as we open a new file.
+ # to avoid that we open /dev/null
+ if (!ref($writer) && !defined(fileno(STDIN))) {
+ POSIX::close(0);
+ open(STDIN, "</dev/null");
+ }
};
my $err = $@;
local $SIG{ALRM} = sub { die "got timeout\n"; } if $timeout;
$oldtimeout = alarm($timeout) if $timeout;
- print $writer $input if defined $input;
- close $writer;
+ if (ref($writer)) {
+ print $writer $input if defined $input;
+ close $writer;
+ }
my $select = new IO::Select;
- $select->add($reader);
+ $select->add($reader) if ref($reader);
$select->add($error);
my $outlog = '';
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;
};
return $mac;
}
+sub shellquote {
+ my $str = shift;
+
+ return String::ShellQuote::shell_quote($str);
+}
+
+# split an shell argument string into an array,
+sub split_args {
+ my ($str) = @_;
+
+ 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;