warnings.pl: again, don't update the timestamp unless we need to

Don't update the timestamp unless we really have to do so.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel) 2020-09-11 17:43:38 -07:00
parent eeccbfe6ec
commit 1a3bf7a3d7

View file

@ -279,17 +279,20 @@ if ($what eq 'c') {
close($out);
# Write data to file if and only if it has changed
# Windows requires append mode here
open($out, '+>>', $outfile)
or die "$0: cannot open output file $outfile: $!\n";
my $datalen = length($outdata);
my $oldlen = read($out, my $oldoutdata, $datalen+1);
if (!defined($oldlen) || $oldlen != $datalen ||
!($oldoutdata eq $outdata)) {
# Data changed, must rewrite
truncate($out, 0);
seek($out, 0, SEEK_SET)
or die "$0: cannot rewind output file $outfile: $!\n";
print $out $outdata;
# For some systems, even if we don't write, opening for append
# apparently touches the timestamp, so we need to read and write
# as separate operations.
if (open(my $out, '<', $outfile)) {
my $datalen = length($outdata);
my $oldlen = read($out, my $oldoutdata, $datalen+1);
close($out);
exit 0 if (defined($oldlen) && $oldlen == $datalen &&
($oldoutdata eq $outdata));
}
# Data changed, must rewrite
open(my $out, '>', $outfile)
or die "$0: cannot open output file $outfile: $!\n";
print $out $outdata;
close($out);