* [pbs-devel] [RFC PATCH proxmox-backup] api2/types ArchiveEntry: fall back to iso 8859-1 if not valid utf8
@ 2021-05-05 7:53 Dominik Csapak
0 siblings, 0 replies; only message in thread
From: Dominik Csapak @ 2021-05-05 7:53 UTC (permalink / raw)
To: pbs-devel
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@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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-05-05 7:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 7:53 [pbs-devel] [RFC PATCH proxmox-backup] api2/types ArchiveEntry: fall back to iso 8859-1 if not valid utf8 Dominik Csapak
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal