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

#340 in Unix APIs

Download history 4531/week @ 2023-12-08 5080/week @ 2023-12-15 2871/week @ 2023-12-22 2843/week @ 2023-12-29 5374/week @ 2024-01-05 5460/week @ 2024-01-12 4252/week @ 2024-01-19 6957/week @ 2024-01-26 6332/week @ 2024-02-02 7123/week @ 2024-02-09 6472/week @ 2024-02-16 5468/week @ 2024-02-23 6522/week @ 2024-03-01 6183/week @ 2024-03-08 7348/week @ 2024-03-15 5773/week @ 2024-03-22

26,780 downloads per month
Used in 6 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

~42KB