feat(fs): add .file(path).hard_link(path)
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 3m59s
Rust / build (map[name:stable]) (push) Successful in 1m45s
Release Please / Release-plz (push) Successful in 1m26s

This commit is contained in:
Paul Campbell 2024-11-03 17:46:13 +00:00
parent 015c28632e
commit 10e6243f6e
3 changed files with 38 additions and 1 deletions

View file

@ -8,7 +8,7 @@ Provides injectable Filesystem and Network resources to make code more testable.
- [x] `std::fs::copy` - `file(path).copy(target)` - Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.
- [x] `std::fs::create_dir` - `dir(path).create()` - Creates a new, empty directory at the provided path
- [x] `std::fs::create_dir_all` - `dir(path).create_all()` - Recursively create a directory and all of its parent components if they are missing.
- [ ] `std::fs::hard_link` - `link(path)create()` - Creates a new hard link on the filesystem.
- [x] `std::fs::hard_link` - `file(path).hard_link(other)` - Creates a new hard link on the filesystem.
- [ ] `std::fs::metadata` - `path(path).metadata()` - Given a path, query the file system to get information about a file, directory, etc.
- [x] `std::fs::read` - `file(path).reader().bytes()` - Read the entire contents of a file into a bytes vector.
- [x] `std::fs::read_dir` - `dir(path).read()` - Returns an iterator over the entries within a directory.

View file

@ -76,4 +76,26 @@ impl<'base, 'path> FileHandle<'base, 'path> {
self.check_error()?;
std::fs::remove_file(self.as_pathbuf()).map_err(Error::Io)
}
/// Creates a hard link on the filesystem.
///
/// Wrapper for [std::fs::hard_link]
///
/// ```
/// # use kxio::fs::Result;
/// # fn main() -> Result<()> {
/// let fs = kxio::fs::temp()?;
/// let src_path = fs.base().join("foo");
/// let src = fs.file(&src_path);
/// # src.write("bar")?;
/// let dst_path = fs.base().join("bar");
/// let dst = fs.file(&dst_path);
/// src.hard_link(&dst)?;
/// # Ok(())
/// # }
/// ```
pub fn hard_link(&self, dest: &FileHandle<'base, 'path>) -> Result<()> {
self.check_error()?;
std::fs::hard_link(self.as_pathbuf(), dest.as_pathbuf()).map_err(Error::Io)
}
}

View file

@ -269,6 +269,21 @@ mod path {
mod file {
use super::*;
#[test]
fn create_hard_link() -> TestResult {
let fs = fs::temp().expect("temp fs");
let file_path = fs.base().join("foo");
let file = fs.file(&file_path);
file.write("content").expect("write");
let link_path = fs.base().join("bar");
let link = fs.file(&link_path);
file.hard_link(&link).expect("hard_link");
let exists = link.exists().expect("exists");
assert!(exists);
Ok(())
}
#[test]
/// Write to a file, read it, verify it exists, is a file and has the expected contents
fn write_read_file_exists() -> TestResult {