Skip to content

Commit c7c65a1

Browse files
authored
symlink: rename parameters and fix error messages to match std (#83)
Previously, upstream named the parameters to `fn symlink(src: P, dest: Q)`. This is confusing because `dest` is the path of the link that's _created_, not the _destination_ the link points to. In fact, it's so confusing that all the error messages from this crate listed the paths incorrectly: if `fs_err::os::unix::fs::symlink("link-destination", "link-source")` failed, the error message would say: failed to symlink file from link-destination to link-source Now, in both `std` and `tokio`, these functions are named like `fn symlink(original: P, link: Q)`. Let's update our wrappers to match the new convention.
1 parent 133cf80 commit c7c65a1

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,17 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
263263

264264
/// Creates a new hard link on the filesystem.
265265
///
266+
/// The `link` path will be a link pointing to the `original` path. Note that
267+
/// systems often require these two paths to both be located on the same
268+
/// filesystem.
269+
///
266270
/// Wrapper for [`fs::hard_link`](https://doc.rust-lang.org/stable/std/fs/fn.hard_link.html).
267-
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
268-
let src = src.as_ref();
269-
let dst = dst.as_ref();
270-
fs::hard_link(src, dst)
271-
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::HardLink, src, dst))
271+
pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
272+
let original = original.as_ref();
273+
let link = link.as_ref();
274+
fs::hard_link(original, link).map_err(|source| {
275+
SourceDestError::build(source, SourceDestErrorKind::HardLink, link, original)
276+
})
272277
}
273278

274279
/// Reads a symbolic link, returning the file that the link points to.
@@ -289,15 +294,20 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
289294
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::Rename, from, to))
290295
}
291296

297+
/// Creates a new symbolic link on the filesystem.
298+
///
299+
/// The `link` path will be a symbolic link pointing to the `original` path.
300+
///
292301
/// Wrapper for [`fs::soft_link`](https://doc.rust-lang.org/stable/std/fs/fn.soft_link.html).
293302
#[deprecated = "replaced with std::os::unix::fs::symlink and \
294303
std::os::windows::fs::{symlink_file, symlink_dir}"]
295-
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
296-
let src = src.as_ref();
297-
let dst = dst.as_ref();
304+
pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
305+
let original = original.as_ref();
306+
let link = link.as_ref();
298307
#[allow(deprecated)]
299-
fs::soft_link(src, dst)
300-
.map_err(|source| SourceDestError::build(source, SourceDestErrorKind::SoftLink, src, dst))
308+
fs::soft_link(original, link).map_err(|source| {
309+
SourceDestError::build(source, SourceDestErrorKind::SoftLink, link, original)
310+
})
301311
}
302312

303313
/// Query the metadata about a file without following symlinks.

src/os/unix.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ pub mod fs {
88

99
/// Creates a new symbolic link on the filesystem.
1010
///
11+
/// The `link` path will be a symbolic link pointing to the `original` path.
12+
///
1113
/// Wrapper for [`std::os::unix::fs::symlink`](https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html)
12-
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
13-
let src = src.as_ref();
14-
let dst = dst.as_ref();
15-
std::os::unix::fs::symlink(src, dst)
16-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Symlink, src, dst))
14+
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
15+
let original = original.as_ref();
16+
let link = link.as_ref();
17+
std::os::unix::fs::symlink(original, link).map_err(|err| {
18+
SourceDestError::build(err, SourceDestErrorKind::Symlink, link, original)
19+
})
1720
}
1821

1922
/// Wrapper for [`std::os::unix::fs::FileExt`](https://doc.rust-lang.org/std/os/unix/fs/trait.FileExt.html).

src/os/windows.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,28 @@ pub mod fs {
66

77
/// Creates a new symlink to a directory on the filesystem.
88
///
9+
/// The `link` path will be a symbolic link pointing to the `original` path.
10+
///
911
/// Wrapper for [std::os::windows::fs::symlink_dir](https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html)
10-
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
11-
let src = src.as_ref();
12-
let dst = dst.as_ref();
13-
std::os::windows::fs::symlink_dir(src, dst)
14-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkDir, src, dst))
12+
pub fn symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
13+
let original = original.as_ref();
14+
let link = link.as_ref();
15+
std::os::windows::fs::symlink_dir(original, link).map_err(|err| {
16+
SourceDestError::build(err, SourceDestErrorKind::SymlinkDir, link, original)
17+
})
1518
}
1619

