2017-09-23 06:22:31 +08:00
|
|
|
use std::cmp;
|
2017-09-23 04:27:39 +08:00
|
|
|
use std::io::prelude::*;
|
2017-09-24 08:10:59 +08:00
|
|
|
use std::io::{SeekFrom, ErrorKind};
|
2017-09-23 04:27:39 +08:00
|
|
|
use std::io;
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
use fs::FileSystemRef;
|
2017-10-09 20:59:52 +08:00
|
|
|
use dir::{FileEntryInfo, DateTime};
|
2017-09-23 04:27:39 +08:00
|
|
|
|
2017-10-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct File<'a, 'b: 'a> {
|
2017-10-07 08:37:29 +08:00
|
|
|
first_cluster: Option<u32>,
|
2017-09-24 02:16:32 +08:00
|
|
|
current_cluster: Option<u32>,
|
2017-10-09 20:59:52 +08:00
|
|
|
offset: u32,
|
|
|
|
entry: Option<FileEntryInfo>,
|
|
|
|
entry_dirty: bool,
|
2017-10-07 20:56:50 +08:00
|
|
|
fs: FileSystemRef<'a, 'b>,
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> File<'a, 'b> {
|
2017-10-09 20:59:52 +08:00
|
|
|
pub(crate) fn new(first_cluster: Option<u32>, entry: Option<FileEntryInfo>, fs: FileSystemRef<'a, 'b>) -> Self {
|
2017-10-07 20:56:50 +08:00
|
|
|
File {
|
2017-10-09 20:59:52 +08:00
|
|
|
first_cluster, entry, fs,
|
2017-10-07 08:37:29 +08:00
|
|
|
current_cluster: first_cluster,
|
2017-09-24 02:16:32 +08:00
|
|
|
offset: 0,
|
2017-10-09 20:59:52 +08:00
|
|
|
entry_dirty: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_size(&mut self) {
|
|
|
|
match self.entry {
|
|
|
|
Some(ref mut e) => {
|
|
|
|
if self.offset > e.data.size() {
|
|
|
|
e.data.size = self.offset;
|
|
|
|
self.entry_dirty = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn truncate(&mut self) {
|
|
|
|
// FIXME: free clusters?
|
|
|
|
match self.entry {
|
|
|
|
Some(ref mut e) => {
|
|
|
|
if e.data.size != self.offset {
|
|
|
|
e.data.size = self.offset;
|
|
|
|
self.entry_dirty = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn global_pos(&self) -> Option<u64> {
|
|
|
|
match self.current_cluster {
|
|
|
|
Some(n) => {
|
|
|
|
let cluster_size = self.fs.get_cluster_size();
|
|
|
|
let offset_in_cluster = self.offset % cluster_size;
|
|
|
|
let offset_in_fs = self.fs.offset_from_cluster(n) + (offset_in_cluster as u64);
|
|
|
|
Some(offset_in_fs)
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn flush_dir_entry(&self) -> io::Result<()> {
|
|
|
|
if self.entry_dirty {
|
|
|
|
self.entry.iter().next().unwrap().write(self.fs)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_modified(&mut self, date_time: DateTime) {
|
|
|
|
match self.entry {
|
|
|
|
Some(ref mut e) => {
|
|
|
|
e.data.set_modified(date_time);
|
|
|
|
self.entry_dirty = true;
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_left_in_file(&self) -> Option<usize> {
|
|
|
|
match self.entry {
|
|
|
|
Some(ref e) => {
|
|
|
|
if e.data.is_file() {
|
|
|
|
Some((e.data.size - self.offset) as usize)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None,
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
impl<'a, 'b> Drop for File<'a, 'b> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.flush().expect("flush failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Read for File<'a, 'b> {
|
2017-09-24 01:42:09 +08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2017-09-24 02:16:32 +08:00
|
|
|
let mut buf_offset: usize = 0;
|
2017-10-04 19:56:44 +08:00
|
|
|
let cluster_size = self.fs.get_cluster_size();
|
2017-09-24 02:16:32 +08:00
|
|
|
loop {
|
2017-10-07 22:07:33 +08:00
|
|
|
let current_cluster = match self.current_cluster {
|
|
|
|
Some(n) => n,
|
|
|
|
None => break,
|
|
|
|
};
|
2017-09-24 02:16:32 +08:00
|
|
|
let offset_in_cluster = self.offset % cluster_size;
|
|
|
|
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
|
2017-10-09 20:59:52 +08:00
|
|
|
let bytes_left_in_file = self.bytes_left_in_file().unwrap_or(bytes_left_in_cluster);
|
2017-09-24 02:16:32 +08:00
|
|
|
let bytes_left_in_buf = buf.len() - buf_offset;
|
2017-09-24 03:53:32 +08:00
|
|
|
let read_size = cmp::min(cmp::min(bytes_left_in_buf, bytes_left_in_cluster), bytes_left_in_file);
|
2017-10-07 22:07:33 +08:00
|
|
|
if read_size == 0 {
|
2017-09-24 02:16:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-10-04 19:56:44 +08:00
|
|
|
let offset_in_fs = self.fs.offset_from_cluster(current_cluster) + (offset_in_cluster as u64);
|
|
|
|
let read_bytes = {
|
2017-10-09 20:59:52 +08:00
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.seek(SeekFrom::Start(offset_in_fs))?;
|
|
|
|
disk.read(&mut buf[buf_offset..buf_offset+read_size])?
|
2017-10-04 19:56:44 +08:00
|
|
|
};
|
2017-09-24 02:16:32 +08:00
|
|
|
if read_bytes == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.offset += read_bytes as u32;
|
|
|
|
buf_offset += read_bytes;
|
|
|
|
if self.offset % cluster_size == 0 {
|
2017-10-07 08:37:29 +08:00
|
|
|
let r = self.fs.cluster_iter(current_cluster).skip(1).next();
|
|
|
|
self.current_cluster = match r {
|
2017-10-07 08:19:19 +08:00
|
|
|
Some(Err(err)) => return Err(err),
|
2017-10-07 08:37:29 +08:00
|
|
|
Some(Ok(n)) => Some(n),
|
|
|
|
None => None,
|
|
|
|
};
|
2017-09-24 02:16:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(buf_offset)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 08:10:59 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
impl<'a, 'b> Write for File<'a, 'b> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
let mut buf_offset: usize = 0;
|
|
|
|
let cluster_size = self.fs.get_cluster_size();
|
|
|
|
loop {
|
|
|
|
let current_cluster = match self.current_cluster {
|
|
|
|
Some(n) => n,
|
|
|
|
None => unimplemented!(), // FIXME: allocate cluster
|
|
|
|
};
|
|
|
|
let offset_in_cluster = self.offset % cluster_size;
|
|
|
|
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
|
|
|
|
let bytes_left_in_buf = buf.len() - buf_offset;
|
|
|
|
let write_size = cmp::min(bytes_left_in_buf, bytes_left_in_cluster);
|
|
|
|
if write_size == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let offset_in_fs = self.fs.offset_from_cluster(current_cluster) + (offset_in_cluster as u64);
|
|
|
|
let written_bytes = {
|
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.seek(SeekFrom::Start(offset_in_fs))?;
|
|
|
|
disk.write(&buf[buf_offset..buf_offset+write_size])?
|
|
|
|
};
|
|
|
|
if written_bytes == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.offset += written_bytes as u32;
|
|
|
|
buf_offset += written_bytes;
|
|
|
|
if self.offset % cluster_size == 0 {
|
|
|
|
let r = self.fs.cluster_iter(current_cluster).skip(1).next();
|
|
|
|
self.current_cluster = match r {
|
|
|
|
Some(Err(err)) => return Err(err),
|
|
|
|
Some(Ok(n)) => Some(n),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.update_size();
|
|
|
|
Ok(buf_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.flush_dir_entry()?;
|
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Seek for File<'a, 'b> {
|
2017-09-24 08:10:59 +08:00
|
|
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
let new_offset = match pos {
|
|
|
|
SeekFrom::Current(x) => self.offset as i64 + x,
|
|
|
|
SeekFrom::Start(x) => x as i64,
|
2017-10-09 20:59:52 +08:00
|
|
|
SeekFrom::End(x) => self.entry.iter().next().expect("cannot seek from end if size is unknown").data.size() as i64 + x,
|
2017-09-24 08:10:59 +08:00
|
|
|
};
|
2017-10-07 22:07:33 +08:00
|
|
|
if new_offset < 0 {
|
2017-09-24 08:10:59 +08:00
|
|
|
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek"));
|
|
|
|
}
|
2017-10-04 19:56:44 +08:00
|
|
|
let cluster_size = self.fs.get_cluster_size();
|
2017-09-24 08:10:59 +08:00
|
|
|
let cluster_count = (new_offset / cluster_size as i64) as usize;
|
2017-10-07 08:37:29 +08:00
|
|
|
let new_cluster = if cluster_count > 0 {
|
2017-10-07 22:07:33 +08:00
|
|
|
match self.first_cluster {
|
|
|
|
Some(n) => {
|
|
|
|
match self.fs.cluster_iter(n).skip(cluster_count).next() {
|
|
|
|
Some(Err(err)) => return Err(err),
|
|
|
|
Some(Ok(n)) => Some(n),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
},
|
2017-10-07 08:37:29 +08:00
|
|
|
None => None,
|
2017-10-07 08:19:19 +08:00
|
|
|
}
|
2017-10-07 08:37:29 +08:00
|
|
|
} else {
|
|
|
|
self.first_cluster
|
|
|
|
};
|
2017-09-24 08:10:59 +08:00
|
|
|
self.offset = new_offset as u32;
|
|
|
|
self.current_cluster = new_cluster;
|
|
|
|
Ok(self.offset as u64)
|
|
|
|
}
|
|
|
|
}
|