Rename some functions based on std::fs module.

This commit is contained in:
Rafał Harabień 2017-09-24 14:34:07 +02:00
parent df2852351b
commit c6ba07848e
3 changed files with 28 additions and 23 deletions

View File

@ -4,7 +4,9 @@ Rust FAT
Introduction Introduction
------------ ------------
FAT Filesystem library implemented in Rust. Supports FAT12, FAT16 and FAT32. FAT filesystem library implemented in Rust.
Supports filesystem versions: FAT12, FAT16, FAT32. Library is read-only at this point but write support is planned. LFN (Long File Name) is not supported yet.
License License
------- -------

View File

@ -47,13 +47,13 @@ pub struct FatDirEntry {
} }
impl FatDirEntry { impl FatDirEntry {
pub fn get_name(&self) -> String { pub fn file_name(&self) -> String {
let name = str::from_utf8(&self.data.name[0..8]).unwrap().trim_right(); 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(); let ext = str::from_utf8(&self.data.name[8..11]).unwrap().trim_right();
if ext == "" { name.to_string() } else { format!("{}.{}", name, ext) } if ext == "" { name.to_string() } else { format!("{}.{}", name, ext) }
} }
pub fn get_attrs(&self) -> FatFileAttributes { pub fn attributes(&self) -> FatFileAttributes {
self.data.attrs self.data.attrs
} }
@ -61,38 +61,42 @@ impl FatDirEntry {
self.data.attrs.contains(FatFileAttributes::DIRECTORY) self.data.attrs.contains(FatFileAttributes::DIRECTORY)
} }
pub fn get_cluster(&self) -> u32 { pub fn is_file(&self) -> bool {
!self.is_dir()
}
pub(crate) fn first_cluster(&self) -> u32 {
((self.data.first_cluster_hi as u32) << 16) | self.data.first_cluster_lo as u32 ((self.data.first_cluster_hi as u32) << 16) | self.data.first_cluster_lo as u32
} }
pub fn get_file(&self) -> FatFile { pub fn to_file(&self) -> FatFile {
if self.is_dir() { if self.is_dir() {
panic!("This is a directory"); panic!("This is a directory");
} }
FatFile::new(self.get_cluster(), Some(self.data.size), self.state.clone()) FatFile::new(self.first_cluster(), Some(self.data.size), self.state.clone())
} }
pub fn get_dir(&self) -> FatDir { pub fn to_dir(&self) -> FatDir {
if !self.is_dir() { if !self.is_dir() {
panic!("This is a file"); panic!("This is a file");
} }
let file = FatFile::new(self.get_cluster(), None, self.state.clone()); let file = FatFile::new(self.first_cluster(), None, self.state.clone());
FatDir::new(Box::new(file), self.state.clone()) FatDir::new(Box::new(file), self.state.clone())
} }
pub fn get_size(&self) -> u32 { pub fn len(&self) -> u64 {
self.data.size self.data.size as u64
} }
pub fn get_create_time(&self) -> DateTime<Local> { pub fn created(&self) -> DateTime<Local> {
Self::convert_date_time(self.data.create_date, self.data.create_time_1) Self::convert_date_time(self.data.create_date, self.data.create_time_1)
} }
pub fn get_access_date(&self) -> Date<Local> { pub fn accessed(&self) -> Date<Local> {
Self::convert_date(self.data.access_date) Self::convert_date(self.data.access_date)
} }
pub fn get_modify_time(&self) -> DateTime<Local> { pub fn modified(&self) -> DateTime<Local> {
Self::convert_date_time(self.data.modify_date, self.data.modify_time) Self::convert_date_time(self.data.modify_date, self.data.modify_time)
} }
@ -100,7 +104,7 @@ impl FatDirEntry {
let (year, month, day) = ((dos_date >> 9) + 1980, (dos_date >> 5) & 0xF, dos_date & 0x1F); let (year, month, day) = ((dos_date >> 9) + 1980, (dos_date >> 5) & 0xF, dos_date & 0x1F);
Local.ymd(year as i32, month as u32, day as u32) Local.ymd(year as i32, month as u32, day as u32)
} }
fn convert_date_time(dos_date: u16, dos_time: u16) -> DateTime<Local> { fn convert_date_time(dos_date: u16, dos_time: u16) -> DateTime<Local> {
let (hour, min, sec) = (dos_time >> 11, (dos_time >> 5) & 0x3F, (dos_time & 0x1F) * 2); let (hour, min, sec) = (dos_time >> 11, (dos_time >> 5) & 0x3F, (dos_time & 0x1F) * 2);
Self::convert_date(dos_date).and_hms(hour as u32, min as u32, sec as u32) Self::convert_date(dos_date).and_hms(hour as u32, min as u32, sec as u32)
@ -163,8 +167,7 @@ impl FatDir {
fn find_entry(&mut self, name: &str) -> io::Result<FatDirEntry> { fn find_entry(&mut self, name: &str) -> io::Result<FatDirEntry> {
let entries: Vec<FatDirEntry> = self.list()?; let entries: Vec<FatDirEntry> = self.list()?;
for e in entries { for e in entries {
if e.get_name().eq_ignore_ascii_case(name) { if e.file_name().eq_ignore_ascii_case(name) {
println!("find entry {}", name);
return Ok(e); return Ok(e);
} }
} }
@ -175,8 +178,8 @@ impl FatDir {
let (name, rest_opt) = Self::split_path(path); let (name, rest_opt) = Self::split_path(path);
let e = self.find_entry(name)?; let e = self.find_entry(name)?;
match rest_opt { match rest_opt {
Some(rest) => e.get_dir().get_dir(rest), Some(rest) => e.to_dir().get_dir(rest),
None => Ok(e.get_dir()) None => Ok(e.to_dir())
} }
} }
@ -184,8 +187,8 @@ impl FatDir {
let (name, rest_opt) = Self::split_path(path); let (name, rest_opt) = Self::split_path(path);
let e = self.find_entry(name)?; let e = self.find_entry(name)?;
match rest_opt { match rest_opt {
Some(rest) => e.get_dir().get_file(rest), Some(rest) => e.to_dir().get_file(rest),
None => Ok(e.get_file()) None => Ok(e.to_file())
} }
} }
} }

View File

@ -21,11 +21,11 @@ fn open_fs(filename: &str) -> FatFileSystem {
fn test_root_dir(mut fs: FatFileSystem) { fn test_root_dir(mut fs: FatFileSystem) {
let mut root_dir = fs.root_dir(); let mut root_dir = fs.root_dir();
let entries = root_dir.list().unwrap(); let entries = root_dir.list().unwrap();
let names = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>(); let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
assert_eq!(names, ["LONG.TXT", "SHORT.TXT", "VERY"]); assert_eq!(names, ["LONG.TXT", "SHORT.TXT", "VERY"]);
// Try read again // Try read again
let entries = root_dir.list().unwrap(); let entries = root_dir.list().unwrap();
let names2 = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>(); let names2 = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
assert_eq!(names2, names); assert_eq!(names2, names);
} }
@ -105,7 +105,7 @@ fn test_get_dir_by_path(mut fs: FatFileSystem) {
let mut root_dir = fs.root_dir(); let mut root_dir = fs.root_dir();
let mut dir = root_dir.get_dir("very/long/path/").unwrap(); let mut dir = root_dir.get_dir("very/long/path/").unwrap();
let entries = dir.list().unwrap(); let entries = dir.list().unwrap();
let names = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>(); let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
assert_eq!(names, [".", "..", "TEST.TXT"]); assert_eq!(names, [".", "..", "TEST.TXT"]);
} }