[pve-devel] [PATCH common v2 1/2] sysfstools: file_write: extend with logging and ignore list

Dominik Csapak d.csapak at proxmox.com
Fri Nov 8 10:32:58 CET 2024


the actual error and path is useful to know when trying to debug or
figure out what did not work, so warn here if there was an error.

Also takes now an optional error list that can be ignored. If
encountering such an error, returns success instead of failure.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/PVE/SysFSTools.pm | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm
index 0bde6d7..156fee6 100644
--- a/src/PVE/SysFSTools.pm
+++ b/src/PVE/SysFSTools.pm
@@ -211,17 +211,31 @@ sub check_iommu_support{
     return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*");
 }
 
+# writes $buf into $filename
+# returns success when encountering an error from the given $ignore_list, e.g. EEXIST
 sub file_write {
-    my ($filename, $buf) = @_;
+    my ($filename, $buf, $ignore_list) = @_;
 
     my $fh = IO::File->new($filename, "w");
     return undef if !$fh;
 
-    my $res = defined(syswrite($fh, $buf)) ? 1 : 0;
-
+    my $res = syswrite($fh, $buf);
     $fh->close();
 
-    return $res;
+    if (defined($res)) {
+	return 1;
+    } elsif (my $err = $!) {
+	if (defined($ignore_list)) {
+	    for my $to_ignore ($ignore_list->@*) {
+		if ($err == $to_ignore) {
+		    return 1;
+		}
+	    }
+	}
+	warn "error writing '$buf' to '$filename': $err\n";
+    }
+
+    return 0;
 }
 
 sub pci_device_info {
-- 
2.39.5





More information about the pve-devel mailing list