forked from M-Labs/rust-fatfs
Avoid using unwrap() in examples
New examples require Rust 1.26 because of returning error from main() function.
This commit is contained in:
parent
a83115dd45
commit
3d29f66aba
@ -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.
|
||||
|
||||
|
@ -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(())
|
||||
}
|
||||
|
@ -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::<Local>::from(e.modified()).format("%Y-%m-%d %H:%M:%S").to_string();
|
||||
println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -65,19 +65,20 @@ impl <T: ReadWriteSeek> Seek for Partition<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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::<fs::File>::new(file, first_lba, last_lba - first_lba + 1).unwrap();
|
||||
let partition = Partition::<fs::File>::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(())
|
||||
}
|
||||
|
@ -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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user