public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager] api2tools: check if file changed before reusing its hash
Date: Mon,  9 Oct 2023 11:35:58 +0200	[thread overview]
Message-ID: <20231009093558.84300-1-m.sandoval@proxmox.com> (raw)

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.

This is to be in sync with the changes made in
https://git.proxmox.com/?p=pmg-api.git;a=commit;h=16d2ff9f8e90db64114a66d78672f5a03f5ee990.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 PVE/API2Tools.pm | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/PVE/API2Tools.pm b/PVE/API2Tools.pm
index a3d7ca84..618c13b3 100644
--- a/PVE/API2Tools.pm
+++ b/PVE/API2Tools.pm
@@ -17,14 +17,22 @@ use PVE::SafeSyslog;
 use PVE::Storage::Plugin;
 
 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(md5_hex($sshkey));
+    $hwaddress_st->@{'mtime', 'ino', 'dev'} = ($st->mtime, $st->ino, $st->dev);
 
     return $hwaddress;
 }
-- 
2.39.2





                 reply	other threads:[~2023-10-09  9:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231009093558.84300-1-m.sandoval@proxmox.com \
    --to=m.sandoval@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal