Commit 2e1f5024 authored by amnesia's avatar amnesia

Upgrade htpdate script (taken from Git 7797fe9).

parent 8ebb2acc
......@@ -31,7 +31,6 @@ use Cwd;
use DateTime;
use DateTime::Format::DateParse;
use English qw( -no_match_vars );
# use Fatal qw(close);
use File::Path qw(rmtree);
use File::Temp qw/tempdir/;
use Getopt::Std;
......@@ -184,9 +183,9 @@ sub getRemoteDateDiff {
# fetch (the page and) referenced resources:
# images, stylesheets, scripts, etc.
my $beforedt = DateTime->now;
my $before = DateTime->now->epoch();
WIFEXITED(system(@cmdline)) or error "Failed to fetch content from $url: $!";
my $localdt = DateTime->now;
my $local = DateTime->now->epoch();
my $newestdt;
eval { $newestdt = newestDateHeader($tmpdir) };
if ($EVAL_ERROR =~ m/No downloaded files can be found/) {
......@@ -197,42 +196,33 @@ sub getRemoteDateDiff {
rmtree($tmpdir);
defined $newestdt or error "Could not get any Date header";
my $newest_epoch = $newestdt->epoch();
my $diffdt = $newestdt - $localdt;
my $diff = $diffdt->in_units('seconds');
my $tookdt = $localdt - $beforedt;
my $took = $tookdt->in_units('seconds');
my $diff = $newest_epoch - $local;
my $took = $local - $before;
debug("$url (took ${took}s) => diff = $diff second(s)");
return $diffdt;
return $diff;
}
sub adjustDate {
my ($diffdt) = @_;
my ($diff) = @_;
defined $diffdt or error "adjustDate was passed an undefined diff";
defined $diff or error "adjustDate was passed an undefined diff";
my $localdt = DateTime->now;
my $absdiffdt = $diffdt->is_positive() ? $diffdt : $diffdt->inverse();
my $diff = $diffdt->in_units('seconds');
my $local = DateTime->now->epoch();
my $absdiff = abs($diff);
debug("Median diff: $diff second(s)");
if ( $maxadjust && DateTime::Duration->compare( $absdiffdt,
DateTime::Duration->new(seconds => $maxadjust),
$localdt )
> 0 ) {
if ( $maxadjust && $absdiff gt $maxadjust ) {
message("Not setting clock as diff ($diff seconds) is too large.");
}
elsif ( DateTime::Duration->compare($absdiffdt,
DateTime::Duration->new(seconds => $minadjust),
$localdt, )
<= 0 ) {
elsif ( $absdiff lt $minadjust) {
message("Not setting clock as diff ($diff seconds) is too small.");
}
else {
# add_duration seems buggy... can't add $diffdt to DateTime->now
my $newtime = DateTime->now->epoch + $diff;
message("Setting time to $newtime...");
if ($set_date) {
......@@ -250,16 +240,16 @@ sub adjustDate {
}
my @urls = parseCommandLine();
my @diffdts = grep {
my @diffs = grep {
defined $_
} map {
my $diffdt = $_->{thread}->join();
if ($paranoid && ! defined $diffdt) {
my $diff = $_->{thread}->join();
if ($paranoid && ! defined $diff) {
error('Paranoid mode: aborting as one server (',
$_->{url},
') could not be reached');
}
$diffdt;
$diff;
} map {
{
url => $_,
......@@ -267,7 +257,5 @@ my @diffdts = grep {
}
} @urls
or error "No Date header could be received.";
my @sorted_diffdts = sort {
$a->in_units('seconds') <=> $b->in_units('seconds')
} @diffdts;
adjustDate($sorted_diffdts[int(@sorted_diffdts / 2)]);
my @sorted_diffs = sort @diffs;
adjustDate($sorted_diffs[int(@sorted_diffs / 2)]);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment