async_std/os/unix/
fs.rs

1//! Unix-specific filesystem extensions.
2
3use crate::io;
4use crate::path::Path;
5use crate::task::spawn_blocking;
6
7/// Creates a new symbolic link on the filesystem.
8///
9/// The `dst` path will be a symbolic link pointing to the `src` path.
10///
11/// This function is an async version of [`std::os::unix::fs::symlink`].
12///
13/// [`std::os::unix::fs::symlink`]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
14///
15/// # Examples
16///
17/// ```no_run
18/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
19/// #
20/// use async_std::os::unix::fs::symlink;
21///
22/// symlink("a.txt", "b.txt").await?;
23/// #
24/// # Ok(()) }) }
25/// ```
26pub async fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
27    let src = src.as_ref().to_owned();
28    let dst = dst.as_ref().to_owned();
29    spawn_blocking(move || std::os::unix::fs::symlink(&src, &dst)).await
30}
31
32cfg_not_docs! {
33    pub use std::os::unix::fs::{DirBuilderExt, DirEntryExt, OpenOptionsExt};
34}
35
36cfg_docs! {
37    /// Unix-specific extensions to `DirBuilder`.
38    pub trait DirBuilderExt {
39        /// Sets the mode to create new directories with. This option defaults to
40        /// `0o777`.
41        fn mode(&mut self, mode: u32) -> &mut Self;
42    }
43
44    /// Unix-specific extension methods for `DirEntry`.
45    pub trait DirEntryExt {
46        /// Returns the underlying `d_ino` field in the contained `dirent`
47        /// structure.
48        fn ino(&self) -> u64;
49    }
50
51    /// Unix-specific extensions to `OpenOptions`.
52    pub trait OpenOptionsExt {
53        /// Sets the mode bits that a new file will be created with.
54        ///
55        /// If a new file is created as part of a `File::open_opts` call then this
56        /// specified `mode` will be used as the permission bits for the new file.
57        /// If no `mode` is set, the default of `0o666` will be used.
58        /// The operating system masks out bits with the systems `umask`, to produce
59        /// the final permissions.
60        fn mode(&mut self, mode: u32) -> &mut Self;
61
62        /// Pass custom flags to the `flags` argument of `open`.
63        ///
64        /// The bits that define the access mode are masked out with `O_ACCMODE`, to
65        /// ensure they do not interfere with the access mode set by Rusts options.
66        ///
67        /// Custom flags can only set flags, not remove flags set by Rusts options.
68        /// This options overwrites any previously set custom flags.
69        fn custom_flags(&mut self, flags: i32) -> &mut Self;
70    }
71}