From 927622c3aa679293c7a380a4c7c5d129067c5c8a Mon Sep 17 00:00:00 2001 From: Stig Thormodsrud Date: Mon, 27 Apr 2009 14:40:25 -0700 Subject: Avoid unnecessary writing of file if it's the same contents. --- scripts/vyatta-banner.pl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'scripts') diff --git a/scripts/vyatta-banner.pl b/scripts/vyatta-banner.pl index e1cd70a1..8c3db846 100644 --- a/scripts/vyatta-banner.pl +++ b/scripts/vyatta-banner.pl @@ -33,6 +33,8 @@ use Vyatta::Config; use Getopt::Long; use File::Copy; +use Digest::MD5 qw(md5_hex); +use Digest::file qw(digest_file_hex); use strict; use warnings; @@ -55,9 +57,23 @@ sub restore_orig_file { return; } +sub is_same_as_file { + my ($file, $value) = @_; + + return if ! -e $file; + my $fdigest = digest_file_hex($file, "MD5"); + my $vdigest = md5_hex("$value"); + return 1 if $fdigest eq $vdigest; + return; +} + sub write_file_value { my ($file, $value) = @_; + # Avoid unnecessary writes. At boot the file will be the + # regenerated with the same content. + return if is_same_as_file($file, $value); + open my $F, '>', $file or die "Error: opening $file [$!]"; print $F "$value"; close $F; -- cgit v1.2.3