Skip to content

Commit 5a942d6

Browse files
authored
Rollup merge of rust-lang#136876 - joshtriplett:locking-might-not-be-advisory, r=Amanieu
Locking documentation updates - Reword file lock documentation to clarify advisory vs mandatory. Remove the word "advisory", and make it more explicit that the lock may be advisory or mandatory depending on platform. - Document that locking a file fails on Windows if the file is opened only for append
2 parents 58a622b + ec2034d commit 5a942d6

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

Diff for: library/std/src/fs.rs

+58-38
Original file line numberDiff line numberDiff line change
@@ -624,20 +624,20 @@ impl File {
624624
self.inner.datasync()
625625
}
626626

627-
/// Acquire an exclusive advisory lock on the file. Blocks until the lock can be acquired.
627+
/// Acquire an exclusive lock on the file. Blocks until the lock can be acquired.
628628
///
629-
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire
630-
/// another lock.
629+
/// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
631630
///
632-
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock the exact
633-
/// behavior is unspecified and platform dependent, including the possibility that it will
634-
/// deadlock. However, if this method returns, then an exclusive lock is held.
631+
/// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
632+
/// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
633+
/// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
634+
/// cause non-lockholders to block.
635635
///
636-
/// If the file not open for writing, it is unspecified whether this function returns an error.
636+
/// If this file handle/descriptor, or a clone of it, already holds an lock the exact behavior
637+
/// is unspecified and platform dependent, including the possibility that it will deadlock.
638+
/// However, if this method returns, then an exclusive lock is held.
637639
///
638-
/// Note, this is an advisory lock meant to interact with [`lock_shared`], [`try_lock`],
639-
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
640-
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
640+
/// If the file not open for writing, it is unspecified whether this function returns an error.
641641
///
642642
/// The lock will be released when this file (along with any other file descriptors/handles
643643
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
@@ -648,8 +648,12 @@ impl File {
648648
/// and the `LockFileEx` function on Windows with the `LOCKFILE_EXCLUSIVE_LOCK` flag. Note that,
649649
/// this [may change in the future][changes].
650650
///
651+
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
652+
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
653+
///
651654
/// [changes]: io#platform-specific-behavior
652655
///
656+
/// [`lock`]: File::lock
653657
/// [`lock_shared`]: File::lock_shared
654658
/// [`try_lock`]: File::try_lock
655659
/// [`try_lock_shared`]: File::try_lock_shared
@@ -674,18 +678,19 @@ impl File {
674678
self.inner.lock()
675679
}
676680

677-
/// Acquire a shared (non-exclusive) advisory lock on the file. Blocks until the lock can be acquired.
681+
/// Acquire a shared (non-exclusive) lock on the file. Blocks until the lock can be acquired.
678682
///
679-
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
680-
/// none may hold an exclusive lock at the same time.
683+
/// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
684+
/// hold an exclusive lock at the same time.
681685
///
682-
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact
683-
/// behavior is unspecified and platform dependent, including the possibility that it will
684-
/// deadlock. However, if this method returns, then a shared lock is held.
686+
/// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
687+
/// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
688+
/// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
689+
/// cause non-lockholders to block.
685690
///
686-
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
687-
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
688-
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
691+
/// If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavior
692+
/// is unspecified and platform dependent, including the possibility that it will deadlock.
693+
/// However, if this method returns, then a shared lock is held.
689694
///
690695
/// The lock will be released when this file (along with any other file descriptors/handles
691696
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
@@ -696,9 +701,13 @@ impl File {
696701
/// and the `LockFileEx` function on Windows. Note that, this
697702
/// [may change in the future][changes].
698703
///
704+
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
705+
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
706+
///
699707
/// [changes]: io#platform-specific-behavior
700708
///
701709
/// [`lock`]: File::lock
710+
/// [`lock_shared`]: File::lock_shared
702711
/// [`try_lock`]: File::try_lock
703712
/// [`try_lock_shared`]: File::try_lock_shared
704713
/// [`unlock`]: File::unlock
@@ -722,24 +731,23 @@ impl File {
722731
self.inner.lock_shared()
723732
}
724733

725-
/// Try to acquire an exclusive advisory lock on the file.
734+
/// Try to acquire an exclusive lock on the file.
726735
///
727736
/// Returns `Ok(false)` if a different lock is already held on this file (via another
728737
/// handle/descriptor).
729738
///
730-
/// This acquires an exclusive advisory lock; no other file handle to this file may acquire
731-
/// another lock.
739+
/// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
732740
///
733-
/// If this file handle/descriptor, or a clone of it, already holds an advisory lock, the exact
734-
/// behavior is unspecified and platform dependent, including the possibility that it will
735-
/// deadlock. However, if this method returns `Ok(true)`, then it has acquired an exclusive
736-
/// lock.
741+
/// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
742+
/// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
743+
/// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
744+
/// cause non-lockholders to block.
737745
///
738-
/// If the file not open for writing, it is unspecified whether this function returns an error.
746+
/// If this file handle/descriptor, or a clone of it, already holds an lock, the exact behavior
747+
/// is unspecified and platform dependent, including the possibility that it will deadlock.
748+
/// However, if this method returns `Ok(true)`, then it has acquired an exclusive lock.
739749
///
740-
/// Note, this is an advisory lock meant to interact with [`lock`], [`lock_shared`],
741-
/// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
742-
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
750+
/// If the file not open for writing, it is unspecified whether this function returns an error.
743751
///
744752
/// The lock will be released when this file (along with any other file descriptors/handles
745753
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
@@ -751,10 +759,14 @@ impl File {
751759
/// and `LOCKFILE_FAIL_IMMEDIATELY` flags. Note that, this
752760
/// [may change in the future][changes].
753761
///
762+
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
763+
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
764+
///
754765
/// [changes]: io#platform-specific-behavior
755766
///
756767
/// [`lock`]: File::lock
757768
/// [`lock_shared`]: File::lock_shared
769+
/// [`try_lock`]: File::try_lock
758770
/// [`try_lock_shared`]: File::try_lock_shared
759771
/// [`unlock`]: File::unlock
760772
/// [`read`]: Read::read
@@ -777,22 +789,23 @@ impl File {
777789
self.inner.try_lock()
778790
}
779791

780-
/// Try to acquire a shared (non-exclusive) advisory lock on the file.
792+
/// Try to acquire a shared (non-exclusive) lock on the file.
781793
///
782794
/// Returns `Ok(false)` if an exclusive lock is already held on this file (via another
783795
/// handle/descriptor).
784796
///
785-
/// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
786-
/// none may hold an exclusive lock at the same time.
797+
/// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
798+
/// hold an exclusive lock at the same time.
787799
///
788-
/// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
800+
/// This lock may be advisory or mandatory. This lock is meant to interact with [`lock`],
801+
/// [`try_lock`], [`lock_shared`], [`try_lock_shared`], and [`unlock`]. Its interactions with
802+
/// other methods, such as [`read`] and [`write`] are platform specific, and it may or may not
803+
/// cause non-lockholders to block.
804+
///
805+
/// If this file handle, or a clone of it, already holds an lock, the exact behavior is
789806
/// unspecified and platform dependent, including the possibility that it will deadlock.
790807
/// However, if this method returns `Ok(true)`, then it has acquired a shared lock.
791808
///
792-
/// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
793-
/// [`try_lock`], and [`unlock`]. Its interactions with other methods, such as [`read`]
794-
/// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
795-
///
796809
/// The lock will be released when this file (along with any other file descriptors/handles
797810
/// duplicated or inherited from it) is closed, or if the [`unlock`] method is called.
798811
///
@@ -803,11 +816,15 @@ impl File {
803816
/// `LOCKFILE_FAIL_IMMEDIATELY` flag. Note that, this
804817
/// [may change in the future][changes].
805818
///
819+
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
820+
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
821+
///
806822
/// [changes]: io#platform-specific-behavior
807823
///
808824
/// [`lock`]: File::lock
809825
/// [`lock_shared`]: File::lock_shared
810826
/// [`try_lock`]: File::try_lock
827+
/// [`try_lock_shared`]: File::try_lock_shared
811828
/// [`unlock`]: File::unlock
812829
/// [`read`]: Read::read
813830
/// [`write`]: Write::write
@@ -844,6 +861,9 @@ impl File {
844861
/// and the `UnlockFile` function on Windows. Note that, this
845862
/// [may change in the future][changes].
846863
///
864+
/// On Windows, locking a file will fail if the file is opened only for append. To lock a file,
865+
/// open it with one of `.read(true)`, `.read(true).append(true)`, or `.write(true)`.
866+
///
847867
/// [changes]: io#platform-specific-behavior
848868
///
849869
/// # Examples

0 commit comments

Comments
 (0)