1720
/// Creates a new symlink to a non-directory file on the filesystem.
1821
///
22+
/// The `link` path will be a symbolic link pointing to the `original` path.
23+
///
1924
/// Wrapper for [std::os::windows::fs::symlink_file](https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html)
20-
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
21-
let src = src.as_ref();
22-
let dst = dst.as_ref();
23-
std::os::windows::fs::symlink_file(src, dst)
24-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkFile, src, dst))
25+
pub fn symlink_file<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
26+
let original = original.as_ref();
27+
let link = link.as_ref();
28+
std::os::windows::fs::symlink_file(original, link).map_err(|err| {
29+
SourceDestError::build(err, SourceDestErrorKind::SymlinkFile, link, original)
30+
})
2531
}
2632

2733
/// Wrapper for [`std::os::windows::fs::FileExt`](https://doc.rust-lang.org/std/os/windows/fs/trait.FileExt.html).

src/tokio/mod.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,17 @@ pub async fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
6464

6565
/// Creates a new hard link on the filesystem.
6666
///
67+
/// The `link` path will be a link pointing to the `original` path. Note that
68+
/// systems often require these two paths to both be located on the same
69+
/// filesystem.
70+
///
6771
/// Wrapper for [`tokio::fs::hard_link`].
6872
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
69-
pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
70-
let (src, dst) = (src.as_ref(), dst.as_ref());
71-
tokio::fs::hard_link(src, dst)
73+
pub async fn hard_link(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
74+
let (original, link) = (original.as_ref(), link.as_ref());
75+
tokio::fs::hard_link(original, link)
7276
.await
73-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::HardLink, src, dst))
77+
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::HardLink, link, original))
7478
}
7579

7680
/// Given a path, queries the file system to get information about a file,
@@ -199,38 +203,46 @@ pub async fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
199203

200204
/// Creates a new symbolic link on the filesystem.
201205
///
206+
/// The `link` path will be a symbolic link pointing to the `original` path.
207+
///
202208
/// Wrapper for [`tokio::fs::symlink`].
203209
#[cfg(unix)]
204210
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
205-
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
206-
let (src, dst) = (src.as_ref(), dst.as_ref());
207-
tokio::fs::symlink(src, dst)
211+
pub async fn symlink(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
212+
let (original, link) = (original.as_ref(), link.as_ref());
213+
tokio::fs::symlink(original, link)
208214
.await
209-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Symlink, src, dst))
215+
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Symlink, link, original))
210216
}
211217

212218
/// Creates a new directory symlink on the filesystem.
213219
///
220+
/// The `link` path will be a symbolic link pointing to the `original` path.
221+
///
214222
/// Wrapper for [`tokio::fs::symlink_dir`].
215223
#[cfg(windows)]
216224
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
217-
pub async fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
218-
let (src, dst) = (src.as_ref(), dst.as_ref());
219-
tokio::fs::symlink_dir(src, dst)
225+
pub async fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
226+
let (original, link) = (original.as_ref(), link.as_ref());
227+
tokio::fs::symlink_dir(original, link)
220228
.await
221-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkDir, src, dst))
229+
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkDir, link, original))
222230
}
223231

224232
/// Creates a new file symbolic link on the filesystem.
225233
///
234+
/// The `link` path will be a symbolic link pointing to the `original` path.
235+
///
226236
/// Wrapper for [`tokio::fs::symlink_file`].
227237
#[cfg(windows)]
228238
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
229-
pub async fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
230-
let (src, dst) = (src.as_ref(), dst.as_ref());
231-
tokio::fs::symlink_file(src, dst)
239+
pub async fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> io::Result<()> {
240+
let (original, link) = (original.as_ref(), link.as_ref());
241+
tokio::fs::symlink_file(original, link)
232242
.await
233-
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkFile, src, dst))
243+
.map_err(|err| {
244+
SourceDestError::build(err, SourceDestErrorKind::SymlinkFile, link, original)
245+
})
234246
}
235247

236248
/// Creates a future that will open a file for writing and write the entire

0 commit comments

Comments
 (0)