1
+ //! file control options
1
2
use crate :: errno:: Errno ;
2
3
#[ cfg( all( target_os = "freebsd" , target_arch = "x86_64" ) ) ]
3
4
use core:: slice;
@@ -45,15 +46,28 @@ pub use self::posix_fadvise::{posix_fadvise, PosixFadviseAdvice};
45
46
#[ cfg( not( target_os = "redox" ) ) ]
46
47
#[ cfg( any( feature = "fs" , feature = "process" , feature = "user" ) ) ]
47
48
libc_bitflags ! {
49
+ /// Flags that control how the various *at syscalls behave.
48
50
#[ cfg_attr( docsrs, doc( cfg( any( feature = "fs" , feature = "process" ) ) ) ) ]
49
51
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.
50
54
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.
51
57
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.
52
60
AT_SYMLINK_NOFOLLOW ;
61
+ /// Don't automount the terminal ("basename") component of pathname if it is a directory
62
+ /// that is an automount point.
53
63
#[ cfg( linux_android) ]
54
64
AT_NO_AUTOMOUNT ;
65
+ /// If the provided path is an empty string, operate on the provided directory file
66
+ /// descriptor instead.
55
67
#[ cfg( any( linux_android, target_os = "freebsd" , target_os = "hurd" ) ) ]
56
68
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
57
71
#[ cfg( not( target_os = "android" ) ) ]
58
72
AT_EACCESS ;
59
73
}
@@ -186,6 +200,10 @@ pub(crate) fn at_rawfd(fd: Option<RawFd>) -> raw::c_int {
186
200
feature ! {
187
201
#![ feature = "fs" ]
188
202
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)
189
207
// The conversion is not identical on all operating systems.
190
208
#[ allow( clippy:: useless_conversion) ]
191
209
pub fn open<P : ?Sized + NixPath >(
@@ -200,6 +218,14 @@ pub fn open<P: ?Sized + NixPath>(
200
218
Errno :: result( fd)
201
219
}
202
220
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)
203
229
// The conversion is not identical on all operating systems.
204
230
#[ allow( clippy:: useless_conversion) ]
205
231
#[ cfg( not( target_os = "redox" ) ) ]
@@ -215,6 +241,14 @@ pub fn openat<P: ?Sized + NixPath>(
215
241
Errno :: result( fd)
216
242
}
217
243
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)
218
252
#[ cfg( not( target_os = "redox" ) ) ]
219
253
pub fn renameat<P1 : ?Sized + NixPath , P2 : ?Sized + NixPath >(
220
254
old_dirfd: Option <RawFd >,
@@ -239,16 +273,30 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
239
273
#[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ]
240
274
#[ cfg( feature = "fs" ) ]
241
275
libc_bitflags ! {
276
+ /// Flags for use with [`renameat2`].
242
277
#[ cfg_attr( docsrs, doc( cfg( feature = "fs" ) ) ) ]
243
278
pub struct RenameFlags : u32 {
279
+ /// Atomically exchange `old_path` and `new_path`.
244
280
RENAME_EXCHANGE ;
281
+ /// Don't overwrite `new_path` of the rename. Return an error if `new_path` already
282
+ /// exists.
245
283
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.
246
288
RENAME_WHITEOUT ;
247
289
}
248
290
}
249
291
250
292
feature ! {
251
293
#![ 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)
252
300
#[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ]
253
301
pub fn renameat2<P1 : ?Sized + NixPath , P2 : ?Sized + NixPath >(
254
302
old_dirfd: Option <RawFd >,
@@ -389,10 +437,21 @@ fn inner_readlink<P: ?Sized + NixPath>(
389
437
}
390
438
}
391
439
440
+ /// Read value of a symbolic link
441
+ ///
442
+ /// # See Also
443
+ /// * [`readlink`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html)
392
444
pub fn readlink<P : ?Sized + NixPath >( path: & P ) -> Result <OsString > {
393
445
inner_readlink( None , path)
394
446
}
395
447
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)
396
455
#[ cfg( not( target_os = "redox" ) ) ]
397
456
pub fn readlinkat<P : ?Sized + NixPath >(
398
457
dirfd: Option <RawFd >,
@@ -437,69 +496,105 @@ libc_bitflags!(
437
496
feature ! {
438
497
#![ feature = "fs" ]
439
498
499
+ /// Commands for use with [`fcntl`].
440
500
#[ cfg( not( target_os = "redox" ) ) ]
441
501
#[ derive( Debug , Eq , Hash , PartialEq ) ]
442
502
#[ non_exhaustive]
443
503
pub enum FcntlArg <' a> {
504
+ /// Duplicate the provided file descriptor
444
505
F_DUPFD ( RawFd ) ,
506
+ /// Duplicate the provided file descriptor and set the `FD_CLOEXEC` flag on it.
445
507
F_DUPFD_CLOEXEC ( RawFd ) ,
508
+ /// Get the close-on-exec flag associated with the file descriptor
446
509
F_GETFD ,
510
+ /// Set the close-on-exec flag associated with the file descriptor
447
511
F_SETFD ( FdFlag ) , // FD_FLAGS
512
+ /// Get descriptor status flags
448
513
F_GETFL ,
514
+ /// Set descriptor status flags
449
515
F_SETFL ( OFlag ) , // O_NONBLOCK
516
+ /// Set or clear a file segment lock
450
517
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.
451
520
F_SETLKW ( & ' a libc:: flock) ,
521
+ /// Get the first lock that blocks the lock description
452
522
F_GETLK ( & ' a mut libc:: flock) ,
523
+ /// Acquire or release an open file description lock
453
524
#[ cfg( linux_android) ]
454
525
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.
455
528
#[ cfg( linux_android) ]
456
529
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.
457
532
#[ cfg( linux_android) ]
458
533
F_OFD_GETLK ( & ' a mut libc:: flock) ,
534
+ /// Add seals to the file
459
535
#[ cfg( any(
460
536
linux_android,
461
537
target_os = "freebsd"
462
538
) ) ]
463
539
F_ADD_SEALS ( SealFlag ) ,
540
+ /// Get seals associated with the file
464
541
#[ cfg( any(
465
542
linux_android,
466
543
target_os = "freebsd"
467
544
) ) ]
468
545
F_GET_SEALS ,
546
+ /// Asks the drive to flush all buffered data to permanent storage.
469
547
#[ cfg( apple_targets) ]
470
548
F_FULLFSYNC ,
549
+ /// fsync + issue barrier to drive
471
550
#[ cfg( apple_targets) ]
472
551
F_BARRIERFSYNC ,
552
+ /// Return the capacity of a pipe
473
553
#[ cfg( linux_android) ]
474
554
F_GETPIPE_SZ ,
555
+ /// Change the capacity of a pipe
475
556
#[ cfg( linux_android) ]
476
557
F_SETPIPE_SZ ( c_int) ,
558
+ /// Look up the path of an open file descriptor, if possible.
477
559
#[ cfg( any(
478
560
target_os = "netbsd" ,
479
561
target_os = "dragonfly" ,
480
562
apple_targets,
481
563
) ) ]
482
564
F_GETPATH ( & ' a mut PathBuf ) ,
565
+ /// Look up the path of an open file descriptor, if possible.
483
566
#[ cfg( all( target_os = "freebsd" , target_arch = "x86_64" ) ) ]
484
567
F_KINFO ( & ' a mut PathBuf ) ,
568
+ /// Return the full path without firmlinks of the fd.
485
569
#[ cfg( apple_targets) ]
486
570
F_GETPATH_NOFIRMLINK ( & ' a mut PathBuf ) ,
487
571
// TODO: Rest of flags
488
572
}
489
573
574
+ /// Commands for use with [`fcntl`].
490
575
#[ cfg( target_os = "redox" ) ]
491
576
#[ derive( Debug , Clone , Copy , Eq , Hash , PartialEq ) ]
492
577
#[ non_exhaustive]
493
578
pub enum FcntlArg {
579
+ /// Duplicate the provided file descriptor
494
580
F_DUPFD ( RawFd ) ,
581
+ /// Duplicate the provided file descriptor and set the `FD_CLOEXEC` flag on it.
495
582
F_DUPFD_CLOEXEC ( RawFd ) ,
583
+ /// Get the close-on-exec flag associated with the file descriptor
496
584
F_GETFD ,
585
+ /// Set the close-on-exec flag associated with the file descriptor
497
586
F_SETFD ( FdFlag ) , // FD_FLAGS
587
+ /// Get descriptor status flags
498
588
F_GETFL ,
589
+ /// Set descriptor status flags
499
590
F_SETFL ( OFlag ) , // O_NONBLOCK
500
591
}
501
592
pub use self :: FcntlArg :: * ;
502
593
594
+ /// Perform various operations on open file descriptors.
595
+ ///
596
+ /// # See Also
597
+ /// * [`fcntl`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html)
503
598
// TODO: Figure out how to handle value fcntl returns
504
599
pub fn fcntl( fd: RawFd , arg: FcntlArg ) -> Result <c_int> {
505
600
let res = unsafe {
@@ -584,17 +679,27 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
584
679
Errno :: result( res)
585
680
}
586
681
682
+ /// Operations for use with [`flock`].
683
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
587
684
#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
588
685
#[ non_exhaustive]
589
686
pub enum FlockArg {
687
+ /// shared file lock
590
688
LockShared ,
689
+ /// exclusive file lock
591
690
LockExclusive ,
691
+ /// Unlock file
592
692
Unlock ,
693
+ /// Shared lock. Do not block when locking.
593
694
LockSharedNonblock ,
695
+ /// Exclusive lock. Do not block when locking.
594
696
LockExclusiveNonblock ,
697
+ #[ allow( missing_docs) ]
698
+ #[ deprecated( since = "0.28.0" , note = "Use FlockArg::Unlock instead" ) ]
595
699
UnlockNonblock ,
596
700
}
597
701
702
+ #[ allow( missing_docs) ]
598
703
#[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
599
704
#[ deprecated( since = "0.28.0" , note = "`fcntl::Flock` should be used instead." ) ]
600
705
pub fn flock( fd: RawFd , arg: FlockArg ) -> Result <( ) > {
@@ -827,6 +932,10 @@ pub fn copy_file_range<Fd1: AsFd, Fd2: AsFd>(
827
932
Errno :: result( ret) . map( |r| r as usize )
828
933
}
829
934
935
+ /// Splice data to/from a pipe
936
+ ///
937
+ /// # See Also
938
+ /// *[`splice`](https://man7.org/linux/man-pages/man2/splice.2.html)
830
939
#[ cfg( linux_android) ]
831
940
pub fn splice(
832
941
fd_in: RawFd ,
@@ -849,6 +958,10 @@ pub fn splice(
849
958
Errno :: result( ret) . map( |r| r as usize )
850
959
}
851
960
961
+ /// Duplicate pipe content
962
+ ///
963
+ /// # See Also
964
+ /// *[`tee`](https://man7.org/linux/man-pages/man2/tee.2.html)
852
965
#[ cfg( linux_android) ]
853
966
pub fn tee(
854
967
fd_in: RawFd ,
@@ -860,6 +973,10 @@ pub fn tee(
860
973
Errno :: result( ret) . map( |r| r as usize )
861
974
}
862
975
976
+ /// Splice user pages to/from a pipe
977
+ ///
978
+ /// # See Also
979
+ /// *[`vmsplice`](https://man7.org/linux/man-pages/man2/vmsplice.2.html)
863
980
#[ cfg( linux_android) ]
864
981
pub fn vmsplice(
865
982
fd: RawFd ,
@@ -938,16 +1055,22 @@ pub struct SpacectlRange(pub libc::off_t, pub libc::off_t);
938
1055
939
1056
#[ cfg( any( target_os = "freebsd" ) ) ]
940
1057
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.
941
1062
#[ inline]
942
1063
pub fn is_empty( & self ) -> bool {
943
1064
self . 1 == 0
944
1065
}
945
1066
1067
+ /// Remaining length of the range
946
1068
#[ inline]
947
1069
pub fn len( & self ) -> libc:: off_t {
948
1070
self . 1
949
1071
}
950
1072
1073
+ /// Next file offset to operate on
951
1074
#[ inline]
952
1075
pub fn offset( & self ) -> libc:: off_t {
953
1076
self . 0
@@ -1083,21 +1206,34 @@ mod posix_fadvise {
1083
1206
1084
1207
#[ cfg( feature = "fs" ) ]
1085
1208
libc_enum! {
1209
+ /// The specific advice provided to [`posix_fadvise`].
1086
1210
#[ repr( i32 ) ]
1087
1211
#[ non_exhaustive]
1088
1212
#[ cfg_attr( docsrs, doc( cfg( feature = "fs" ) ) ) ]
1089
1213
pub enum PosixFadviseAdvice {
1214
+ /// Revert to the default data access behavior.
1090
1215
POSIX_FADV_NORMAL ,
1216
+ /// The file data will be accessed sequentially.
1091
1217
POSIX_FADV_SEQUENTIAL ,
1218
+ /// A hint that file data will be accessed randomly, and prefetching is likely not
1219
+ /// advantageous.
1092
1220
POSIX_FADV_RANDOM ,
1221
+ /// The specified data will only be accessed once and then not reused.
1093
1222
POSIX_FADV_NOREUSE ,
1223
+ /// The specified data will be accessed in the near future.
1094
1224
POSIX_FADV_WILLNEED ,
1225
+ /// The specified data will not be accessed in the near future.
1095
1226
POSIX_FADV_DONTNEED ,
1096
1227
}
1097
1228
}
1098
1229
1099
1230
feature! {
1100
1231
#![ 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)
1101
1237
pub fn posix_fadvise(
1102
1238
fd: RawFd ,
1103
1239
offset: libc:: off_t,
@@ -1115,6 +1251,10 @@ mod posix_fadvise {
1115
1251
}
1116
1252
}
1117
1253
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)
1118
1258
#[ cfg( any(
1119
1259
linux_android,
1120
1260
freebsdlike,
0 commit comments