diff --git a/README.md b/README.md index b5e681a..552ec46 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,12 @@ Put this in your crate root: You can start using library now: - let img_file = File::open("fat.img").unwrap(); + let img_file = File::open("fat.img")?; let mut buf_stream = fatfs::BufStream::new(img_file); - let fs = fatfs::FileSystem::new(&mut buf_stream, fatfs::FsOptions::new()).unwrap(); + let fs = fatfs::FileSystem::new(&mut buf_stream, fatfs::FsOptions::new())?; let mut root_dir = fs.root_dir(); - let mut file = root_dir.create_file("hello.txt").unwrap(); - file.write_all(b"Hello World!").unwrap(); + let mut file = root_dir.create_file("hello.txt")?; + file.write_all(b"Hello World!")?; See more examples in `examples` subdirectory. diff --git a/examples/cat.rs b/examples/cat.rs index 2592f46..e529814 100644 --- a/examples/cat.rs +++ b/examples/cat.rs @@ -2,18 +2,18 @@ extern crate fatfs; use std::env; use std::fs::File; -use std::io::prelude::*; -use std::str; +use std::io::{self, prelude::*}; use fatfs::{FileSystem, FsOptions, BufStream}; -fn main() { - let file = File::open("resources/fat32.img").unwrap(); +fn main() -> io::Result<()> { + let file = File::open("resources/fat32.img")?; let mut buf_rdr = BufStream::new(file); - let fs = FileSystem::new(&mut buf_rdr, FsOptions::new()).unwrap(); + let fs = FileSystem::new(&mut buf_rdr, FsOptions::new())?; let mut root_dir = fs.root_dir(); - let mut file = root_dir.open_file(&env::args().nth(1).unwrap()).unwrap(); + let mut file = root_dir.open_file(&env::args().nth(1).unwrap())?; let mut buf = vec![]; - file.read_to_end(&mut buf).unwrap(); - print!("{}", str::from_utf8(&buf).unwrap()); + file.read_to_end(&mut buf)?; + print!("{}", String::from_utf8_lossy(&buf)); + Ok(()) } diff --git a/examples/ls.rs b/examples/ls.rs index 8281f2f..16989f1 100644 --- a/examples/ls.rs +++ b/examples/ls.rs @@ -3,6 +3,7 @@ extern crate chrono; use std::env; use std::fs::File; +use std::io; use chrono::{DateTime, Local}; use fatfs::{FileSystem, FsOptions, BufStream}; @@ -22,19 +23,20 @@ fn format_file_size(size: u64) -> String { } } -fn main() { - let file = File::open("resources/fat32.img").unwrap(); +fn main() -> io::Result<()> { + let file = File::open("resources/fat32.img")?; let mut buf_rdr = BufStream::new(file); - let fs = FileSystem::new(&mut buf_rdr, FsOptions::new()).unwrap(); + let fs = FileSystem::new(&mut buf_rdr, FsOptions::new())?; let mut root_dir = fs.root_dir(); let dir = match env::args().nth(1) { None => root_dir, Some(ref path) if path == "." => root_dir, - Some(ref path) => root_dir.open_dir(&path).unwrap(), + Some(ref path) => root_dir.open_dir(&path)?, }; for r in dir.iter() { - let e = r.unwrap(); + let e = r?; let modified = DateTime::::from(e.modified()).format("%Y-%m-%d %H:%M:%S").to_string(); println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name()); } + Ok(()) } diff --git a/examples/partition.rs b/examples/partition.rs index 2559ac4..0c8598c 100644 --- a/examples/partition.rs +++ b/examples/partition.rs @@ -65,19 +65,20 @@ impl Seek for Partition { } } -fn main() { +fn main() -> io::Result<()> { // Open disk image - let file = fs::File::open("resources/fat32.img").unwrap(); + let file = fs::File::open("resources/fat32.img")?; // Provide sample partition localization. In real application it should be read from MBR/GPT. let first_lba = 0; let last_lba = 10000; // Create partition using provided start address and size in bytes - let partition = Partition::::new(file, first_lba, last_lba - first_lba + 1).unwrap(); + let partition = Partition::::new(file, first_lba, last_lba - first_lba + 1)?; // Create buffered stream to optimize file access let mut buf_rdr = BufStream::new(partition); // Finally initialize filesystem struct using provided partition - let fs = FileSystem::new(&mut buf_rdr, FsOptions::new()).unwrap(); + let fs = FileSystem::new(&mut buf_rdr, FsOptions::new())?; // Read and display volume label println!("Volume Label: {}", fs.volume_label()); - // other operations + // other operations... + Ok(()) } diff --git a/examples/write.rs b/examples/write.rs index 2f5a0ae..9fe01fc 100644 --- a/examples/write.rs +++ b/examples/write.rs @@ -1,21 +1,22 @@ extern crate fatfs; use std::fs::OpenOptions; -use std::io::prelude::*; +use std::io::{self, prelude::*}; use fatfs::{FileSystem, FsOptions, BufStream}; -fn main() { +fn main() -> io::Result<()> { let img_file = match OpenOptions::new().read(true).write(true).open("fat.img") { Ok(file) => file, Err(err) => { - println!("Failed to open image: {}", err); - return; + println!("Failed to open image!"); + return Err(err); } }; let mut buf_stream = BufStream::new(img_file); let options = FsOptions::new().update_accessed_date(true); - let fs = FileSystem::new(&mut buf_stream, options).unwrap(); - let mut file = fs.root_dir().create_file("hello.txt").unwrap(); - file.write_all(b"Hello World!").unwrap(); + let fs = FileSystem::new(&mut buf_stream, options)?; + let mut file = fs.root_dir().create_file("hello.txt")?; + file.write_all(b"Hello World!")?; + Ok(()) }