diff --git a/src/services/fs.rs b/src/services/fs.rs index ddca72c..c19e568 100644 --- a/src/services/fs.rs +++ b/src/services/fs.rs @@ -56,6 +56,7 @@ pub struct File { offset: u64, } +#[derive(Clone)] pub struct OpenOptions { read: bool, write: bool, @@ -76,7 +77,7 @@ impl Fs { } pub fn sdmc(&self) -> Result { - let mut handle = 0u64; + let mut handle = 0; unsafe { let id = ArchiveID::Sdmc; let path = fsMakePath(PathType::Empty.into(), ptr::null() as _); @@ -94,25 +95,20 @@ impl Fs { } impl Archive { - pub fn file_open(&self, path: &Path) -> Result { - self.file_open_options().read(true).create(true).open(path) - } - - pub fn file_open_options(&self) -> OpenOptions { - OpenOptions { - read: false, - write: false, - create: false, - arch_handle: self.handle, - } - } - pub fn get_id(&self) -> ArchiveID { self.id } } impl File { + pub fn open>(arch: &Archive, path: P) -> Result { + OpenOptions::new().read(true).archive(arch).open(path) + } + + pub fn create>(arch: &Archive, path: P) -> Result { + OpenOptions::new().write(true).create(true).archive(arch).open(path) + } + pub fn len(&self) -> Result { unsafe { let mut len = 0; @@ -125,6 +121,17 @@ impl File { } } + pub fn set_len(&mut self, len: u64) -> Result<(), i32> { + unsafe { + let r = FSFILE_SetSize(self.handle, len); + if r < 0 { + Err(r) + } else { + Ok(()) + } + } + } + pub fn read(&mut self, buf: &mut [u8]) -> Result { unsafe { let mut n_read = 0; @@ -166,6 +173,15 @@ impl File { } impl OpenOptions { + pub fn new() -> OpenOptions { + OpenOptions { + read: false, + write: false, + create: false, + arch_handle: 0, + } + } + pub fn read(&mut self, read: bool) -> &mut OpenOptions { self.read = read; self @@ -181,6 +197,11 @@ impl OpenOptions { self } + pub fn archive(&mut self, archive: &Archive) -> &mut OpenOptions { + self.arch_handle = archive.handle; + self + } + pub fn open>(&self, path: P) -> Result { self._open(path.as_ref(), self.get_open_flags()) } @@ -223,7 +244,6 @@ impl Drop for Fs { } } - impl Drop for Archive { fn drop(&mut self) { unsafe { @@ -254,20 +274,6 @@ impl From for FS_PathType { } } -impl From for PathType { - fn from(f: FS_PathType) -> Self { - use self::PathType::*; - use libctru::services::fs::FS_PathType::*; - match f { - PATH_INVALID => Invalid, - PATH_EMPTY => Empty, - PATH_BINARY => Binary, - PATH_ASCII => ASCII, - PATH_UTF16 => UTF16, - } - } -} - impl From for FS_ArchiveID { fn from(a: ArchiveID) -> Self { use self::ArchiveID::*;