From bfa4984ae36b4edd125037f9b1cfac09918dab63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Fri, 6 Oct 2017 15:51:01 +0200 Subject: [PATCH] Use lossy conversion for UTF-8/16 strings instead of panicking. --- src/dir.rs | 15 ++++++++++----- src/fs.rs | 3 +-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index cb6c879..5fe901d 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -3,7 +3,6 @@ use std::fmt; use std::io::prelude::*; use std::io; use std::io::{Cursor, ErrorKind, SeekFrom}; -use std::str; use byteorder::{LittleEndian, ReadBytesExt}; use chrono::{DateTime, Date, TimeZone, Local}; @@ -92,14 +91,20 @@ pub struct FatDirEntry<'a, 'b: 'a> { impl <'a, 'b> FatDirEntry<'a, 'b> { pub fn short_file_name(&self) -> String { - let name = str::from_utf8(&self.data.name[0..8]).unwrap().trim_right(); - let ext = str::from_utf8(&self.data.name[8..11]).unwrap().trim_right(); - if ext == "" { name.to_string() } else { format!("{}.{}", name, ext) } + let name_str = String::from_utf8_lossy(&self.data.name[0..8]); + let ext_str = String::from_utf8_lossy(&self.data.name[8..11]); + let name_trimmed = name_str.trim_right(); + let ext_trimmed = ext_str.trim_right(); + if ext_trimmed.is_empty() { + name_trimmed.to_string() + } else { + format!("{}.{}", name_trimmed, ext_trimmed) + } } pub fn file_name(&self) -> String { if self.lfn.len() > 0 { - String::from_utf16(&self.lfn).unwrap() + String::from_utf16_lossy(&self.lfn) } else { self.short_file_name() } diff --git a/src/fs.rs b/src/fs.rs index 5633670..d0d23f1 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -4,7 +4,6 @@ use core::iter; use std::io::prelude::*; use std::io::{Error, ErrorKind, SeekFrom}; use std::io; -use std::str; use byteorder::{LittleEndian, ReadBytesExt}; use file::FatFile; @@ -120,7 +119,7 @@ impl <'a> FatFileSystem<'a> { } pub fn volume_label(&self) -> String { - str::from_utf8(&self.boot.bpb.volume_label).unwrap().trim_right().to_string() + String::from_utf8_lossy(&self.boot.bpb.volume_label).trim_right().to_string() } pub fn root_dir<'b>(&'b self) -> FatDir<'b, 'a> {