Use lossy conversion for UTF-8/16 strings instead of panicking.

This commit is contained in:
Rafał Harabień 2017-10-06 15:51:01 +02:00
parent bc835d59b5
commit bfa4984ae3
2 changed files with 11 additions and 7 deletions

View File

@ -3,7 +3,6 @@ use std::fmt;
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::io::{Cursor, ErrorKind, SeekFrom}; use std::io::{Cursor, ErrorKind, SeekFrom};
use std::str;
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use chrono::{DateTime, Date, TimeZone, Local}; use chrono::{DateTime, Date, TimeZone, Local};
@ -92,14 +91,20 @@ pub struct FatDirEntry<'a, 'b: 'a> {
impl <'a, 'b> FatDirEntry<'a, 'b> { impl <'a, 'b> FatDirEntry<'a, 'b> {
pub fn short_file_name(&self) -> String { pub fn short_file_name(&self) -> String {
let name = str::from_utf8(&self.data.name[0..8]).unwrap().trim_right(); let name_str = String::from_utf8_lossy(&self.data.name[0..8]);
let ext = str::from_utf8(&self.data.name[8..11]).unwrap().trim_right(); let ext_str = String::from_utf8_lossy(&self.data.name[8..11]);
if ext == "" { name.to_string() } else { format!("{}.{}", name, ext) } 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 { pub fn file_name(&self) -> String {
if self.lfn.len() > 0 { if self.lfn.len() > 0 {
String::from_utf16(&self.lfn).unwrap() String::from_utf16_lossy(&self.lfn)
} else { } else {
self.short_file_name() self.short_file_name()
} }

View File

@ -4,7 +4,6 @@ use core::iter;
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{Error, ErrorKind, SeekFrom}; use std::io::{Error, ErrorKind, SeekFrom};
use std::io; use std::io;
use std::str;
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use file::FatFile; use file::FatFile;
@ -120,7 +119,7 @@ impl <'a> FatFileSystem<'a> {
} }
pub fn volume_label(&self) -> String { 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> { pub fn root_dir<'b>(&'b self) -> FatDir<'b, 'a> {