diff --git a/README.md b/README.md index 6b09e01..ea54678 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/fs/file.rs b/src/fs/file.rs index 8f30e4e..57b8e92 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -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) + } } diff --git a/tests/fs.rs b/tests/fs.rs index 3eb5fff..fc19ab1 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -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 {