VirtualFileSystem

Trait VirtualFileSystem 

Source
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§

Source

fn abs<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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));
Source

fn all_dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn all_files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn all_paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn append<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
where T: AsRef<Path>,

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());
Source

fn append_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<[u8]>,

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");
Source

fn append_line<T, U>(&self, path: T, line: U) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<str>,

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");
Source

fn append_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<str>,

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");
Source

fn chmod<T>(&self, path: T, mode: u32) -> Result<(), RvError>
where T: AsRef<Path>,

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_b for 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);
Source

fn chmod_b<T>(&self, path: T) -> Result<Chmod, RvError>
where T: AsRef<Path>,

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);
Source

fn chown<T>(&self, path: T, uid: u32, gid: u32) -> Result<(), RvError>
where T: AsRef<Path>,

Change the ownership of the path recursivly

  • Handles path expansion and absolute path resolution
  • Use chown_b for 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));
Source

fn chown_b<T>(&self, path: T) -> Result<Chown, RvError>
where T: AsRef<Path>,

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));
Source

fn config_dir<T>(&self, config: T) -> Option<PathBuf>
where T: AsRef<str>,

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");
}
Source

fn copy<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<Path>,

Copies src to dst recursively

  • dst will be copied into if it is an existing directory
  • dst will 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");
Source

fn copy_b<T, U>(&self, src: T, dst: U) -> Result<Copier, RvError>
where T: AsRef<Path>, U: AsRef<Path>,

Creates a new Copier for use with the builder pattern

  • dst will be copied into if it is an existing directory
  • dst will 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");
Source

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);
Source

fn dirs<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn entries<T>(&self, path: T) -> Result<Entries, RvError>
where T: AsRef<Path>,

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]);
Source

fn entry<T>(&self, path: T) -> Result<VfsEntry, RvError>
where T: AsRef<Path>,

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());
Source

fn exists<T>(&self, path: T) -> bool
where T: AsRef<Path>,

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);
Source

fn files<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn gid<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>,

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);
Source

fn is_exec<T>(&self, path: T) -> bool
where T: AsRef<Path>,

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);
Source

fn is_dir<T>(&self, path: T) -> bool
where T: AsRef<Path>,

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);
Source

fn is_file<T>(&self, path: T) -> bool
where T: AsRef<Path>,

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);
Source

fn is_readonly<T>(&self, path: T) -> bool
where T: AsRef<Path>,

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);

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);

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);

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);
Source

fn mkdir_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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);
Source

fn mkdir_p<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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);
Source

fn mkfile<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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);
Source

fn mkfile_m<T>(&self, path: T, mode: u32) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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);
Source

fn mode<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>,

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);
Source

fn move_p<T, U>(&self, src: T, dst: U) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<Path>,

Move a file or directory

  • Handles path expansion and absolute path resolution
  • Always moves src into dst if dst is 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);
Source

fn owner<T>(&self, path: T) -> Result<(u32, u32), RvError>
where T: AsRef<Path>,

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));
Source

fn paths<T>(&self, path: T) -> Result<Vec<PathBuf>, RvError>
where T: AsRef<Path>,

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]);
Source

fn read<T>(&self, path: T) -> Result<Box<dyn ReadSeek>, RvError>
where T: AsRef<Path>,

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());
Source

fn read_all<T>(&self, path: T) -> Result<String, RvError>
where T: AsRef<Path>,

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");
Source

fn read_lines<T>(&self, path: T) -> Result<Vec<String>, RvError>
where T: AsRef<Path>,

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()]);

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"));

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);
Source

fn remove<T>(&self, path: T) -> Result<(), RvError>
where T: AsRef<Path>,

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_all instead
§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);
Source

fn remove_all<T>(&self, path: T) -> Result<(), RvError>
where T: AsRef<Path>,

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);
Source

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);
Source

fn set_cwd<T>(&self, path: T) -> Result<PathBuf, RvError>
where T: AsRef<Path>,

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);

Creates a new symbolic link

  • Handles path expansion and absolute path resolution
  • Computes the target path src relative to the dst link name’s absolute path
  • Returns the link path
§Arguments
  • link - the path of the link being created
  • target - 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);
Source

fn uid<T>(&self, path: T) -> Result<u32, RvError>
where T: AsRef<Path>,

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);
Source

fn upcast(self) -> Vfs

Up cast the trait type to the enum wrapper

§Examples
use rivia::prelude::*;

let vfs = Memfs::new().upcast();
Source

fn write<T>(&self, path: T) -> Result<Box<dyn Write>, RvError>
where T: AsRef<Path>,

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");
Source

fn write_all<T, U>(&self, path: T, data: U) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<[u8]>,

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");
Source

fn write_lines<T, U>(&self, path: T, lines: &[U]) -> Result<(), RvError>
where T: AsRef<Path>, U: AsRef<str>,

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.

Implementors§