[pve-devel] [PATCH pve-common 1/3] tools: allow to specify file encoding for file_set_contents

Lukas Wagner l.wagner at proxmox.com
Wed Aug 30 14:37:52 CEST 2023


Rationale: This is used from cfs_write_file, which is now also used to
write utf8-encoded strings that come from Rust. If no encoding is
specified while writing the file, we run into problems with certain
special characters (e.g. 'ü')

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 src/PVE/Tools.pm | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 50240c8..f7224f8 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -237,7 +237,7 @@ sub lock_file {
 }
 
 sub file_set_contents {
-    my ($filename, $data, $perm)  = @_;
+    my ($filename, $data, $perm, $encoding)  = @_;
 
     $perm = 0644 if !defined($perm);
 
@@ -247,6 +247,11 @@ sub file_set_contents {
 	my ($fh, $tries) = (undef, 0);
 	while (!$fh && $tries++ < 3) {
 	    $fh = IO::File->new($tmpname, O_WRONLY|O_CREAT|O_EXCL, $perm);
+
+	    if (defined($encoding)) {
+		binmode($fh, ":encoding($encoding)");
+	    }
+
 	    if (!$fh && $! == EEXIST) {
 		unlink($tmpname) or die "unable to delete old temp file: $!\n";
 	    }
-- 
2.39.2






More information about the pve-devel mailing list