broker_tokio/fs/
hard_link.rs

1use crate::fs::asyncify;
2
3use std::io;
4use std::path::Path;
5
6/// Creates a new hard link on the filesystem.
7///
8/// This is an async version of [`std::fs::hard_link`][std]
9///
10/// [std]: std::fs::hard_link
11///
12/// The `dst` path will be a link pointing to the `src` path. Note that systems
13/// often require these two paths to both be located on the same filesystem.
14///
15/// # Platform-specific behavior
16///
17/// This function currently corresponds to the `link` function on Unix
18/// and the `CreateHardLink` function on Windows.
19/// Note that, this [may change in the future][changes].
20///
21/// [changes]: https://doc.rust-lang.org/std/io/index.html#platform-specific-behavior
22///
23/// # Errors
24///
25/// This function will return an error in the following situations, but is not
26/// limited to just these cases:
27///
28/// * The `src` path is not a file or doesn't exist.
29///
30/// # Examples
31///
32/// ```no_run
33/// use tokio::fs;
34///
35/// #[tokio::main]
36/// async fn main() -> std::io::Result<()> {
37///     fs::hard_link("a.txt", "b.txt").await?; // Hard link a.txt to b.txt
38///     Ok(())
39/// }
40/// ```
41pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
42    let src = src.as_ref().to_owned();
43    let dst = dst.as_ref().to_owned();
44
45    asyncify(move || std::fs::hard_link(src, dst)).await
46}