mirror of
git://git.proxmox.com/git/pve-zsync.git
synced 2025-01-03 09:17:37 +03:00
introduce and use read_file helper
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
parent
f06c336b2a
commit
d01b047d26
53
pve-zsync
53
pve-zsync
@ -81,6 +81,19 @@ sub check_bin {
|
|||||||
die "unable to find command '$bin'\n";
|
die "unable to find command '$bin'\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub read_file {
|
||||||
|
my ($filename, $one_line_only) = @_;
|
||||||
|
|
||||||
|
my $fh = IO::File->new($filename, "r")
|
||||||
|
or die "Could not open file ${filename}: $!\n";
|
||||||
|
|
||||||
|
my $text = $one_line_only ? <$fh> : [ <$fh> ];
|
||||||
|
|
||||||
|
close($fh);
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
sub cut_target_width {
|
sub cut_target_width {
|
||||||
my ($path, $maxlen) = @_;
|
my ($path, $maxlen) = @_;
|
||||||
$path =~ s@/+@/@g;
|
$path =~ s@/+@/@g;
|
||||||
@ -202,14 +215,9 @@ sub read_cron {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $fh = IO::File->new("< $CRONJOBS");
|
my $text = read_file($CRONJOBS, 0);
|
||||||
die "Could not open file $CRONJOBS: $!\n" if !$fh;
|
|
||||||
|
|
||||||
my @text = <$fh>;
|
return encode_cron(@{$text});
|
||||||
|
|
||||||
close($fh);
|
|
||||||
|
|
||||||
return encode_cron(@text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub parse_argv {
|
sub parse_argv {
|
||||||
@ -329,28 +337,14 @@ sub read_state {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $fh = IO::File->new("< $STATE");
|
my $text = read_file($STATE, 1);
|
||||||
die "Could not open file $STATE: $!\n" if !$fh;
|
return decode_json($text);
|
||||||
|
|
||||||
my $text = <$fh>;
|
|
||||||
my $states = decode_json($text);
|
|
||||||
|
|
||||||
close($fh);
|
|
||||||
|
|
||||||
return $states;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub update_state {
|
sub update_state {
|
||||||
my ($job) = @_;
|
my ($job) = @_;
|
||||||
my $text;
|
|
||||||
my $in_fh;
|
|
||||||
|
|
||||||
eval {
|
my $text = eval { read_file($STATE, 1); };
|
||||||
|
|
||||||
$in_fh = IO::File->new("< $STATE");
|
|
||||||
die "Could not open file $STATE: $!\n" if !$in_fh;
|
|
||||||
$text = <$in_fh>;
|
|
||||||
};
|
|
||||||
|
|
||||||
my $out_fh = IO::File->new("> $STATE.new");
|
my $out_fh = IO::File->new("> $STATE.new");
|
||||||
die "Could not open file ${STATE}.new: $!\n" if !$out_fh;
|
die "Could not open file ${STATE}.new: $!\n" if !$out_fh;
|
||||||
@ -382,9 +376,6 @@ sub update_state {
|
|||||||
|
|
||||||
close($out_fh);
|
close($out_fh);
|
||||||
rename "$STATE.new", $STATE;
|
rename "$STATE.new", $STATE;
|
||||||
eval {
|
|
||||||
close($in_fh);
|
|
||||||
};
|
|
||||||
|
|
||||||
return $states;
|
return $states;
|
||||||
}
|
}
|
||||||
@ -399,12 +390,9 @@ sub update_cron {
|
|||||||
my $header = "SHELL=/bin/sh\n";
|
my $header = "SHELL=/bin/sh\n";
|
||||||
$header .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n";
|
$header .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n";
|
||||||
|
|
||||||
my $fh = IO::File->new("< $CRONJOBS");
|
my $current = read_file($CRONJOBS, 0);
|
||||||
die "Could not open file $CRONJOBS: $!\n" if !$fh;
|
|
||||||
|
|
||||||
my @test = <$fh>;
|
foreach my $line (@{$current}) {
|
||||||
|
|
||||||
while (my $line = shift(@test)) {
|
|
||||||
chomp($line);
|
chomp($line);
|
||||||
if ($line =~ m/source $job->{source} .*name $job->{name} /) {
|
if ($line =~ m/source $job->{source} .*name $job->{name} /) {
|
||||||
$updated = 1;
|
$updated = 1;
|
||||||
@ -433,7 +421,6 @@ sub update_cron {
|
|||||||
close ($new_fh);
|
close ($new_fh);
|
||||||
|
|
||||||
die "can't move $CRONJOBS.new: $!\n" if !rename "${CRONJOBS}.new", $CRONJOBS;
|
die "can't move $CRONJOBS.new: $!\n" if !rename "${CRONJOBS}.new", $CRONJOBS;
|
||||||
close ($fh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub format_job {
|
sub format_job {
|
||||||
|
Loading…
Reference in New Issue
Block a user