From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8BF6E89A6 for ; Thu, 31 Aug 2023 16:18:17 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6EEC7B435 for ; Thu, 31 Aug 2023 16:17:47 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Thu, 31 Aug 2023 16:17:46 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 61A2E47B59 for ; Thu, 31 Aug 2023 16:17:46 +0200 (CEST) Date: Thu, 31 Aug 2023 16:17:36 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Maximiliano Sandoval , pmg-devel@lists.proxmox.com References: <20230831133357.149789-1-m.sandoval@proxmox.com> In-Reply-To: <20230831133357.149789-1-m.sandoval@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.16.0 (https://github.com/astroidmail/astroid) Message-Id: <1693490808.5czmj11yww.astroid@yuna.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.063 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [utils.pm, proxmox.com] Subject: Re: [pmg-devel] [PATCH pmg-api] utils: check if file changed before reusing its hash X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Aug 2023 14:18:17 -0000 On August 31, 2023 3:33 pm, Maximiliano Sandoval 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. >=20 > Signed-off-by: Maximiliano Sandoval > --- > src/PMG/Utils.pm | 28 ++++++++++++++++++++++++---- > 1 file changed, 24 insertions(+), 4 deletions(-) >=20 > diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm > index c19b31f..f8e6b7c 100644 > --- a/src/PMG/Utils.pm > +++ b/src/PMG/Utils.pm > @@ -49,6 +49,8 @@ postgres_admin_cmd > try_decode_utf8 > ); > =20 > +my $host_rsa_key_path =3D '/etc/ssh/ssh_host_rsa_key.pub'; > + > my $valid_pmg_realms =3D ['pam', 'pmg', 'quarantine']; > =20 > PVE::JSONSchema::register_standard_option('realm', { > @@ -1353,14 +1355,32 @@ sub scan_journal_for_rbl_rejects { > } > =20 > my $hwaddress; > +my $hwaddress_st =3D {}; > + > +sub get_server_id { > + my $sshkey =3D PVE::Tools::file_get_contents($host_rsa_key_path); > + return uc(Digest::MD5::md5_hex($sshkey)); > +} > =20 > sub get_hwaddress { > + my $st =3D stat($host_rsa_key_path); > =20 > - return $hwaddress if defined ($hwaddress); > + if (! defined($hwaddress)) { FWIW, this condition > + $hwaddress_st->{mtime} =3D $st->mtime; > + $hwaddress_st->{ino} =3D $st->ino; > + $hwaddress_st->{dev} =3D $st->dev; > + $hwaddress =3D get_server_id(); > + } > + > + if ($hwaddress_st->{mtime} !=3D $st->mtime > + || $hwaddress_st->{ino} !=3D $st->ino > + || $hwaddress_st->{dev} !=3D $st->dev) { and this one can be combined, since the executed code is the same, and as long as the check for $hwaddress comes first, the condition will short-circuit on the first execution (filling both variables), and subsequent executions will compare the stat metadata. > + $hwaddress_st->{mtime} =3D $st->mtime; > + $hwaddress_st->{ino} =3D $st->ino; > + $hwaddress_st->{dev} =3D $st->dev; > =20 > - my $fn =3D '/etc/ssh/ssh_host_rsa_key.pub'; > - my $sshkey =3D PVE::Tools::file_get_contents($fn); > - $hwaddress =3D uc(Digest::MD5::md5_hex($sshkey)); > + $hwaddress =3D get_server_id(); this change would then not be needed anymore ;) I am not sure how often we have this pattern, and whether it's worth to have a generic "read_cached_file" helper? e.g., like this: my $cached =3D {}; sub something { .. my $raw =3D read_cached_file($path, $cached); .. } where both (the original copy of?) $raw and the stat metadata are stored in $cached, with the user not needing to know about the implementation details? just food for thought, most such things go through pmxcfs (which has its own caching) and INotify (same) anyway.. > + } > =20 > return $hwaddress; > } > --=20 > 2.39.2 >=20 >=20 >=20 > _______________________________________________ > pmg-devel mailing list > pmg-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel >=20 >=20 >=20