Last active
August 29, 2018 16:21
-
-
Save sijad/12f92b0f7c2ce69832f0c4c3dd58769b to your computer and use it in GitHub Desktop.
rust readonly filesystem based on filesystem-rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate filesystem; | |
use std::fs::{self}; | |
use std::io::{Result, Error, ErrorKind}; | |
use std::path::{Path, PathBuf}; | |
use filesystem::{OsFileSystem, FileSystem}; | |
#[derive(Clone, Debug, Default)] | |
pub struct OsSafeFileSystem { | |
fs: OsFileSystem, | |
read_only: bool, | |
} | |
impl OsSafeFileSystem { | |
pub fn new(read_only: bool) -> Self { | |
OsSafeFileSystem { | |
fs: OsFileSystem{}, | |
read_only, | |
} | |
} | |
} | |
impl FileSystem for OsSafeFileSystem { | |
type DirEntry = fs::DirEntry; | |
type ReadDir = fs::ReadDir; | |
fn current_dir(&self) -> Result<PathBuf> { | |
self.fs.current_dir() | |
} | |
fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
self.fs.set_current_dir(path) | |
} | |
fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool { | |
self.fs.is_dir(path) | |
} | |
fn is_file<P: AsRef<Path>>(&self, path: P) -> bool { | |
self.fs.is_file(path) | |
} | |
fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.create_dir(path) | |
} | |
fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.create_dir_all(path) | |
} | |
fn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.remove_dir(path) | |
} | |
fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.remove_dir_all(path) | |
} | |
fn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<Self::ReadDir> { | |
self.fs.read_dir(path) | |
} | |
fn write_file<P, B>(&self, path: P, buf: B) -> Result<()> | |
where | |
P: AsRef<Path>, | |
B: AsRef<[u8]>, | |
{ | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.write_file(path, buf) | |
} | |
fn overwrite_file<P, B>(&self, path: P, buf: B) -> Result<()> | |
where | |
P: AsRef<Path>, | |
B: AsRef<[u8]>, | |
{ | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.overwrite_file(path, buf) | |
} | |
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>> { | |
self.fs.read_file(path) | |
} | |
fn read_file_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String> { | |
self.fs.read_file_to_string(path) | |
} | |
fn create_file<P, B>(&self, path: P, buf: B) -> Result<()> | |
where | |
P: AsRef<Path>, | |
B: AsRef<[u8]>, | |
{ | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.create_file(path, buf) | |
} | |
fn remove_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.remove_file(path) | |
} | |
fn copy_file<P, Q>(&self, from: P, to: Q) -> Result<()> | |
where | |
P: AsRef<Path>, | |
Q: AsRef<Path>, | |
{ | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.copy_file(from, to) | |
} | |
fn rename<P, Q>(&self, from: P, to: Q) -> Result<()> | |
where | |
P: AsRef<Path>, | |
Q: AsRef<Path>, | |
{ | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.rename(from, to) | |
} | |
fn readonly<P: AsRef<Path>>(&self, path: P) -> Result<bool> { | |
self.fs.readonly(path) | |
} | |
fn set_readonly<P: AsRef<Path>>(&self, path: P, readonly: bool) -> Result<()> { | |
if self.read_only { | |
return permission_denied() | |
} | |
self.fs.set_readonly(path, readonly) | |
} | |
fn len<P: AsRef<Path>>(&self, path: P) -> u64 { | |
self.fs.len(path) | |
} | |
} | |
fn permission_denied() -> Result<()> { | |
Err(Error::from(ErrorKind::PermissionDenied)) | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn perm_denied() { | |
assert_eq!(permission_denied().is_err(), true); | |
assert_eq!(permission_denied().unwrap_err().kind(), ErrorKind::PermissionDenied); | |
} | |
#[test] | |
fn run_time_change() { | |
let mut osf = OsSafeFileSystem::new(true); | |
assert_eq!(osf.create_dir("test").unwrap_err().kind(), ErrorKind::PermissionDenied); | |
osf.read_only = false; | |
assert_eq!(osf.create_dir("test").is_ok(), true) | |
} | |
} | |
fn main() { | |
let mut osf = OsSafeFileSystem::new(true); | |
osf.read_only = false; | |
let _ = osf.create_dir("test_dir"); | |
let res = osf.remove_dir("test_dir"); | |
println!("{}", res.unwrap_err()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment