Skip to content

Commit 8731404

Browse files
committed
Add docs to the time and fcntl modules
Also, deprecate FlockArg::UnlockNonblock. That combination of flags doesn't make sense. And indeed, reviewing the kernel source code shows that the kernel will ignore LOCK_NB when LOCK_UN is set.
1 parent 1acc5e8 commit 8731404

File tree

3 files changed

+178
-8
lines changed

3 files changed

+178
-8
lines changed

src/fcntl.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! file control options
12
use crate::errno::Errno;
23
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
34
use core::slice;
@@ -45,15 +46,28 @@ pub use self::posix_fadvise::{posix_fadvise, PosixFadviseAdvice};
4546
#[cfg(not(target_os = "redox"))]
4647
#[cfg(any(feature = "fs", feature = "process", feature = "user"))]
4748
libc_bitflags! {
49+
/// Flags that control how the various *at syscalls behave.
4850
#[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "process"))))]
4951
pub struct AtFlags: c_int {
52+
/// Used with [`unlink`](crate::unistd::unlink) to indicate that the path to remove is a
53+
/// directory, and not a regular file.
5054
AT_REMOVEDIR;
55+
/// Used with [`linkat`](crate::unistd::linkat`) to create a link to a symbolic link's
56+
/// target, instead of to the symbolic link itself.
5157
AT_SYMLINK_FOLLOW;
58+
/// Used with [`linkat`](crate::unistd::linkat`) to create a link to a symbolic link
59+
/// itself, instead of to the symbolic link's target.
5260
AT_SYMLINK_NOFOLLOW;
61+
/// Don't automount the terminal ("basename") component of pathname if it is a directory
62+
/// that is an automount point.
5363
#[cfg(linux_android)]
5464
AT_NO_AUTOMOUNT;
65+
/// If the provided path is an empty string, operate on the provided directory file
66+
/// descriptor instead.
5567
#[cfg(any(linux_android, target_os = "freebsd", target_os = "hurd"))]
5668
AT_EMPTY_PATH;
69+
/// Used with [`faccessat`](crate::unistd::faccessat), the checks for accessibility are
70+
/// performed using the effective user and group IDs instead of the real user and group ID
5771
#[cfg(not(target_os = "android"))]
5872
AT_EACCESS;
5973
}
@@ -186,6 +200,10 @@ pub(crate) fn at_rawfd(fd: Option<RawFd>) -> raw::c_int {
186200
feature! {
187201
#![feature = "fs"]
188202

203+
/// open or create a file for reading, writing or executing
204+
///
205+
/// # See Also
206+
/// [`open`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html)
189207
// The conversion is not identical on all operating systems.
190208
#[allow(clippy::useless_conversion)]
191209
pub fn open<P: ?Sized + NixPath>(
@@ -200,6 +218,14 @@ pub fn open<P: ?Sized + NixPath>(
200218
Errno::result(fd)
201219
}
202220

221+
/// open or create a file for reading, writing or executing
222+
///
223+
/// The `openat` function is equivalent to the [`open`] function except in the case where the path
224+
/// specifies a relative path. In that case, the file to be opened is determined relative to the
225+
/// directory associated with the file descriptor `fd`.
226+
///
227+
/// # See Also
228+
/// [`openat`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html)
203229
// The conversion is not identical on all operating systems.
204230
#[allow(clippy::useless_conversion)]
205231
#[cfg(not(target_os = "redox"))]
@@ -215,6 +241,14 @@ pub fn openat<P: ?Sized + NixPath>(
215241
Errno::result(fd)
216242
}
217243

244+
/// Change the name of a file.
245+
///
246+
/// The `renameat` function is equivalent to `rename` except in the case where either `old_path`
247+
/// or `new_path` specifies a relative path. In such cases, the file to be renamed (or the its new
248+
/// name, respectively) is located relative to `old_dirfd` or `new_dirfd`, respectively
249+
///
250+
/// # See Also
251+
/// [`renameat`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html)
218252
#[cfg(not(target_os = "redox"))]
219253
pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
220254
old_dirfd: Option<RawFd>,
@@ -239,16 +273,30 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
239273
#[cfg(all(target_os = "linux", target_env = "gnu"))]
240274
#[cfg(feature = "fs")]
241275
libc_bitflags! {
276+
/// Flags for use with [`renameat2`].
242277
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
243278
pub struct RenameFlags: u32 {
279+
/// Atomically exchange `old_path` and `new_path`.
244280
RENAME_EXCHANGE;
281+
/// Don't overwrite `new_path` of the rename. Return an error if `new_path` already
282+
/// exists.
245283
RENAME_NOREPLACE;
284+
/// creates a "whiteout" object at the source of the rename at the same time as performing
285+
/// the rename.
286+
///
287+
/// This operation makes sense only for overlay/union filesystem implementations.
246288
RENAME_WHITEOUT;
247289
}
248290
}
249291

250292
feature! {
251293
#![feature = "fs"]
294+
/// Like [`renameat`], but with an additional `flags` argument.
295+
///
296+
/// A `renameat2` call with a zero flags argument is equivalent to `renameat`.
297+
///
298+
/// # See Also
299+
/// * [`rename`](https://man7.org/linux/man-pages/man2/rename.2.html)
252300
#[cfg(all(target_os = "linux", target_env = "gnu"))]
253301
pub fn renameat2<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
254302
old_dirfd: Option<RawFd>,
@@ -389,10 +437,21 @@ fn inner_readlink<P: ?Sized + NixPath>(
389437
}
390438
}
391439

440+
/// Read value of a symbolic link
441+
///
442+
/// # See Also
443+
/// * [`readlink`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html)
392444
pub fn readlink<P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
393445
inner_readlink(None, path)
394446
}
395447

448+
/// Read value of a symbolic link.
449+
///
450+
/// Equivalent to [`readlink` ] except where `path` specifies a relative path. In that case,
451+
/// interpret `path` relative to open file specified by `dirfd`.
452+
///
453+
/// # See Also
454+
/// * [`readlink`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html)
396455
#[cfg(not(target_os = "redox"))]
397456
pub fn readlinkat<P: ?Sized + NixPath>(
398457
dirfd: Option<RawFd>,
@@ -437,69 +496,105 @@ libc_bitflags!(
437496
feature! {
438497
#![feature = "fs"]
439498

499+
/// Commands for use with [`fcntl`].
440500
#[cfg(not(target_os = "redox"))]
441501
#[derive(Debug, Eq, Hash, PartialEq)]
442502
#[non_exhaustive]
443503
pub enum FcntlArg<'a> {
504+
/// Duplicate the provided file descriptor
444505
F_DUPFD(RawFd),
506+
/// Duplicate the provided file descriptor and set the `FD_CLOEXEC` flag on it.
445507
F_DUPFD_CLOEXEC(RawFd),
508+
/// Get the close-on-exec flag associated with the file descriptor
446509
F_GETFD,
510+
/// Set the close-on-exec flag associated with the file descriptor
447511
F_SETFD(FdFlag), // FD_FLAGS
512+
/// Get descriptor status flags
448513
F_GETFL,
514+
/// Set descriptor status flags
449515
F_SETFL(OFlag), // O_NONBLOCK
516+
/// Set or clear a file segment lock
450517
F_SETLK(&'a libc::flock),
518+
/// Like [`F_SETLK`](FcntlArg::F_SETLK) except that if a shared or exclusive lock is blocked by
519+
/// other locks, the process waits until the request can be satisfied.
451520
F_SETLKW(&'a libc::flock),
521+
/// Get the first lock that blocks the lock description
452522
F_GETLK(&'a mut libc::flock),
523+
/// Acquire or release an open file description lock
453524
#[cfg(linux_android)]
454525
F_OFD_SETLK(&'a libc::flock),
526+
/// Like [`F_OFD_SETLK`](FcntlArg::F_OFD_SETLK) except that if a conflicting lock is held on
527+
/// the file, then wait for that lock to be released.
455528
#[cfg(linux_android)]
456529
F_OFD_SETLKW(&'a libc::flock),
530+
/// Determine whether it would be possible to create the given lock. If not, return details
531+
/// about one existing lock that would prevent it.
457532
#[cfg(linux_android)]
458533
F_OFD_GETLK(&'a mut libc::flock),
534+
/// Add seals to the file
459535
#[cfg(any(
460536
linux_android,
461537
target_os = "freebsd"
462538
))]
463539
F_ADD_SEALS(SealFlag),
540+
/// Get seals associated with the file
464541
#[cfg(any(
465542
linux_android,
466543
target_os = "freebsd"
467544
))]
468545
F_GET_SEALS,
546+
/// Asks the drive to flush all buffered data to permanent storage.
469547
#[cfg(apple_targets)]
470548
F_FULLFSYNC,
549+
/// fsync + issue barrier to drive
471550
#[cfg(apple_targets)]
472551
F_BARRIERFSYNC,
552+
/// Return the capacity of a pipe
473553
#[cfg(linux_android)]
474554
F_GETPIPE_SZ,
555+
/// Change the capacity of a pipe
475556
#[cfg(linux_android)]
476557
F_SETPIPE_SZ(c_int),
558+
/// Look up the path of an open file descriptor, if possible.
477559
#[cfg(any(
478560
target_os = "netbsd",
479561
target_os = "dragonfly",
480562
apple_targets,
481563
))]
482564
F_GETPATH(&'a mut PathBuf),
565+
/// Look up the path of an open file descriptor, if possible.
483566
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
484567
F_KINFO(&'a mut PathBuf),
568+
/// Return the full path without firmlinks of the fd.
485569
#[cfg(apple_targets)]
486570
F_GETPATH_NOFIRMLINK(&'a mut PathBuf),
487571
// TODO: Rest of flags
488572
}
489573

574+
/// Commands for use with [`fcntl`].
490575
#[cfg(target_os = "redox")]
491576
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
492577
#[non_exhaustive]
493578
pub enum FcntlArg {
579+
/// Duplicate the provided file descriptor
494580
F_DUPFD(RawFd),
581+
/// Duplicate the provided file descriptor and set the `FD_CLOEXEC` flag on it.
495582
F_DUPFD_CLOEXEC(RawFd),
583+
/// Get the close-on-exec flag associated with the file descriptor
496584
F_GETFD,
585+
/// Set the close-on-exec flag associated with the file descriptor
497586
F_SETFD(FdFlag), // FD_FLAGS
587+
/// Get descriptor status flags
498588
F_GETFL,
589+
/// Set descriptor status flags
499590
F_SETFL(OFlag), // O_NONBLOCK
500591
}
501592
pub use self::FcntlArg::*;
502593

594+
/// Perform various operations on open file descriptors.
595+
///
596+
/// # See Also
597+
/// * [`fcntl`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html)
503598
// TODO: Figure out how to handle value fcntl returns
504599
pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
505600
let res = unsafe {
@@ -584,17 +679,27 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
584679
Errno::result(res)
585680
}
586681

682+
/// Operations for use with [`flock`].
683+
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
587684
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
588685
#[non_exhaustive]
589686
pub enum FlockArg {
687+
/// shared file lock
590688
LockShared,
689+
/// exclusive file lock
591690
LockExclusive,
691+
/// Unlock file
592692
Unlock,
693+
/// Shared lock. Do not block when locking.
593694
LockSharedNonblock,
695+
/// Exclusive lock. Do not block when locking.
594696
LockExclusiveNonblock,
697+
#[allow(missing_docs)]
698+
#[deprecated(since = "0.28.0", note = "Use FlockArg::Unlock instead")]
595699
UnlockNonblock,
596700
}
597701

702+
#[allow(missing_docs)]
598703
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
599704
#[deprecated(since = "0.28.0", note = "`fcntl::Flock` should be used instead.")]
600705
pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
@@ -827,6 +932,10 @@ pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
827932
Errno::result(ret).map(|r| r as usize)
828933
}
829934

935+
/// Splice data to/from a pipe
936+
///
937+
/// # See Also
938+
/// *[`splice`](https://man7.org/linux/man-pages/man2/splice.2.html)
830939
#[cfg(linux_android)]
831940
pub fn splice(
832941
fd_in: RawFd,
@@ -849,6 +958,10 @@ pub fn splice(
849958
Errno::result(ret).map(|r| r as usize)
850959
}
851960

961+
/// Duplicate pipe content
962+
///
963+
/// # See Also
964+
/// *[`tee`](https://man7.org/linux/man-pages/man2/tee.2.html)
852965
#[cfg(linux_android)]
853966
pub fn tee(
854967
fd_in: RawFd,
@@ -860,6 +973,10 @@ pub fn tee(
860973
Errno::result(ret).map(|r| r as usize)
861974
}
862975

976+
/// Splice user pages to/from a pipe
977+
///
978+
/// # See Also
979+
/// *[`vmsplice`](https://man7.org/linux/man-pages/man2/vmsplice.2.html)
863980
#[cfg(linux_android)]
864981
pub fn vmsplice(
865982
fd: RawFd,
@@ -938,16 +1055,22 @@ pub struct SpacectlRange(pub libc::off_t, pub libc::off_t);
9381055

9391056
#[cfg(any(target_os = "freebsd"))]
9401057
impl SpacectlRange {
1058+
/// Is the range empty?
1059+
///
1060+
/// After a successful call to [`fspacectl`], A value of `true` for `SpacectlRange::is_empty`
1061+
/// indicates that the operation is complete.
9411062
#[inline]
9421063
pub fn is_empty(&self) -> bool {
9431064
self.1 == 0
9441065
}
9451066

1067+
/// Remaining length of the range
9461068
#[inline]
9471069
pub fn len(&self) -> libc::off_t {
9481070
self.1
9491071
}
9501072

1073+
/// Next file offset to operate on
9511074
#[inline]
9521075
pub fn offset(&self) -> libc::off_t {
9531076
self.0
@@ -1083,21 +1206,34 @@ mod posix_fadvise {
10831206

10841207
#[cfg(feature = "fs")]
10851208
libc_enum! {
1209+
/// The specific advice provided to [`posix_fadvise`].
10861210
#[repr(i32)]
10871211
#[non_exhaustive]
10881212
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
10891213
pub enum PosixFadviseAdvice {
1214+
/// Revert to the default data access behavior.
10901215
POSIX_FADV_NORMAL,
1216+
/// The file data will be accessed sequentially.
10911217
POSIX_FADV_SEQUENTIAL,
1218+
/// A hint that file data will be accessed randomly, and prefetching is likely not
1219+
/// advantageous.
10921220
POSIX_FADV_RANDOM,
1221+
/// The specified data will only be accessed once and then not reused.
10931222
POSIX_FADV_NOREUSE,
1223+
/// The specified data will be accessed in the near future.
10941224
POSIX_FADV_WILLNEED,
1225+
/// The specified data will not be accessed in the near future.
10951226
POSIX_FADV_DONTNEED,
10961227
}
10971228
}
10981229

10991230
feature! {
11001231
#![feature = "fs"]
1232+
/// Allows a process to describe to the system its data access behavior for an open file
1233+
/// descriptor.
1234+
///
1235+
/// # See Also
1236+
/// * [`posix_fadvise`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fadvise.html)
11011237
pub fn posix_fadvise(
11021238
fd: RawFd,
11031239
offset: libc::off_t,
@@ -1115,6 +1251,10 @@ mod posix_fadvise {
11151251
}
11161252
}
11171253

1254+
/// Pre-allocate storage for a range in a file
1255+
///
1256+
/// # See Also
1257+
/// * [`posix_fallocate`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html)
11181258
#[cfg(any(
11191259
linux_android,
11201260
freebsdlike,

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ feature! {
118118
#[deny(missing_docs)]
119119
pub mod features;
120120
}
121-
#[allow(missing_docs)]
122121
pub mod fcntl;
123122
feature! {
124123
#![feature = "net"]
@@ -164,7 +163,6 @@ feature! {
164163
pub mod sys;
165164
feature! {
166165
#![feature = "time"]
167-
#[allow(missing_docs)]
168166
pub mod time;
169167
}
170168
// This can be implemented for other platforms as soon as libc

0 commit comments

Comments
 (0)