summaryrefslogtreecommitdiff
path: root/src/fcntl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/fcntl.rs')
-rw-r--r--src/fcntl.rs81
1 files changed, 75 insertions, 6 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs
index dd8e59a6..74d9eb9c 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -5,13 +5,15 @@ use std::ffi::OsString;
use std::os::raw;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::io::RawFd;
-use crate::sys::stat::Mode;
-use crate::{NixPath, Result};
#[cfg(any(target_os = "android", target_os = "linux"))]
use std::ptr; // For splice and copy_file_range
-#[cfg(any(target_os = "android", target_os = "linux"))]
-use crate::sys::uio::IoVec; // For vmsplice
+#[cfg(feature = "fs")]
+use crate::{
+ NixPath,
+ Result,
+ sys::stat::Mode
+};
#[cfg(any(
target_os = "linux",
@@ -22,10 +24,13 @@ use crate::sys::uio::IoVec; // For vmsplice
target_env = "uclibc",
target_os = "freebsd"
))]
-pub use self::posix_fadvise::*;
+#[cfg(feature = "fs")]
+pub use self::posix_fadvise::{PosixFadviseAdvice, posix_fadvise};
#[cfg(not(target_os = "redox"))]
+#[cfg(any(feature = "fs", feature = "process"))]
libc_bitflags! {
+ #[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "process"))))]
pub struct AtFlags: c_int {
AT_REMOVEDIR;
AT_SYMLINK_FOLLOW;
@@ -39,18 +44,22 @@ libc_bitflags! {
}
}
+#[cfg(any(feature = "fs", feature = "term"))]
libc_bitflags!(
/// Configuration options for opened files.
+ #[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "term"))))]
pub struct OFlag: c_int {
/// Mask for the access mode of the file.
O_ACCMODE;
/// Use alternate I/O semantics.
#[cfg(target_os = "netbsd")]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_ALT_IO;
/// Open the file in append-only mode.
O_APPEND;
/// Generate a signal when input or output becomes possible.
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_ASYNC;
/// Closes the file descriptor once an `execve` call is made.
///
@@ -64,9 +73,11 @@ libc_bitflags!(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_DIRECT;
/// If the specified path isn't a directory, fail.
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_DIRECTORY;
/// Implicitly follow each `write()` with an `fdatasync()`.
#[cfg(any(target_os = "android",
@@ -75,11 +86,13 @@ libc_bitflags!(
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_DSYNC;
/// Error out if a file was not created.
O_EXCL;
/// Open for execute only.
#[cfg(target_os = "freebsd")]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_EXEC;
/// Open with an exclusive file lock.
#[cfg(any(target_os = "dragonfly",
@@ -89,6 +102,7 @@ libc_bitflags!(
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_EXLOCK;
/// Same as `O_SYNC`.
#[cfg(any(target_os = "dragonfly",
@@ -99,18 +113,23 @@ libc_bitflags!(
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_FSYNC;
/// Allow files whose sizes can't be represented in an `off_t` to be opened.
#[cfg(any(target_os = "android", target_os = "linux"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_LARGEFILE;
/// Do not update the file last access time during `read(2)`s.
#[cfg(any(target_os = "android", target_os = "linux"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_NOATIME;
/// Don't attach the device as the process' controlling terminal.
#[cfg(not(target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_NOCTTY;
/// Same as `O_NONBLOCK`.
#[cfg(not(target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_NDELAY;
/// `open()` will fail if the given path is a symbolic link.
O_NOFOLLOW;
@@ -118,11 +137,13 @@ libc_bitflags!(
O_NONBLOCK;
/// Don't deliver `SIGPIPE`.
#[cfg(target_os = "netbsd")]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_NOSIGPIPE;
/// Obtain a file descriptor for low-level access.
///
/// The file itself is not opened and other file operations will fail.
#[cfg(any(target_os = "android", target_os = "linux", target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_PATH;
/// Only allow reading.
///
@@ -134,9 +155,11 @@ libc_bitflags!(
O_RDWR;
/// Similar to `O_DSYNC` but applies to `read`s instead.
#[cfg(any(target_os = "linux", target_os = "netbsd", target_os = "openbsd"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_RSYNC;
/// Skip search permission checks.
#[cfg(target_os = "netbsd")]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_SEARCH;
/// Open with a shared file lock.
#[cfg(any(target_os = "dragonfly",
@@ -146,17 +169,21 @@ libc_bitflags!(
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_SHLOCK;
/// Implicitly follow each `write()` with an `fsync()`.
#[cfg(not(target_os = "redox"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_SYNC;
/// Create an unnamed temporary file.
#[cfg(any(target_os = "android", target_os = "linux"))]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_TMPFILE;
/// Truncate an existing regular file to 0 length if it allows writing.
O_TRUNC;
/// Restore default TTY attributes.
#[cfg(target_os = "freebsd")]
+ #[cfg_attr(docsrs, doc(cfg(all())))]
O_TTY_INIT;
/// Only allow writing.
///
@@ -165,6 +192,9 @@ libc_bitflags!(
}
);
+feature! {
+#![feature = "fs"]
+
// The conversion is not identical on all operating systems.
#[allow(clippy::useless_conversion)]
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
@@ -209,12 +239,15 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
})??;
Errno::result(res).map(drop)
}
+}
#[cfg(all(
target_os = "linux",
target_env = "gnu",
))]
+#[cfg(feature = "fs")]
libc_bitflags! {
+ #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct RenameFlags: u32 {
RENAME_EXCHANGE;
RENAME_NOREPLACE;
@@ -222,6 +255,8 @@ libc_bitflags! {
}
}
+feature! {
+#![feature = "fs"]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
@@ -348,10 +383,13 @@ pub(crate) fn at_rawfd(fd: Option<RawFd>) -> raw::c_int {
Some(fd) => fd,
}
}
+}
#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(feature = "fs")]
libc_bitflags!(
/// Additional flags for file sealing, which allows for limiting operations on a file.
+ #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct SealFlag: c_int {
/// Prevents further calls to `fcntl()` with `F_ADD_SEALS`.
F_SEAL_SEAL;
@@ -364,14 +402,19 @@ libc_bitflags!(
}
);
+#[cfg(feature = "fs")]
libc_bitflags!(
/// Additional configuration flags for `fcntl`'s `F_SETFD`.
+ #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct FdFlag: c_int {
/// The file descriptor will automatically be closed during a successful `execve(2)`.
FD_CLOEXEC;
}
);
+feature! {
+#![feature = "fs"]
+
#[cfg(not(target_os = "redox"))]
#[derive(Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
@@ -455,6 +498,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
Errno::result(res)
}
+// TODO: convert to libc_enum
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FlockArg {
@@ -483,10 +527,13 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
Errno::result(res).map(drop)
}
+}
#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(feature = "zerocopy")]
libc_bitflags! {
/// Additional flags to `splice` and friends.
+ #[cfg_attr(docsrs, doc(cfg(feature = "zerocopy")))]
pub struct SpliceFFlags: c_uint {
/// Request that pages be moved instead of copied.
///
@@ -505,6 +552,9 @@ libc_bitflags! {
}
}
+feature! {
+#![feature = "zerocopy"]
+
/// Copy a range of data from one file to another
///
/// The `copy_file_range` system call performs an in-kernel copy between
@@ -577,7 +627,12 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu
}
#[cfg(any(target_os = "linux", target_os = "android"))]
-pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<usize> {
+pub fn vmsplice(
+ fd: RawFd,
+ iov: &[crate::sys::uio::IoVec<&[u8]>],
+ flags: SpliceFFlags
+ ) -> Result<usize>
+{
let ret = unsafe {
libc::vmsplice(
fd,
@@ -588,10 +643,13 @@ pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<
};
Errno::result(ret).map(|r| r as usize)
}
+}
#[cfg(any(target_os = "linux"))]
+#[cfg(feature = "fs")]
libc_bitflags!(
/// Mode argument flags for fallocate determining operation performed on a given range.
+ #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct FallocateFlags: c_int {
/// File size is not changed.
///
@@ -620,11 +678,15 @@ libc_bitflags!(
}
);
+feature! {
+#![feature = "fs"]
+
/// Manipulates file space.
///
/// Allows the caller to directly manipulate the allocated disk space for the
/// file referred to by fd.
#[cfg(any(target_os = "linux"))]
+#[cfg(feature = "fs")]
pub fn fallocate(
fd: RawFd,
mode: FallocateFlags,
@@ -635,6 +697,7 @@ pub fn fallocate(
Errno::result(res).map(drop)
}
+
#[cfg(any(
target_os = "linux",
target_os = "android",
@@ -649,9 +712,11 @@ mod posix_fadvise {
use std::os::unix::io::RawFd;
use crate::Result;
+ #[cfg(feature = "fs")]
libc_enum! {
#[repr(i32)]
#[non_exhaustive]
+ #[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub enum PosixFadviseAdvice {
POSIX_FADV_NORMAL,
POSIX_FADV_SEQUENTIAL,
@@ -662,6 +727,8 @@ mod posix_fadvise {
}
}
+ feature! {
+ #![feature = "fs"]
pub fn posix_fadvise(
fd: RawFd,
offset: libc::off_t,
@@ -676,6 +743,7 @@ mod posix_fadvise {
Err(Errno::from_i32(res))
}
}
+ }
}
#[cfg(any(
@@ -694,3 +762,4 @@ pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Resu
Ok(errno) => Err(Errno::from_i32(errno)),
}
}
+}