pub trait VirtualFileSystem:
Debug
+ Send
+ Sync
+ 'static {
Show 52 methods
// Required methods
fn abs<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn all_dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn all_files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn all_paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn append<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
where T: AsRef<Path>;
fn append_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<[u8]>;
fn append_line<T, U>(&self, path: T, line: U) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<str>;
fn append_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<str>;
fn chmod<T>(&self, path: T, mode: u32) -> Result<(), RvError>
where T: AsRef<Path>;
fn chmod_b<T>(&self, path: T) -> Result<Chmod, RvError>
where T: AsRef<Path>;
fn chown<T>(&self, path: T, uid: u32, gid: u32) -> Result<(), RvError>
where T: AsRef<Path>;
fn chown_b<T>(&self, path: T) -> Result<Chown, RvError>
where T: AsRef<Path>;
fn config_dir<T>(&self, config: T) -> Option<PathBuf>
where T: AsRef<str>;
fn copy<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<Path>;
fn copy_b<T, U>(&self, src: T, dst: U) -> Result<Copier, RvError>
where T: AsRef<Path>,
U: AsRef<Path>;
fn cwd(&self) -> Result<PathBuf, RvError>;
fn dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn entries<T>(&self, path: T) -> Result<Entries, RvError>
where T: AsRef<Path>;
fn entry<T>(&self, path: T) -> Result<VfsEntry, RvError>
where T: AsRef<Path>;
fn exists<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn gid<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>;
fn is_exec<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_dir<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_file<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_readonly<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_symlink<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_symlink_dir<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn is_symlink_file<T>(&self, path: T) -> bool
where T: AsRef<Path>;
fn mkdir_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn mkdir_p<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn mkfile<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn mkfile_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn mode<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>;
fn move_p<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<Path>;
fn owner<T>(&self, path: T) -> Result<(u32, u32), RvError>
where T: AsRef<Path>;
fn paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>;
fn read<T>(&self, path: T) -> Result<Box<dyn ReadSeek>, RvError>
where T: AsRef<Path>;
fn read_all<T>(&self, path: T) -> Result<String, RvError>
where T: AsRef<Path>;
fn read_lines<T>(&self, path: T) -> Result<Vec<String>, RvError>
where T: AsRef<Path>;
fn readlink<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn readlink_abs<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn remove<T>(&self, path: T) -> Result<(), RvError>
where T: AsRef<Path>;
fn remove_all<T>(&self, path: T) -> Result<(), RvError>
where T: AsRef<Path>;
fn root(&self) -> PathBuf;
fn set_cwd<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>;
fn symlink<T, U>(&self, link: T, target: U) -> Result<PathBuf, RvError>
where T: AsRef<Path>,
U: AsRef<Path>;
fn uid<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>;
fn upcast(self) -> Vfs;
fn write<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
where T: AsRef<Path>;
fn write_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<[u8]>;
fn write_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
where T: AsRef<Path>,
U: AsRef<str>;
}Expand description
Defines a virtual file system that can be implemented by various backed providers
Required Methods§
Sourcefn abs<T>(&self, path: T) -> Result<PathBuf, RvError>
fn abs<T>(&self, path: T) -> Result<PathBuf, RvError>
Return the path in an absolute clean form
- Environment variable expansion
- Relative path resolution for
.and.. - No IO resolution so it will work even with paths that don’t exist
§Errors
- PathError::ParentNotFound(PathBuf) when parent is not found
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let home = sys::home_dir().unwrap();
assert_eq!(vfs.abs("~").unwrap(), PathBuf::from(&home));Sourcefn all_dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn all_dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all dirs for the given path recursively
- Results are sorted by filename, are distict and don’t include the given path
- Handles path expansion and absolute path resolution
- Paths are returned in absolute form
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let dir1 = tmpdir.mash("dir1");
let dir2 = dir1.mash("dir2");
assert_vfs_mkdir_p!(vfs, &dir2);
assert_iter_eq(vfs.all_dirs(&tmpdir).unwrap(), vec![dir1, dir2]);Sourcefn all_files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn all_files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all files for the given path recursively
- Results are sorted by filename, are distict and don’t include the given path
- Handles path expansion and absolute path resolution
- Paths are returned in absolute form
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let file1 = tmpdir.mash("file1");
let dir1 = tmpdir.mash("dir1");
let file2 = dir1.mash("file2");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_mkfile!(vfs, &file2);
assert_iter_eq(vfs.all_files(&tmpdir).unwrap(), vec![file2, file1]);Sourcefn all_paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn all_paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all paths for the given path recursively
- Results are sorted by filename, are distict and don’t include the given path
- Handles path expansion and absolute path resolution
- Paths are returned in absolute form
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let dir1 = tmpdir.mash("dir1");
let file1 = tmpdir.mash("file1");
let file2 = dir1.mash("file2");
let file3 = dir1.mash("file3");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_mkfile!(vfs, &file2);
assert_vfs_mkfile!(vfs, &file3);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![dir1, file2, file3, file1]);Sourcefn append<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
fn append<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
Opens a file in append mode
- Handles path expansion and absolute path resolution
- Creates a file if it does not exist or appends to it if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
let mut f = vfs.write(&file).unwrap();
f.write_all(b"foobar").unwrap();
f.flush().unwrap();
let mut f = vfs.append(&file).unwrap();
f.write_all(b"123").unwrap();
f.flush().unwrap();
assert_vfs_read_all!(vfs, &file, "foobar123".to_string());Sourcefn append_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
fn append_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
Append the given data to to the target file
- Handles path expansion and absolute path resolution
- Creates a file if it does not exist or appends to it if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert_vfs_write_all!(vfs, &file, "foobar 1");
assert!(vfs.append_all(&file, "foobar 2").is_ok());
assert_vfs_is_file!(vfs, &file);
assert_vfs_read_all!(vfs, &file, "foobar 1foobar 2");Sourcefn append_line<T, U>(&self, path: T, line: U) -> Result<(), RvError>
fn append_line<T, U>(&self, path: T, line: U) -> Result<(), RvError>
Append the given line to to the target file including a newline
- Handles path expansion and absolute path resolution
- Creates a file if it does not exist or appends to it if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert_vfs_write_all!(vfs, &file, "foobar 1");
assert!(vfs.append_line(&file, "foobar 2").is_ok());
assert_vfs_is_file!(vfs, &file);
assert_vfs_read_all!(vfs, &file, "foobar 1foobar 2\n");Sourcefn append_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
fn append_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
Append the given lines to to the target file including newlines
- Handles path expansion and absolute path resolution
- Creates a file if it does not exist or appends to it if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert!(vfs.append_lines(&file, &["1", "2"]).is_ok());
assert_vfs_is_file!(vfs, &file);
assert_vfs_read_all!(vfs, &file, "1\n2\n");Sourcefn chmod<T>(&self, path: T, mode: u32) -> Result<(), RvError>
fn chmod<T>(&self, path: T, mode: u32) -> Result<(), RvError>
Change all file/dir permissions recursivly to mode
- Handles path expansion and absolute path resolution
- Doesn’t follow links by default, use the builder
chomd_bfor this option
§Errors
- PathError::Empty when the given path is empty
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_mkfile!(vfs, &file);
assert_eq!(vfs.mode(&file).unwrap(), 0o100644);
assert!(vfs.chmod(&file, 0o555).is_ok());
assert_eq!(vfs.mode(&file).unwrap(), 0o100555);Sourcefn chmod_b<T>(&self, path: T) -> Result<Chmod, RvError>
fn chmod_b<T>(&self, path: T) -> Result<Chmod, RvError>
Returns a new Chmod builder for advanced chmod options
- Handles path expansion and absolute path resolution
- Provides options for recursion, following links, narrowing in on file types etc…
§Errors
- PathError::Empty when the given path is empty
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = dir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert_eq!(vfs.mode(&dir).unwrap(), 0o40755);
assert_eq!(vfs.mode(&file).unwrap(), 0o100644);
assert!(vfs.chmod_b(&dir).unwrap().recurse().all(0o777).exec().is_ok());
assert_eq!(vfs.mode(&dir).unwrap(), 0o40777);
assert_eq!(vfs.mode(&file).unwrap(), 0o100777);Sourcefn chown<T>(&self, path: T, uid: u32, gid: u32) -> Result<(), RvError>
fn chown<T>(&self, path: T, uid: u32, gid: u32) -> Result<(), RvError>
Change the ownership of the path recursivly
- Handles path expansion and absolute path resolution
- Use
chown_bfor more options
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file1 = vfs.root().mash("file1");
assert_vfs_mkfile!(vfs, &file1);
assert!(vfs.chown(&file1, 5, 7).is_ok());
assert_eq!(vfs.owner(&file1).unwrap(), (5, 7));Sourcefn chown_b<T>(&self, path: T) -> Result<Chown, RvError>
fn chown_b<T>(&self, path: T) -> Result<Chown, RvError>
Creates new Chown for use with the builder pattern
- Handles path expansion and absolute path resolution
- Provides options for recursion, following links, narrowing in on file types etc…
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file1 = vfs.root().mash("file1");
assert_vfs_mkfile!(vfs, &file1);
assert!(vfs.chown_b(&file1).unwrap().owner(5, 7).exec().is_ok());
assert_eq!(vfs.owner(&file1).unwrap(), (5, 7));Sourcefn config_dir<T>(&self, config: T) -> Option<PathBuf>
fn config_dir<T>(&self, config: T) -> Option<PathBuf>
Returns the highest priority active configuration directory.
- Searches first the $XDG_CONFIG_HOME directory, then the $XDG_CONFIG_DIRS directories.
- Returns the first directory that contains the given configuration file.
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs(); // replace this with Vfs::stdfs() for the real filesystem
let dir = PathBuf::from("/etc/xdg");
vfs.mkdir_p(&dir).unwrap();
let filepath = dir.mash("rivia.toml");
vfs.write_all(&filepath, "this is a test").unwrap();
assert_eq!(vfs.config_dir("rivia.toml").unwrap().to_str().unwrap(), "/etc/xdg");
if let Some(config_dir) = vfs.config_dir("rivia.toml") {
let path = config_dir.mash("rivia.toml");
let config = vfs.read_all(&path).unwrap();
assert_eq!(config, "this is a test");
}Sourcefn copy<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
fn copy<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
Copies src to dst recursively
dstwill be copied into if it is an existing directorydstwill be a copy of the src if it doesn’t exist- Creates destination directories as needed
- Handles environment variable expansion
- Handles relative path resolution for
.and.. - Doesn’t follow links
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file1 = vfs.root().mash("file1");
let file2 = vfs.root().mash("file2");
assert_vfs_write_all!(vfs, &file1, "this is a test");
assert!(vfs.copy(&file1, &file2).is_ok());
assert_vfs_read_all!(vfs, &file2, "this is a test");Sourcefn copy_b<T, U>(&self, src: T, dst: U) -> Result<Copier, RvError>
fn copy_b<T, U>(&self, src: T, dst: U) -> Result<Copier, RvError>
Creates a new Copier for use with the builder pattern
dstwill be copied into if it is an existing directorydstwill be a copy of the src if it doesn’t exist- Handles environment variable expansion
- Handles relative path resolution for
.and.. - Options for recursion, mode setting and following links
- Execute by calling
exec
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file1 = vfs.root().mash("file1");
let file2 = vfs.root().mash("file2");
assert_vfs_write_all!(vfs, &file1, "this is a test");
assert!(vfs.copy_b(&file1, &file2).unwrap().exec().is_ok());
assert_vfs_read_all!(vfs, &file2, "this is a test");Sourcefn cwd(&self) -> Result<PathBuf, RvError>
fn cwd(&self) -> Result<PathBuf, RvError>
Returns the current working directory
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
assert_eq!(vfs.cwd().unwrap(), vfs.root());
assert_eq!(&vfs.mkdir_p(&dir).unwrap(), &dir);
assert_eq!(&vfs.set_cwd(&dir).unwrap(), &dir);
assert_eq!(&vfs.cwd().unwrap(), &dir);Sourcefn dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all directories for the given path, sorted by name
- Handles path expansion and absolute path resolution
- Paths are returned as abs paths
- Doesn’t include the path itself only its children nor is this recursive
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let dir1 = tmpdir.mash("dir1");
let dir2 = tmpdir.mash("dir2");
let file1 = tmpdir.mash("file1");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkdir_p!(vfs, &dir2);
assert_vfs_mkfile!(vfs, &file1);
assert_iter_eq(vfs.dirs(&tmpdir).unwrap(), vec![dir1, dir2]);Sourcefn entries<T>(&self, path: T) -> Result<Entries, RvError>
fn entries<T>(&self, path: T) -> Result<Entries, RvError>
Returns an iterator over the given path
- Handles path expansion and absolute path resolution
- Handles recursive path traversal
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = dir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
let mut iter = vfs.entries(vfs.root()).unwrap().into_iter();
assert_iter_eq(iter.map(|x| x.unwrap().path_buf()), vec![vfs.root(), dir, file]);Sourcefn entry<T>(&self, path: T) -> Result<VfsEntry, RvError>
fn entry<T>(&self, path: T) -> Result<VfsEntry, RvError>
Return a virtual filesystem entry for the given path
- Handles converting path to absolute form
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_mkfile!(vfs, &file);
assert!(vfs.entry(&file).unwrap().is_file());Sourcefn exists<T>(&self, path: T) -> bool
fn exists<T>(&self, path: T) -> bool
Returns true if the path exists
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("foo");
assert_eq!(vfs.exists(&dir), false);
assert_vfs_mkdir_p!(vfs, &dir);
assert_eq!(vfs.exists(&dir), true);Sourcefn files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all files for the given path, sorted by name
- Handles path expansion and absolute path resolution
- Paths are returned as abs paths
- Doesn’t include the path itself only its children nor is this recursive
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let dir1 = tmpdir.mash("dir1");
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_mkfile!(vfs, &file2);
assert_iter_eq(vfs.files(&tmpdir).unwrap(), vec![file1, file2]);Sourcefn gid<T>(&self, path: T) -> Result<u32, RvError>
fn gid<T>(&self, path: T) -> Result<u32, RvError>
Returns the group ID of the owner of this file
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
assert_eq!(vfs.gid(vfs.root()).unwrap(), 1000);Sourcefn is_exec<T>(&self, path: T) -> bool
fn is_exec<T>(&self, path: T) -> bool
Returns true if the given path exists and is readonly
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert!(vfs.mkfile_m(&file, 0o644).is_ok());
assert_eq!(vfs.is_exec(&file), false);
assert!(vfs.chmod(&file, 0o777).is_ok());
assert_eq!(vfs.is_exec(&file), true);Sourcefn is_dir<T>(&self, path: T) -> bool
fn is_dir<T>(&self, path: T) -> bool
Returns true if the given path exists and is a directory
- Handles path expansion and absolute path resolution
- Link exclusion i.e. links even if pointing to a directory return false
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
assert_eq!(vfs.is_dir(&dir), false);
assert_vfs_mkdir_p!(vfs, &dir);
assert_eq!(vfs.is_dir(&dir), true);Sourcefn is_file<T>(&self, path: T) -> bool
fn is_file<T>(&self, path: T) -> bool
Returns true if the given path exists and is a file
- Handles path expansion and absolute path resolution
- Link exclusion i.e. links even if pointing to a file return false
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_eq!(vfs.is_file(&file), false);
assert_vfs_mkfile!(vfs, &file);
assert_eq!(vfs.is_file(&file), true);Sourcefn is_readonly<T>(&self, path: T) -> bool
fn is_readonly<T>(&self, path: T) -> bool
Returns true if the given path exists and is readonly
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert!(vfs.mkfile_m(&file, 0o644).is_ok());
assert_eq!(vfs.is_readonly(&file), false);
assert!(vfs.chmod_b(&file).unwrap().readonly().exec().is_ok());
assert_eq!(vfs.mode(&file).unwrap(), 0o100444);
assert_eq!(vfs.is_readonly(&file), true);Sourcefn is_symlink<T>(&self, path: T) -> bool
fn is_symlink<T>(&self, path: T) -> bool
Returns true if the given path exists and is a symlink
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
let link = vfs.root().mash("link");
assert_eq!(vfs.is_symlink(&link), false);
assert_vfs_symlink!(vfs, &link, &file);
assert_eq!(vfs.is_symlink(&link), true);Sourcefn is_symlink_dir<T>(&self, path: T) -> bool
fn is_symlink_dir<T>(&self, path: T) -> bool
Returns true if the given path exists and is a symlink pointing to a directory
- Handles path expansion and absolute path resolution
- Checks the path itself and what it points to
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = vfs.root().mash("file");
let link1 = vfs.root().mash("link1");
let link2 = vfs.root().mash("link2");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert_vfs_symlink!(vfs, &link1, &dir);
assert_vfs_symlink!(vfs, &link2, &file);
assert_eq!(vfs.is_symlink_dir(&link1), true);
assert_eq!(vfs.is_symlink_dir(&link2), false);Sourcefn is_symlink_file<T>(&self, path: T) -> bool
fn is_symlink_file<T>(&self, path: T) -> bool
Returns true if the given path exists and is a symlink pointing to a file
- Handles path expansion and absolute path resolution
- Checks the path itself and what it points to
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = vfs.root().mash("file");
let link1 = vfs.root().mash("link1");
let link2 = vfs.root().mash("link2");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert_vfs_symlink!(vfs, &link1, &dir);
assert_vfs_symlink!(vfs, &link2, &file);
assert_eq!(vfs.is_symlink_file(&link1), false);
assert_eq!(vfs.is_symlink_file(&link2), true);Sourcefn mkdir_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
fn mkdir_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
Creates the given directory and any parent directories needed with the given mode
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
assert!(vfs.mkdir_m(&dir, 0o555).is_ok());
assert_eq!(vfs.mode(&dir).unwrap(), 0o40555);Sourcefn mkdir_p<T>(&self, path: T) -> Result<PathBuf, RvError>
fn mkdir_p<T>(&self, path: T) -> Result<PathBuf, RvError>
Creates the given directory and any parent directories needed
- Handles path expansion and absolute path resolution
§Errors
- PathError::IsNotDir(PathBuf) when the path already exists and is not a directory
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
assert_vfs_no_dir!(vfs, &dir);
assert_eq!(&vfs.mkdir_p(&dir).unwrap(), &dir);
assert_vfs_is_dir!(vfs, &dir);Sourcefn mkfile<T>(&self, path: T) -> Result<PathBuf, RvError>
fn mkfile<T>(&self, path: T) -> Result<PathBuf, RvError>
Create an empty file similar to the linux touch command
- Handles path expansion and absolute path resolution
- Default file creation permissions 0o666 with umask usually ends up being 0o644
§Errors
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotDir(PathBuf) when the given path’s parent isn’t a directory
- PathError::IsNotFile(PathBuf) when the given path exists but isn’t a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert_eq!(&vfs.mkfile(&file).unwrap(), &file);
assert_vfs_is_file!(vfs, &file);Sourcefn mkfile_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
fn mkfile_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
Wraps mkfile allowing for setting the file’s mode.
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert!(vfs.mkfile_m(&file, 0o555).is_ok());
assert_eq!(vfs.mode(&file).unwrap(), 0o100555);Sourcefn mode<T>(&self, path: T) -> Result<u32, RvError>
fn mode<T>(&self, path: T) -> Result<u32, RvError>
Returns the permissions for a file, directory or link
- Handles path expansion and absolute path resolution
§Errors
- PathError::Empty when the given path is empty
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_mkfile!(vfs, &file);
assert_eq!(vfs.mode(&file).unwrap(), 0o100644);
assert!(vfs.chmod(&file, 0o555).is_ok());
assert_eq!(vfs.mode(&file).unwrap(), 0o100555);Sourcefn move_p<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
fn move_p<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
Move a file or directory
- Handles path expansion and absolute path resolution
- Always moves
srcintodstifdstis an existing directory - Replaces destination files if they exist
§Errors
- PathError::DoesNotExist when the source doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = vfs.root().mash("file");
let dirfile = dir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert!(vfs.move_p(&file, &dir).is_ok());
assert_vfs_no_file!(vfs, &file);
assert_vfs_is_file!(vfs, &dirfile);Sourcefn owner<T>(&self, path: T) -> Result<(u32, u32), RvError>
fn owner<T>(&self, path: T) -> Result<(u32, u32), RvError>
Returns the (user ID, group ID) of the owner of this file
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
assert_eq!(vfs.owner(vfs.root()).unwrap(), (1000, 1000));Sourcefn paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
fn paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
Returns all paths for the given path, sorted by name
- Handles path expansion and absolute path resolution
- Paths are returned as abs paths
- Doesn’t include the path itself only its children nor is this recursive
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let tmpdir = vfs.root().mash("tmpdir");
let dir1 = tmpdir.mash("dir1");
let dir2 = tmpdir.mash("dir2");
let file1 = tmpdir.mash("file1");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkdir_p!(vfs, &dir2);
assert_vfs_mkfile!(vfs, &file1);
assert_iter_eq(vfs.paths(&tmpdir).unwrap(), vec![dir1, dir2, file1]);Sourcefn read<T>(&self, path: T) -> Result<Box<dyn ReadSeek>, RvError>
fn read<T>(&self, path: T) -> Result<Box<dyn ReadSeek>, RvError>
Open a file in readonly mode
- Provides a handle to a Read + Seek implementation
- Handles path expansion and absolute path resolution
§Errors
- PathError::IsNotFile(PathBuf) when the given path isn’t a file
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_write_all!(vfs, &file, b"foobar 1");
let mut file = vfs.read(&file).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf);
assert_eq!(buf, "foobar 1".to_string());Sourcefn read_all<T>(&self, path: T) -> Result<String, RvError>
fn read_all<T>(&self, path: T) -> Result<String, RvError>
Read all data from the given file and return it as a String
- Handles path expansion and absolute path resolution
§Errors
- PathError::IsNotFile(PathBuf) when the given path isn’t a file
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_write_all!(vfs, &file, b"foobar 1");
assert_vfs_read_all!(vfs, &file, "foobar 1");Sourcefn read_lines<T>(&self, path: T) -> Result<Vec<String>, RvError>
fn read_lines<T>(&self, path: T) -> Result<Vec<String>, RvError>
Read the given file and returns it as lines in a vector
- Handles path expansion and absolute path resolution
§Errors
- PathError::IsNotFile(PathBuf) when the given path isn’t a file
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_write_all!(vfs, &file, "1\n2");
assert_eq!(vfs.read_lines(&file).unwrap(), vec!["1".to_string(), "2".to_string()]);Sourcefn readlink<T>(&self, path: T) -> Result<PathBuf, RvError>
fn readlink<T>(&self, path: T) -> Result<PathBuf, RvError>
Returns the relative path of the target the link points to
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let link = dir.mash("link");
let file = vfs.root().mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert_vfs_symlink!(vfs, &link, &file);
assert_vfs_readlink!(vfs, &link, PathBuf::from("..").mash("file"));Sourcefn readlink_abs<T>(&self, path: T) -> Result<PathBuf, RvError>
fn readlink_abs<T>(&self, path: T) -> Result<PathBuf, RvError>
Returns the absolute path of the target the link points to
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
let link = vfs.root().mash("link");
assert_vfs_mkfile!(vfs, &file);
assert_vfs_symlink!(vfs, &link, &file);
assert_vfs_readlink_abs!(vfs, &link, &file);Sourcefn remove<T>(&self, path: T) -> Result<(), RvError>
fn remove<T>(&self, path: T) -> Result<(), RvError>
Removes the given empty directory or file
- Handles path expansion and absolute path resolution
- Link exclusion i.e. removes the link themselves not what its points to
§Errors
- a directory containing files will trigger an error. use
remove_allinstead
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_mkfile!(vfs, &file);
assert_vfs_exists!(vfs, &file);
assert_vfs_remove!(vfs, &file);
assert_vfs_no_exists!(vfs, &file);Sourcefn remove_all<T>(&self, path: T) -> Result<(), RvError>
fn remove_all<T>(&self, path: T) -> Result<(), RvError>
Removes the given directory after removing all of its contents
- Handles path expansion and absolute path resolution
- Link exclusion i.e. removes the link themselves not what its points to
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
let file = dir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
assert_vfs_is_file!(vfs, &file);
assert_vfs_remove_all!(vfs, &dir);
assert_vfs_no_exists!(vfs, &file);
assert_vfs_no_exists!(vfs, &dir);Sourcefn root(&self) -> PathBuf
fn root(&self) -> PathBuf
Returns the current root directory
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let mut root = PathBuf::new();
root.push(Component::RootDir);
assert_eq!(vfs.root(), root);Sourcefn set_cwd<T>(&self, path: T) -> Result<PathBuf, RvError>
fn set_cwd<T>(&self, path: T) -> Result<PathBuf, RvError>
Set the current working directory
- Handles path expansion and absolute path resolution
- Relative path will use the current working directory
§Errors
- PathError::DoesNotExist(PathBuf) when the given path doesn’t exist
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let dir = vfs.root().mash("dir");
assert_eq!(vfs.cwd().unwrap(), vfs.root());
assert_vfs_mkdir_p!(vfs, &dir);
assert_eq!(vfs.set_cwd(&dir).unwrap(), dir.clone());
assert_eq!(vfs.cwd().unwrap(), dir);Sourcefn symlink<T, U>(&self, link: T, target: U) -> Result<PathBuf, RvError>
fn symlink<T, U>(&self, link: T, target: U) -> Result<PathBuf, RvError>
Creates a new symbolic link
- Handles path expansion and absolute path resolution
- Computes the target path
srcrelative to thedstlink name’s absolute path - Returns the link path
§Arguments
link- the path of the link being createdtarget- the path that the link will point to
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
let link = vfs.root().mash("link");
assert_vfs_mkfile!(vfs, &file);
assert_vfs_symlink!(vfs, &link, &file);
assert_vfs_readlink_abs!(vfs, &link, &file);Sourcefn uid<T>(&self, path: T) -> Result<u32, RvError>
fn uid<T>(&self, path: T) -> Result<u32, RvError>
Returns the user ID of the owner of this file
- Handles path expansion and absolute path resolution
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
assert_eq!(vfs.uid(vfs.root()).unwrap(), 1000);Sourcefn upcast(self) -> Vfs
fn upcast(self) -> Vfs
Up cast the trait type to the enum wrapper
§Examples
use rivia::prelude::*;
let vfs = Memfs::new().upcast();Sourcefn write<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
fn write<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
Opens a file in write-only mode
- Creates a file if it does not exist or truncates it if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
let mut f = vfs.write(&file).unwrap();
f.write_all(b"foobar").unwrap();
f.flush().unwrap();
assert_vfs_read_all!(vfs, &file, "foobar");Sourcefn write_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
fn write_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
Write the given data to to the target file
- Handles path expansion and absolute path resolution
- Create the file first if it doesn’t exist or truncating it first if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert_vfs_write_all!(vfs, &file, "foobar 1");
assert_vfs_is_file!(vfs, &file);
assert_vfs_read_all!(vfs, &file, "foobar 1");Sourcefn write_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
fn write_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
Write the given lines to to the target file including final newline
- Handles path expansion and absolute path resolution
- Create the file first if it doesn’t exist or truncating it first if it does
§Errors
- PathError::IsNotDir(PathBuf) when the given path’s parent exists but is not a directory
- PathError::DoesNotExist(PathBuf) when the given path’s parent doesn’t exist
- PathError::IsNotFile(PathBuf) when the given path exists but is not a file
§Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_no_file!(vfs, &file);
assert!(vfs.write_lines(&file, &["1", "2"]).is_ok());
assert_vfs_is_file!(vfs, &file);
assert_vfs_read_all!(vfs, &file, "1\n2\n".to_string());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.