21 releases

0.1.21 Apr 16, 2021
0.1.20 Mar 19, 2021
0.1.19 Jun 10, 2020
0.1.18 Aug 5, 2019
0.1.2 Dec 28, 2016

#183 in Filesystem

Download history 520/week @ 2024-07-21 608/week @ 2024-07-28 604/week @ 2024-08-04 655/week @ 2024-08-11 1112/week @ 2024-08-18 1287/week @ 2024-08-25 1037/week @ 2024-09-01 12710/week @ 2024-09-08 12824/week @ 2024-09-15 17741/week @ 2024-09-22 16982/week @ 2024-09-29 17325/week @ 2024-10-06 15155/week @ 2024-10-13 14156/week @ 2024-10-20 14789/week @ 2024-10-27 14418/week @ 2024-11-03

60,700 downloads per month
Used in 23 crates (11 directly)

MIT/Apache

39KB
741 lines

Openat Crate

Status: Beta

Documentation | Github | Crate

The interface to openat, symlinkat, and other functions in *at family.

Dependent crates

This crate is a thin wrapper for the underlying system calls. You may find the extension methods in openat-ext useful.

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Handling Files Relative to File Descriptor

Main concept here is a Dir which holds O_PATH file descriptor, you can create it with:

  • Dir::open("/some/path") -- open this directory as a file descriptor
  • Dir::from_raw_fd(fd) -- uses a file descriptor provided elsewhere

Note after opening file descriptors refer to same directory regardless of where it's moved or mounted (with pivot_root or mount --move). It may also be unmounted or be out of chroot and you will still be able to access files relative to it.

Note2: The constructor Dir::cwd() is deprecated, and it's recommended to use Dir::open(".") instead.

Note3: Some OS's (e.g., macOS) do not provide O_PATH, in which case the file descriptor is of regular type.

Most other operations are done on Dir object and are executed relative to it:

  • Dir::list_dir()
  • Dir::sub_dir()
  • Dir::read_link()
  • Dir::open_file()
  • Dir::create_file()
  • Dir::update_file()
  • Dir::create_dir()
  • Dir::symlink()
  • Dir::local_rename()

Functions that expect path relative to the directory accept both the traditional path-like objects, such as Path, PathBuf and &str, and Entry type returned from list_dir(). The latter is faster as underlying system call wants CString and we keep that in entry.

Note that if path supplied to any method of dir is absolute the Dir file descriptor is ignored.

Also while all methods of dir accept any path if you want to prevent certain symlink attacks and race condition you should only use a single-component path. I.e. open one part of a chain at a time.

Dependencies