async_std/fs/
hard_link.rs

1use crate::io;
2use crate::path::Path;
3use crate::task::spawn_blocking;
4use crate::utils::Context as _;
5
6/// Creates a hard link on the filesystem.
7///
8/// The `dst` path will be a link pointing to the `src` path. Note that operating systems often
9/// require these two paths to be located on the same filesystem.
10///
11/// This function is an async version of [`std::fs::hard_link`].
12///
13/// [`std::fs::hard_link`]: https://doc.rust-lang.org/std/fs/fn.hard_link.html
14///
15/// # Errors
16///
17/// An error will be returned in the following situations:
18///
19/// * `src` does not point to an existing file.
20/// * Some other I/O error occurred.
21///
22/// # Examples
23///
24/// ```no_run
25/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
26/// #
27/// use async_std::fs;
28///
29/// fs::hard_link("a.txt", "b.txt").await?;
30/// #
31/// # Ok(()) }) }
32/// ```
33pub async fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
34    let from = from.as_ref().to_owned();
35    let to = to.as_ref().to_owned();
36    spawn_blocking(move || {
37        std::fs::hard_link(&from, &to).context(|| {
38            format!(
39                "could not create a hard link from `{}` to `{}`",
40                from.display(),
41                to.display()
42            )
43        })
44    })
45    .await
46}