10 releases (5 breaking)

0.6.3 Sep 20, 2023
0.6.1 Oct 11, 2022
0.5.0 Jul 3, 2021
0.3.0 Oct 5, 2020
0.2.0 Jun 27, 2020

#318 in Unix APIs

Download history 4581/week @ 2024-01-15 3950/week @ 2024-01-22 7186/week @ 2024-01-29 6741/week @ 2024-02-05 7952/week @ 2024-02-12 5262/week @ 2024-02-19 5441/week @ 2024-02-26 6582/week @ 2024-03-04 7066/week @ 2024-03-11 6807/week @ 2024-03-18 7509/week @ 2024-03-25 7765/week @ 2024-04-01 6889/week @ 2024-04-08 7027/week @ 2024-04-15 8399/week @ 2024-04-22 8692/week @ 2024-04-29

31,160 downloads per month
Used in 5 crates (4 directly)

BSD-2-Clause

15KB
141 lines

filedesc docs tests

This crate exposes a single type: FileDesc, which acts as a thin wrapper around open file descriptors. The wrapped file descriptor is closed when the wrapper is dropped.

You can call FileDesc::new() with any type that implements IntoRawFd, or duplicate the file descriptor of a type that implements AsRawFd with duplicate_from, or directly from a raw file descriptor with from_raw_fd() and duplicate_raw_fd(). Wrapped file descriptors can also be duplicated with the duplicate() function.

Close-on-exec

Whenever the library duplicates a file descriptor, it tries to set the close-on-exec flag atomically. On platforms where this is not supported, the library falls back to setting the flag non-atomically. When an existing file descriptor is wrapped, the close-on-exec flag is left as it was.

You can also check or set the close-on-exec flag with the get_close_on_exec() and set_close_on_exec functions.

Example

use filedesc::FileDesc;
let fd = unsafe { FileDesc::from_raw_fd(raw_fd) };
let duplicated = unsafe { fd.duplicate()? };
assert_eq!(duplicated.get_close_on_exec()?, true);

duplicated.set_close_on_exec(false)?;
assert_eq!(duplicated.get_close_on_exec()?, false);

Dependencies

~43KB