Skip to content

Commit

Permalink
vfs: add open_or_create functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bitful-pannul committed Aug 29, 2024
1 parent 631d346 commit b1907fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/vfs/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ pub fn open_dir(path: &str, create: bool, timeout: Option<u64>) -> Result<Direct
}
}

/// Open or create a directory at path.
pub fn open_or_create_dir(path: &str) -> Result<Directory, VfsError> {
match open_dir(path, false, None) {
Ok(dir) => Ok(dir),
Err(_) => match open_dir(path, true, None) {
Ok(dir) => Ok(dir),
Err(e) => Err(e),
},
}
}

/// Removes a dir at path, errors if path not found or path is not a directory.
pub fn remove_dir(path: &str, timeout: Option<u64>) -> Result<(), VfsError> {
let timeout = timeout.unwrap_or(5);
Expand Down
11 changes: 11 additions & 0 deletions src/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ pub fn open_file(path: &str, create: bool, timeout: Option<u64>) -> Result<File,
}
}

/// Open or create a file at path.
pub fn open_or_create_file(path: &str) -> Result<File, VfsError> {
match open_file(path, false, None) {
Ok(file) => Ok(file),
Err(_) => match open_file(path, true, None) {
Ok(file) => Ok(file),
Err(e) => Err(e),
},
}
}

/// Creates a file at path, if file found at path, truncates it to 0.
pub fn create_file(path: &str, timeout: Option<u64>) -> Result<File, VfsError> {
let timeout = timeout.unwrap_or(5);
Expand Down

0 comments on commit b1907fe

Please sign in to comment.