]> git.proxmox.com Git - pmg-api.git/commitdiff
utils: check if file changed before reusing its hash
authorMaximiliano Sandoval <m.sandoval@proxmox.com>
Thu, 31 Aug 2023 14:41:13 +0000 (16:41 +0200)
committerStoiko Ivanov <s.ivanov@proxmox.com>
Tue, 12 Sep 2023 14:58:34 +0000 (16:58 +0200)
We cache the hash of this file, it makes sense to first check if the
file changed via `stat` and recompute the hash if needed.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
src/PMG/Utils.pm

index c19b31f97f020533d41b30bf81793e5f82e30746..d9c9d2d53ac48990f4c47cfebbbc48c71f841971 100644 (file)
@@ -1353,14 +1353,22 @@ sub scan_journal_for_rbl_rejects {
 }
 
 my $hwaddress;
+my $hwaddress_st = {};
 
 sub get_hwaddress {
+    my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
+    my $st = stat($fn);
 
-    return $hwaddress if defined ($hwaddress);
+    if (defined($hwaddress)
+       && $hwaddress_st->{mtime} == $st->mtime
+       && $hwaddress_st->{ino} == $st->ino
+       && $hwaddress_st->{dev} == $st->dev) {
+       return $hwaddress;
+    }
 
-    my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
     my $sshkey = PVE::Tools::file_get_contents($fn);
     $hwaddress = uc(Digest::MD5::md5_hex($sshkey));
+    $hwaddress_st->@{'mtime', 'ino', 'dev'} = ($st->mtime, $st->ino, $st->dev);
 
     return $hwaddress;
 }