forked from M-Labs/rust-fatfs
Add .editorconfig file and fix whitespaces in existing files. (#4)
This commit is contained in:
parent
cffa875d34
commit
529d717e30
14
.editorconfig
Normal file
14
.editorconfig
Normal file
@ -0,0 +1,14 @@
|
||||
# EditorConfig helps developers define and maintain consistent
|
||||
# coding styles between different editors and IDEs
|
||||
# http://editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
[*.rs]
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
max_line_length = 120
|
92
src/dir.rs
92
src/dir.rs
@ -104,13 +104,13 @@ impl DirFileEntryData {
|
||||
let n = ((self.first_cluster_hi as u32) << 16) | self.first_cluster_lo as u32;
|
||||
if n == 0 { None } else { Some(n) }
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn set_first_cluster(&mut self, cluster: Option<u32>) {
|
||||
let n = cluster.unwrap_or(0);
|
||||
self.first_cluster_hi = (n >> 16) as u16;
|
||||
self.first_cluster_lo = (n & 0xFFFF) as u16;
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn size(&self) -> Option<u32> {
|
||||
if self.is_file() {
|
||||
Some(self.size)
|
||||
@ -118,24 +118,24 @@ impl DirFileEntryData {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn set_size(&mut self, size: u32) {
|
||||
self.size = size;
|
||||
}
|
||||
|
||||
|
||||
pub fn is_dir(&self) -> bool {
|
||||
self.attrs.contains(FileAttributes::DIRECTORY)
|
||||
}
|
||||
|
||||
|
||||
pub fn is_file(&self) -> bool {
|
||||
!self.is_dir()
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn set_modified(&mut self, date_time: DateTime) {
|
||||
self.modify_date = date_time.date.to_u16();
|
||||
self.modify_time = date_time.time.to_u16();
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn serialize(&self, wrt: &mut Write) -> io::Result<()> {
|
||||
wrt.write_all(&self.name)?;
|
||||
wrt.write_u8(self.attrs.bits())?;
|
||||
@ -151,11 +151,11 @@ impl DirFileEntryData {
|
||||
wrt.write_u32::<LittleEndian>(self.size)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn is_free(&self) -> bool {
|
||||
self.name[0] == DIR_ENTRY_FREE_FLAG
|
||||
}
|
||||
|
||||
|
||||
fn is_end(&self) -> bool {
|
||||
self.name[0] == 0
|
||||
}
|
||||
@ -192,11 +192,11 @@ impl DirLfnEntryData {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn is_free(&self) -> bool {
|
||||
self.order == DIR_ENTRY_FREE_FLAG
|
||||
}
|
||||
|
||||
|
||||
fn is_end(&self) -> bool {
|
||||
self.order == 0
|
||||
}
|
||||
@ -215,7 +215,7 @@ impl DirEntryData {
|
||||
&mut DirEntryData::Lfn(ref mut lfn) => lfn.serialize(wrt),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn deserialize(rdr: &mut Read) -> io::Result<DirEntryData> {
|
||||
let mut name = [0; 11];
|
||||
rdr.read_exact(&mut name)?;
|
||||
@ -251,14 +251,14 @@ impl DirEntryData {
|
||||
Ok(DirEntryData::File(data))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn is_free(&self) -> bool {
|
||||
match self {
|
||||
&DirEntryData::File(ref file) => file.is_free(),
|
||||
&DirEntryData::Lfn(ref lfn) => lfn.is_free(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn is_end(&self) -> bool {
|
||||
match self {
|
||||
&DirEntryData::File(ref file) => file.is_end(),
|
||||
@ -280,7 +280,7 @@ impl Date {
|
||||
let (year, month, day) = ((dos_date >> 9) + 1980, (dos_date >> 5) & 0xF, dos_date & 0x1F);
|
||||
Date { year, month, day }
|
||||
}
|
||||
|
||||
|
||||
fn to_u16(&self) -> u16 {
|
||||
((self.year - 1980) << 9) | (self.month << 5) | self.day
|
||||
}
|
||||
@ -299,7 +299,7 @@ impl Time {
|
||||
let (hour, min, sec) = (dos_time >> 11, (dos_time >> 5) & 0x3F, (dos_time & 0x1F) * 2);
|
||||
Time { hour, min, sec }
|
||||
}
|
||||
|
||||
|
||||
fn to_u16(&self) -> u16 {
|
||||
(self.hour << 11) | (self.min << 5) | (self.sec / 2)
|
||||
}
|
||||
@ -375,7 +375,7 @@ impl <'a, 'b> DirEntry<'a, 'b> {
|
||||
format!("{}.{}", name_trimmed, ext_trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns long file name or if it doesn't exist fallbacks to short file name.
|
||||
pub fn file_name(&self) -> String {
|
||||
if self.lfn.len() > 0 {
|
||||
@ -384,33 +384,33 @@ impl <'a, 'b> DirEntry<'a, 'b> {
|
||||
self.short_file_name()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns file attributes
|
||||
pub fn attributes(&self) -> FileAttributes {
|
||||
self.data.attrs
|
||||
}
|
||||
|
||||
|
||||
/// Checks if entry belongs to directory.
|
||||
pub fn is_dir(&self) -> bool {
|
||||
self.data.is_dir()
|
||||
}
|
||||
|
||||
|
||||
/// Checks if entry belongs to regular file.
|
||||
pub fn is_file(&self) -> bool {
|
||||
self.data.is_file()
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn first_cluster(&self) -> Option<u32> {
|
||||
self.data.first_cluster()
|
||||
}
|
||||
|
||||
|
||||
fn entry_info(&self) -> FileEntryInfo {
|
||||
FileEntryInfo {
|
||||
data: self.data.clone(),
|
||||
pos: self.entry_pos,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns File struct for this entry.
|
||||
///
|
||||
/// Panics if this is not a file.
|
||||
@ -418,7 +418,7 @@ impl <'a, 'b> DirEntry<'a, 'b> {
|
||||
assert!(!self.is_dir(), "Not a file entry");
|
||||
File::new(self.first_cluster(), Some(self.entry_info()), self.fs)
|
||||
}
|
||||
|
||||
|
||||
/// Returns Dir struct for this entry.
|
||||
///
|
||||
/// Panics if this is not a directory.
|
||||
@ -432,22 +432,22 @@ impl <'a, 'b> DirEntry<'a, 'b> {
|
||||
None => self.fs.root_dir(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns file size or 0 for directory.
|
||||
pub fn len(&self) -> u64 {
|
||||
self.data.size as u64
|
||||
}
|
||||
|
||||
|
||||
/// Returns file creation date and time.
|
||||
pub fn created(&self) -> DateTime {
|
||||
DateTime::from_u16(self.data.create_date, self.data.create_time_1)
|
||||
}
|
||||
|
||||
|
||||
/// Returns file last access date.
|
||||
pub fn accessed(&self) -> Date {
|
||||
Date::from_u16(self.data.access_date)
|
||||
}
|
||||
|
||||
|
||||
/// Returns file last modification date and time.
|
||||
pub fn modified(&self) -> DateTime {
|
||||
DateTime::from_u16(self.data.modify_date, self.data.modify_time)
|
||||
@ -468,11 +468,11 @@ pub struct Dir<'a, 'b: 'a> {
|
||||
}
|
||||
|
||||
impl <'a, 'b> Dir<'a, 'b> {
|
||||
|
||||
|
||||
pub(crate) fn new(stream: DirRawStream<'a, 'b>, fs: FileSystemRef<'a, 'b>) -> Dir<'a, 'b> {
|
||||
Dir { stream, fs }
|
||||
}
|
||||
|
||||
|
||||
/// Creates directory entries iterator
|
||||
pub fn iter(&self) -> DirIter<'a, 'b> {
|
||||
DirIter {
|
||||
@ -481,14 +481,14 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
err: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) {
|
||||
let mut path_split = path.trim_matches('/').splitn(2, "/");
|
||||
let comp = path_split.next().unwrap(); // safe unwrap - splitn always returns at least one element
|
||||
let rest_opt = path_split.next();
|
||||
(comp, rest_opt)
|
||||
}
|
||||
|
||||
|
||||
fn find_entry(&mut self, name: &str) -> io::Result<DirEntry<'a, 'b>> {
|
||||
for r in self.iter() {
|
||||
let e = r?;
|
||||
@ -498,7 +498,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
}
|
||||
Err(io::Error::new(ErrorKind::NotFound, "file not found"))
|
||||
}
|
||||
|
||||
|
||||
/// Opens existing directory
|
||||
pub fn open_dir(&mut self, path: &str) -> io::Result<Dir<'a, 'b>> {
|
||||
let (name, rest_opt) = Self::split_path(path);
|
||||
@ -508,7 +508,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
None => Ok(e.to_dir())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Opens existing file.
|
||||
pub fn open_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
|
||||
let (name, rest_opt) = Self::split_path(path);
|
||||
@ -518,7 +518,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
None => Ok(e.to_file())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Creates new file or opens existing.
|
||||
pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
|
||||
let (name, rest_opt) = Self::split_path(path);
|
||||
@ -533,7 +533,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn is_empty(&mut self) -> io::Result<bool> {
|
||||
for r in self.iter() {
|
||||
let e = r?;
|
||||
@ -544,7 +544,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
|
||||
/// Removes existing file or directory.
|
||||
///
|
||||
/// Make sure there is no reference to this file (no File instance) or filesystem corruption
|
||||
@ -581,7 +581,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn find_free_entries(&mut self, num_entries: usize) -> io::Result<DirRawStream<'a, 'b>> {
|
||||
let mut stream = self.stream.clone();
|
||||
let mut first_free = 0;
|
||||
@ -611,7 +611,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn gen_short_name(name: &str) -> [u8;11] {
|
||||
// short name is always uppercase
|
||||
let mut name_upper = name.to_uppercase();
|
||||
@ -634,7 +634,7 @@ impl <'a, 'b> Dir<'a, 'b> {
|
||||
// FIXME: make sure short name is unique...
|
||||
short_name
|
||||
}
|
||||
|
||||
|
||||
fn create_file_entry(&mut self, name: &str) -> io::Result<DirEntry<'a, 'b>> {
|
||||
if name.len() > 255 {
|
||||
return Err(io::Error::new(ErrorKind::InvalidInput, "filename too long"));
|
||||
@ -700,7 +700,7 @@ impl <'a, 'b> DirIter<'a, 'b> {
|
||||
fn read_dir_entry_raw_data(&mut self) -> io::Result<DirEntryData> {
|
||||
DirEntryData::deserialize(&mut self.stream)
|
||||
}
|
||||
|
||||
|
||||
fn read_dir_entry(&mut self) -> io::Result<Option<DirEntry<'a, 'b>>> {
|
||||
let mut lfn_buf = LongNameBuilder::new();
|
||||
let mut offset = self.stream.seek(SeekFrom::Current(0))?;
|
||||
@ -788,12 +788,12 @@ impl LongNameBuilder {
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.buf.clear();
|
||||
self.index = 0;
|
||||
}
|
||||
|
||||
|
||||
fn to_vec(mut self) -> Vec<u16> {
|
||||
if self.index == 1 {
|
||||
self.truncate();
|
||||
@ -803,7 +803,7 @@ impl LongNameBuilder {
|
||||
Vec::<u16>::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn truncate(&mut self) {
|
||||
// Truncate 0 and 0xFFFF characters from LFN buffer
|
||||
let mut lfn_len = self.buf.len();
|
||||
@ -818,7 +818,7 @@ impl LongNameBuilder {
|
||||
}
|
||||
self.buf.truncate(lfn_len);
|
||||
}
|
||||
|
||||
|
||||
fn process(&mut self, data: &DirLfnEntryData) {
|
||||
let is_last = (data.order & LFN_ENTRY_LAST_FLAG) != 0;
|
||||
let index = data.order & 0x1F;
|
||||
@ -848,7 +848,7 @@ impl LongNameBuilder {
|
||||
self.buf[pos+5..pos+11].copy_from_slice(&data.name_1);
|
||||
self.buf[pos+11..pos+13].copy_from_slice(&data.name_2);
|
||||
}
|
||||
|
||||
|
||||
fn validate_chksum(&mut self, short_name: &[u8]) {
|
||||
let chksum = lfn_checksum(short_name);
|
||||
if chksum != self.chksum {
|
||||
|
18
src/file.rs
18
src/file.rs
@ -31,7 +31,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
entry_dirty: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn update_size(&mut self) {
|
||||
let offset = self.offset;
|
||||
match self.entry {
|
||||
@ -44,7 +44,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Truncate file to current position.
|
||||
pub fn truncate(&mut self) -> io::Result<()> {
|
||||
let offset = self.offset;
|
||||
@ -53,7 +53,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
if e.data.size().map_or(false, |s| offset == s) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
e.data.set_size(self.offset);
|
||||
if self.offset == 0 {
|
||||
e.data.set_first_cluster(None);
|
||||
@ -75,7 +75,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn abs_pos(&self) -> Option<u64> {
|
||||
// Returns current position relative to filesystem start
|
||||
// Note: when between clusters it returns position after previous cluster
|
||||
@ -89,7 +89,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn flush_dir_entry(&self) -> io::Result<()> {
|
||||
if self.entry_dirty {
|
||||
match self.entry {
|
||||
@ -99,7 +99,7 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
/// Set date and time of last modification for this file.
|
||||
///
|
||||
/// Note: this library doesn't know current time so changing timestamp must be done manually.
|
||||
@ -112,14 +112,14 @@ impl <'a, 'b> File<'a, 'b> {
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn bytes_left_in_file(&self) -> Option<usize> {
|
||||
match self.entry {
|
||||
Some(ref e) => e.data.size().map(|s| (s - self.offset) as usize),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn set_first_cluster(&mut self, cluster: u32) {
|
||||
self.first_cluster = Some(cluster);
|
||||
match self.entry {
|
||||
@ -241,7 +241,7 @@ impl<'a, 'b> Write for File<'a, 'b> {
|
||||
self.update_size();
|
||||
Ok(written_bytes)
|
||||
}
|
||||
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.flush_dir_entry()?;
|
||||
let mut disk = self.fs.disk.borrow_mut();
|
||||
|
48
src/fs.rs
48
src/fs.rs
@ -39,7 +39,7 @@ pub(crate) struct BiosParameterBlock {
|
||||
heads: u16,
|
||||
hidden_sectors: u32,
|
||||
total_sectors_32: u32,
|
||||
|
||||
|
||||
// Extended BIOS Parameter Block
|
||||
sectors_per_fat_32: u32,
|
||||
extended_flags: u16,
|
||||
@ -66,7 +66,7 @@ pub(crate) struct BootRecord {
|
||||
}
|
||||
|
||||
impl Default for BootRecord {
|
||||
fn default() -> BootRecord {
|
||||
fn default() -> BootRecord {
|
||||
BootRecord {
|
||||
bootjmp: Default::default(),
|
||||
oem_name: Default::default(),
|
||||
@ -98,7 +98,7 @@ impl <'a> FileSystem<'a> {
|
||||
if boot.boot_sig != [0x55, 0xAA] {
|
||||
return Err(Error::new(ErrorKind::Other, "invalid signature"));
|
||||
}
|
||||
|
||||
|
||||
let total_sectors = if boot.bpb.total_sectors_16 == 0 { boot.bpb.total_sectors_32 } else { boot.bpb.total_sectors_16 as u32 };
|
||||
let sectors_per_fat = if boot.bpb.sectors_per_fat_16 == 0 { boot.bpb.sectors_per_fat_32 } else { boot.bpb.sectors_per_fat_16 as u32 };
|
||||
let root_dir_sectors = (((boot.bpb.root_entries * 32) + (boot.bpb.bytes_per_sector - 1)) / boot.bpb.bytes_per_sector) as u32;
|
||||
@ -106,7 +106,7 @@ impl <'a> FileSystem<'a> {
|
||||
let data_sectors = total_sectors - (boot.bpb.reserved_sectors as u32 + (boot.bpb.fats as u32 * sectors_per_fat) + root_dir_sectors as u32);
|
||||
let total_clusters = data_sectors / boot.bpb.sectors_per_cluster as u32;
|
||||
let fat_type = Self::fat_type_from_clusters(total_clusters);
|
||||
|
||||
|
||||
Ok(FileSystem {
|
||||
disk: RefCell::new(disk),
|
||||
fat_type,
|
||||
@ -115,17 +115,17 @@ impl <'a> FileSystem<'a> {
|
||||
root_dir_sectors,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/// Returns type of used File Allocation Table (FAT).
|
||||
pub fn fat_type(&self) -> FatType {
|
||||
self.fat_type
|
||||
}
|
||||
|
||||
|
||||
/// Returns volume identifier read from BPB in Boot Sector.
|
||||
pub fn volume_id(&self) -> u32 {
|
||||
self.boot.bpb.volume_id
|
||||
}
|
||||
|
||||
|
||||
/// Returns volume label from BPB in Boot Sector.
|
||||
///
|
||||
/// Note: File with VOLUME_ID attribute in root directory is ignored by this library.
|
||||
@ -133,7 +133,7 @@ impl <'a> FileSystem<'a> {
|
||||
pub fn volume_label(&self) -> String {
|
||||
String::from_utf8_lossy(&self.boot.bpb.volume_label).trim_right().to_string()
|
||||
}
|
||||
|
||||
|
||||
/// Returns root directory object allowing futher penetration of filesystem structure.
|
||||
pub fn root_dir<'b>(&'b self) -> Dir<'b, 'a> {
|
||||
let root_rdr = {
|
||||
@ -145,7 +145,7 @@ impl <'a> FileSystem<'a> {
|
||||
};
|
||||
Dir::new(root_rdr, self)
|
||||
}
|
||||
|
||||
|
||||
fn read_bpb(rdr: &mut Read) -> io::Result<BiosParameterBlock> {
|
||||
let mut bpb: BiosParameterBlock = Default::default();
|
||||
bpb.bytes_per_sector = rdr.read_u16::<LittleEndian>()?;
|
||||
@ -160,7 +160,7 @@ impl <'a> FileSystem<'a> {
|
||||
bpb.heads = rdr.read_u16::<LittleEndian>()?;
|
||||
bpb.hidden_sectors = rdr.read_u32::<LittleEndian>()?;
|
||||
bpb.total_sectors_32 = rdr.read_u32::<LittleEndian>()?;
|
||||
|
||||
|
||||
// sanity checks
|
||||
if bpb.bytes_per_sector < 512 {
|
||||
return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB"));
|
||||
@ -174,7 +174,7 @@ impl <'a> FileSystem<'a> {
|
||||
if bpb.fats == 0 {
|
||||
return Err(Error::new(ErrorKind::Other, "invalid fats value in BPB"));
|
||||
}
|
||||
|
||||
|
||||
if bpb.sectors_per_fat_16 == 0 {
|
||||
bpb.sectors_per_fat_32 = rdr.read_u32::<LittleEndian>()?;
|
||||
bpb.extended_flags = rdr.read_u16::<LittleEndian>()?;
|
||||
@ -199,7 +199,7 @@ impl <'a> FileSystem<'a> {
|
||||
}
|
||||
Ok(bpb)
|
||||
}
|
||||
|
||||
|
||||
fn fat_type_from_clusters(total_clusters: u32) -> FatType {
|
||||
if total_clusters < 4085 {
|
||||
FatType::Fat12
|
||||
@ -209,13 +209,13 @@ impl <'a> FileSystem<'a> {
|
||||
FatType::Fat32
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_boot_record(rdr: &mut Read) -> io::Result<BootRecord> {
|
||||
let mut boot: BootRecord = Default::default();
|
||||
rdr.read_exact(&mut boot.bootjmp)?;
|
||||
rdr.read_exact(&mut boot.oem_name)?;
|
||||
boot.bpb = Self::read_bpb(rdr)?;
|
||||
|
||||
|
||||
if boot.bpb.sectors_per_fat_16 == 0 {
|
||||
rdr.read_exact(&mut boot.boot_code[0..420])?;
|
||||
} else {
|
||||
@ -224,23 +224,23 @@ impl <'a> FileSystem<'a> {
|
||||
rdr.read_exact(&mut boot.boot_sig)?;
|
||||
Ok(boot)
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn offset_from_sector(&self, sector: u32) -> u64 {
|
||||
(sector as u64) * self.boot.bpb.bytes_per_sector as u64
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn sector_from_cluster(&self, cluster: u32) -> u32 {
|
||||
((cluster - 2) * self.boot.bpb.sectors_per_cluster as u32) + self.first_data_sector
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn get_cluster_size(&self) -> u32 {
|
||||
self.boot.bpb.sectors_per_cluster as u32 * self.boot.bpb.bytes_per_sector as u32
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
|
||||
self.offset_from_sector(self.sector_from_cluster(cluser))
|
||||
}
|
||||
|
||||
|
||||
fn fat_slice<'b>(&'b self) -> DiskSlice<'b, 'a> {
|
||||
let sectors_per_fat =
|
||||
if self.boot.bpb.sectors_per_fat_16 == 0 { self.boot.bpb.sectors_per_fat_32 }
|
||||
@ -255,12 +255,12 @@ impl <'a> FileSystem<'a> {
|
||||
};
|
||||
DiskSlice::from_sectors(fat_first_sector, sectors_per_fat, mirrors, self)
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn cluster_iter<'b>(&'b self, cluster: u32) -> ClusterIterator<'b, 'a> {
|
||||
let disk_slice = self.fat_slice();
|
||||
ClusterIterator::new(disk_slice, self.fat_type, cluster)
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn alloc_cluster(&self, prev_cluster: Option<u32>) -> io::Result<u32> {
|
||||
let mut disk_slice = self.fat_slice();
|
||||
alloc_cluster(&mut disk_slice, self.fat_type, prev_cluster)
|
||||
@ -280,12 +280,12 @@ impl <'a, 'b> DiskSlice<'a, 'b> {
|
||||
pub(crate) fn new(begin: u64, size: u64, mirrors: u8, fs: FileSystemRef<'a, 'b>) -> Self {
|
||||
DiskSlice { begin, size, mirrors, fs, offset: 0 }
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn from_sectors(first_sector: u32, sector_count: u32, mirrors: u8, fs: FileSystemRef<'a, 'b>) -> Self {
|
||||
let bytes_per_sector = fs.boot.bpb.bytes_per_sector as u64;
|
||||
Self::new(first_sector as u64 * bytes_per_sector, sector_count as u64 * bytes_per_sector, mirrors, fs)
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn abs_pos(&self) -> u64 {
|
||||
self.begin + self.offset
|
||||
}
|
||||
@ -315,7 +315,7 @@ impl <'a, 'b> Write for DiskSlice<'a, 'b> {
|
||||
self.offset += write_size as u64;
|
||||
Ok(write_size)
|
||||
}
|
||||
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
let mut disk = self.fs.disk.borrow_mut();
|
||||
disk.flush()
|
||||
|
14
src/table.rs
14
src/table.rs
@ -85,7 +85,7 @@ impl FatTrait for Fat12 {
|
||||
n => FatValue::Data(n as u32),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn set(fat: &mut DiskSlice, cluster: u32, value: FatValue) -> io::Result<()> {
|
||||
let raw_val = match value {
|
||||
FatValue::Free => 0,
|
||||
@ -141,7 +141,7 @@ impl FatTrait for Fat16 {
|
||||
n => FatValue::Data(n as u32),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn set(fat: &mut DiskSlice, cluster: u32, value: FatValue) -> io::Result<()> {
|
||||
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
|
||||
let raw_val = match value {
|
||||
@ -153,7 +153,7 @@ impl FatTrait for Fat16 {
|
||||
fat.write_u16::<LittleEndian>(raw_val)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn find_free(fat: &mut ReadSeek, hint_cluster: u32) -> io::Result<u32> {
|
||||
let mut cluster = hint_cluster;
|
||||
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
|
||||
@ -178,7 +178,7 @@ impl FatTrait for Fat32 {
|
||||
n => FatValue::Data(n as u32),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn set(fat: &mut DiskSlice, cluster: u32, value: FatValue) -> io::Result<()> {
|
||||
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
|
||||
let raw_val = match value {
|
||||
@ -190,7 +190,7 @@ impl FatTrait for Fat32 {
|
||||
fat.write_u32::<LittleEndian>(raw_val)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn find_free(fat: &mut ReadSeek, hint_cluster: u32) -> io::Result<u32> {
|
||||
let mut cluster = hint_cluster;
|
||||
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
|
||||
@ -221,7 +221,7 @@ impl <'a, 'b> ClusterIterator<'a, 'b> {
|
||||
err: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn truncate(&mut self) -> io::Result<()> {
|
||||
match self.cluster {
|
||||
Some(n) => {
|
||||
@ -232,7 +232,7 @@ impl <'a, 'b> ClusterIterator<'a, 'b> {
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn free(&mut self) -> io::Result<()> {
|
||||
loop {
|
||||
let prev = self.cluster;
|
||||
|
10
src/utils.rs
10
src/utils.rs
@ -32,7 +32,7 @@ impl<T: Read+Write+Seek> BufStream<T> {
|
||||
write: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn flush_buf(&mut self) -> io::Result<()> {
|
||||
if self.write {
|
||||
self.inner.write_all(&self.buf[..self.pos])?;
|
||||
@ -40,7 +40,7 @@ impl<T: Read+Write+Seek> BufStream<T> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn make_reader(&mut self) -> io::Result<()> {
|
||||
if self.write {
|
||||
self.flush_buf()?;
|
||||
@ -50,7 +50,7 @@ impl<T: Read+Write+Seek> BufStream<T> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn make_writter(&mut self) -> io::Result<()> {
|
||||
if !self.write {
|
||||
self.inner.seek(io::SeekFrom::Current(-(self.len as i64 - self.pos as i64)))?;
|
||||
@ -71,7 +71,7 @@ impl<T: Read+Write+Seek> BufRead for BufStream<T> {
|
||||
}
|
||||
Ok(&self.buf[self.pos..self.len])
|
||||
}
|
||||
|
||||
|
||||
fn consume(&mut self, amt: usize) {
|
||||
self.pos = cmp::min(self.pos + amt, self.len);
|
||||
}
|
||||
@ -108,7 +108,7 @@ impl<T: Read+Write+Seek> Write for BufStream<T> {
|
||||
self.pos += written;
|
||||
Ok(written)
|
||||
}
|
||||
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.flush_buf()?;
|
||||
self.inner.flush()
|
||||
|
@ -54,12 +54,12 @@ fn test_read_seek_short_file(fs: FileSystem) {
|
||||
let mut buf = Vec::new();
|
||||
short_file.read_to_end(&mut buf).unwrap();
|
||||
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT);
|
||||
|
||||
|
||||
assert_eq!(short_file.seek(SeekFrom::Start(5)).unwrap(), 5);
|
||||
let mut buf2 = [0; 5];
|
||||
short_file.read_exact(&mut buf2).unwrap();
|
||||
assert_eq!(str::from_utf8(&buf2).unwrap(), &TEST_TEXT[5..10]);
|
||||
|
||||
|
||||
assert_eq!(short_file.seek(SeekFrom::Start(1000)).unwrap(), TEST_TEXT.len() as u64);
|
||||
let mut buf2 = [0; 5];
|
||||
assert_eq!(short_file.read(&mut buf2).unwrap(), 0);
|
||||
@ -86,7 +86,7 @@ fn test_read_long_file(fs: FileSystem) {
|
||||
let mut buf = Vec::new();
|
||||
long_file.read_to_end(&mut buf).unwrap();
|
||||
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT.repeat(1000));
|
||||
|
||||
|
||||
assert_eq!(long_file.seek(SeekFrom::Start(2017)).unwrap(), 2017);
|
||||
buf.clear();
|
||||
let mut buf2 = [0; 10];
|
||||
@ -114,11 +114,11 @@ fn test_get_dir_by_path(fs: FileSystem) {
|
||||
let dir = root_dir.open_dir("very/long/path/").unwrap();
|
||||
let names = dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
assert_eq!(names, [".", "..", "test.txt"]);
|
||||
|
||||
|
||||
let dir2 = root_dir.open_dir("very/long/path/././.").unwrap();
|
||||
let names2 = dir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
assert_eq!(names2, [".", "..", "test.txt"]);
|
||||
|
||||
|
||||
let root_dir2 = root_dir.open_dir("very/long/path/../../..").unwrap();
|
||||
let root_names = root_dir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
let root_names2 = root_dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
@ -146,7 +146,7 @@ fn test_get_file_by_path(fs: FileSystem) {
|
||||
let mut buf = Vec::new();
|
||||
file.read_to_end(&mut buf).unwrap();
|
||||
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT);
|
||||
|
||||
|
||||
// let mut file = root_dir.open_file("very-long-dir-name/very-long-file-name.txt").unwrap();
|
||||
// let mut buf = Vec::new();
|
||||
// file.read_to_end(&mut buf).unwrap();
|
||||
|
@ -100,7 +100,7 @@ fn test_remove(fs: FileSystem) {
|
||||
names = dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
assert_eq!(names, [".", ".."]);
|
||||
assert!(root_dir.remove("very/long/path").is_ok());
|
||||
|
||||
|
||||
names = root_dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
||||
assert_eq!(names, ["long.txt", "short.txt", "very", "very-long-dir-name"]);
|
||||
root_dir.remove("long.txt").unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user