all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] [PATCH pmg-api v2] utils: check if file changed before reusing its hash
@ 2023-08-31 14:41 Maximiliano Sandoval
  2023-09-12 15:12 ` [pmg-devel] applied: " Stoiko Ivanov
  0 siblings, 1 reply; 2+ messages in thread
From: Maximiliano Sandoval @ 2023-08-31 14:41 UTC (permalink / raw)
  To: pmg-devel

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>
---
 Differences from v1:
   - Dont add unnecesary variables or functions
   - Use hash slices

 src/PMG/Utils.pm | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index c19b31f..d9c9d2d 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -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;
 }
-- 
2.39.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pmg-devel] applied: [PATCH pmg-api v2] utils: check if file changed before reusing its hash
  2023-08-31 14:41 [pmg-devel] [PATCH pmg-api v2] utils: check if file changed before reusing its hash Maximiliano Sandoval
@ 2023-09-12 15:12 ` Stoiko Ivanov
  0 siblings, 0 replies; 2+ messages in thread
From: Stoiko Ivanov @ 2023-09-12 15:12 UTC (permalink / raw)
  To: Maximiliano Sandoval; +Cc: pmg-devel

applied the patch - Thanks!

a follow-up that does a syslog on a rather visible level ('err'),
in case this actually changes while a system is running, as Thomas
suggested would still be appreciated though!


On Thu, 31 Aug 2023 16:41:13 +0200
Maximiliano Sandoval <m.sandoval@proxmox.com> wrote:

> 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>
> ---
>  Differences from v1:
>    - Dont add unnecesary variables or functions
>    - Use hash slices
> 
>  src/PMG/Utils.pm | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
> index c19b31f..d9c9d2d 100644
> --- a/src/PMG/Utils.pm
> +++ b/src/PMG/Utils.pm
> @@ -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;
>  }





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-09-12 15:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 14:41 [pmg-devel] [PATCH pmg-api v2] utils: check if file changed before reusing its hash Maximiliano Sandoval
2023-09-12 15:12 ` [pmg-devel] applied: " Stoiko Ivanov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal