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:
|
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 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 root_dir = fs.root_dir();
|
||||||
let mut file = root_dir.create_file("hello.txt").unwrap();
|
let mut file = root_dir.create_file("hello.txt")?;
|
||||||
file.write_all(b"Hello World!").unwrap();
|
file.write_all(b"Hello World!")?;
|
||||||
|
|
||||||
See more examples in `examples` subdirectory.
|
See more examples in `examples` subdirectory.
|
||||||
|
|
||||||
|
@ -2,18 +2,18 @@ extern crate fatfs;
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::{self, prelude::*};
|
||||||
use std::str;
|
|
||||||
|
|
||||||
use fatfs::{FileSystem, FsOptions, BufStream};
|
use fatfs::{FileSystem, FsOptions, BufStream};
|
||||||
|
|
||||||
fn main() {
|
fn main() -> io::Result<()> {
|
||||||
let file = File::open("resources/fat32.img").unwrap();
|
let file = File::open("resources/fat32.img")?;
|
||||||
let mut buf_rdr = BufStream::new(file);
|
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 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![];
|
let mut buf = vec![];
|
||||||
file.read_to_end(&mut buf).unwrap();
|
file.read_to_end(&mut buf)?;
|
||||||
print!("{}", str::from_utf8(&buf).unwrap());
|
print!("{}", String::from_utf8_lossy(&buf));
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ extern crate chrono;
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local};
|
||||||
|
|
||||||
use fatfs::{FileSystem, FsOptions, BufStream};
|
use fatfs::{FileSystem, FsOptions, BufStream};
|
||||||
@ -22,19 +23,20 @@ fn format_file_size(size: u64) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> io::Result<()> {
|
||||||
let file = File::open("resources/fat32.img").unwrap();
|
let file = File::open("resources/fat32.img")?;
|
||||||
let mut buf_rdr = BufStream::new(file);
|
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 root_dir = fs.root_dir();
|
||||||
let dir = match env::args().nth(1) {
|
let dir = match env::args().nth(1) {
|
||||||
None => root_dir,
|
None => root_dir,
|
||||||
Some(ref path) if path == "." => 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() {
|
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();
|
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());
|
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
|
// 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.
|
// Provide sample partition localization. In real application it should be read from MBR/GPT.
|
||||||
let first_lba = 0;
|
let first_lba = 0;
|
||||||
let last_lba = 10000;
|
let last_lba = 10000;
|
||||||
// Create partition using provided start address and size in bytes
|
// 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
|
// Create buffered stream to optimize file access
|
||||||
let mut buf_rdr = BufStream::new(partition);
|
let mut buf_rdr = BufStream::new(partition);
|
||||||
// Finally initialize filesystem struct using provided 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
|
// Read and display volume label
|
||||||
println!("Volume Label: {}", fs.volume_label());
|
println!("Volume Label: {}", fs.volume_label());
|
||||||
// other operations
|
// other operations...
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,22 @@
|
|||||||
extern crate fatfs;
|
extern crate fatfs;
|
||||||
|
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::prelude::*;
|
use std::io::{self, prelude::*};
|
||||||
|
|
||||||
use fatfs::{FileSystem, FsOptions, BufStream};
|
use fatfs::{FileSystem, FsOptions, BufStream};
|
||||||
|
|
||||||
fn main() {
|
fn main() -> io::Result<()> {
|
||||||
let img_file = match OpenOptions::new().read(true).write(true).open("fat.img") {
|
let img_file = match OpenOptions::new().read(true).write(true).open("fat.img") {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("Failed to open image: {}", err);
|
println!("Failed to open image!");
|
||||||
return;
|
return Err(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut buf_stream = BufStream::new(img_file);
|
let mut buf_stream = BufStream::new(img_file);
|
||||||
let options = FsOptions::new().update_accessed_date(true);
|
let options = FsOptions::new().update_accessed_date(true);
|
||||||
let fs = FileSystem::new(&mut buf_stream, options).unwrap();
|
let fs = FileSystem::new(&mut buf_stream, options)?;
|
||||||
let mut file = fs.root_dir().create_file("hello.txt").unwrap();
|
let mut file = fs.root_dir().create_file("hello.txt")?;
|
||||||
file.write_all(b"Hello World!").unwrap();
|
file.write_all(b"Hello World!")?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user