[pbs-devel] [RFC PATCH proxmox-backup] api2/types ArchiveEntry: fall back to iso 8859-1 if not valid utf8

Dominik Csapak d.csapak at proxmox.com
Wed May 5 09:53:02 CEST 2021


In case a file name is not valid utf-8 we fall back to iso 8859-1. this works
neatly, since the byte sequence of visible (non-control) characters in iso
maps 1:1 to unicode codepoints which happens when we do 'u8 as char' in rust.

Theoretically the source could also be another iso variant (e.g. iso 8859-15),
but this is (at this point) impossible to detect because the bytes simply have
a different meaning.

If we want, we could somehow make this configurable (e.g. with a parameter),
but i'm not sure it is worth the effort

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/api2/types/mod.rs | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/api2/types/mod.rs b/src/api2/types/mod.rs
index e829f207..03660531 100644
--- a/src/api2/types/mod.rs
+++ b/src/api2/types/mod.rs
@@ -1379,10 +1379,17 @@ impl ArchiveEntry {
         entry_type: Option<&DirEntryAttribute>,
         size: Option<u64>,
     ) -> Self {
+        let name = filepath.split(|x| *x == b'/').last().unwrap();
+        let text = match String::from_utf8(name.to_vec()) {
+            Ok(text) => text,
+            Err(err) => { // fall back to iso-8859-1
+                err.as_bytes().iter().map(|&c| c as char).collect()
+            }
+        };
+
         Self {
             filepath: base64::encode(filepath),
-            text: String::from_utf8_lossy(filepath.split(|x| *x == b'/').last().unwrap())
-                .to_string(),
+            text,
             entry_type: match entry_type {
                 Some(entry_type) => CatalogEntryType::from(entry_type).to_string(),
                 None => "v".to_owned(),
-- 
2.20.1






More information about the pbs-devel mailing list