From 2943910ec3c92353622db4025fbdf3d8df12c964 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Wed, 30 Aug 2023 14:37:52 +0200 Subject: [PATCH] tools: allow to force UTF-8 encoding for file_set_contents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 [FG: use flag parameter instead of encoding as a string use stricter 'UTF-8' instaed of 'utf8' (see 'perldoc Enocode')] Suggested-by: Fabian Grünbichler [FE: implement changes suggested by Fabian move binmode call to where $fh is known to be set] Signed-off-by: Fiona Ebner --- src/PVE/Tools.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 2cfe93f..569f0cd 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -238,7 +238,7 @@ sub lock_file { } sub file_set_contents { - my ($filename, $data, $perm) = @_; + my ($filename, $data, $perm, $force_utf8) = @_; $perm = 0644 if !defined($perm); @@ -253,6 +253,9 @@ sub file_set_contents { } } die "unable to open file '$tmpname' - $!\n" if !$fh; + + binmode($fh, ":encoding(UTF-8)") if $force_utf8; + die "unable to write '$tmpname' - $!\n" unless print $fh $data; die "closing file '$tmpname' failed - $!\n" unless close $fh; };