@@ -615,6 +615,181 @@ impl File {
615
615
self . inner . datasync ( )
616
616
}
617
617
618
+ /// Acquire an exclusive advisory lock on the file. Blocks until the lock can be acquired.
619
+ ///
620
+ /// This acquires an exclusive advisory lock; no other file handle to this file may acquire
621
+ /// another lock.
622
+ ///
623
+ /// If this file handle already holds an advisory lock the exact behavior is
624
+ /// unspecified and platform dependent, including the possibility that it will deadlock.
625
+ /// However, if this method returns, then an exclusive lock is held.
626
+ ///
627
+ /// If the file not open for writing, it is unspecified whether this function returns an error.
628
+ ///
629
+ /// Note, this is an advisory lock meant to interact with [`lock_shared`], [`try_lock`],
630
+ /// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
631
+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
632
+ ///
633
+ /// [`lock_shared`]: File::lock_shared
634
+ /// [`try_lock`]: File::try_lock
635
+ /// [`try_lock_shared`]: File::try_lock_shared
636
+ /// [`unlock`]: File::unlock
637
+ /// [`read`]: Read::read
638
+ /// [`write`]: Write::write
639
+ ///
640
+ /// # Examples
641
+ ///
642
+ /// ```no_run
643
+ /// #![feature(file_lock)]
644
+ /// use std::fs::File;
645
+ ///
646
+ /// fn main() -> std::io::Result<()> {
647
+ /// let f = File::open("foo.txt")?;
648
+ /// f.lock()?;
649
+ /// Ok(())
650
+ /// }
651
+ /// ```
652
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
653
+ pub fn lock ( & self ) -> io:: Result < ( ) > {
654
+ self . inner . lock ( )
655
+ }
656
+
657
+ /// Acquire a shared advisory lock on the file. Blocks until the lock can be acquired.
658
+ ///
659
+ /// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
660
+ /// none may hold an exclusive lock.
661
+ ///
662
+ /// If this file handle already holds an advisory lock, the exact behavior is unspecified and
663
+ /// platform dependent, including the possibility that it will deadlock.
664
+ /// However, if this method returns, then a shared lock is held.
665
+ ///
666
+ /// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
667
+ /// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
668
+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
669
+ ///
670
+ /// [`lock`]: File::lock
671
+ /// [`try_lock`]: File::try_lock
672
+ /// [`try_lock_shared`]: File::try_lock_shared
673
+ /// [`unlock`]: File::unlock
674
+ /// [`read`]: Read::read
675
+ /// [`write`]: Write::write
676
+ ///
677
+ /// # Examples
678
+ ///
679
+ /// ```no_run
680
+ /// #![feature(file_lock)]
681
+ /// use std::fs::File;
682
+ ///
683
+ /// fn main() -> std::io::Result<()> {
684
+ /// let f = File::open("foo.txt")?;
685
+ /// f.lock_shared()?;
686
+ /// Ok(())
687
+ /// }
688
+ /// ```
689
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
690
+ pub fn lock_shared ( & self ) -> io:: Result < ( ) > {
691
+ self . inner . lock_shared ( )
692
+ }
693
+
694
+ /// Acquire an exclusive advisory lock on the file. Returns `Ok(false)` if the file is locked.
695
+ ///
696
+ /// This acquires an exclusive advisory lock; no other file handle to this file may acquire
697
+ /// another lock.
698
+ ///
699
+ /// If this file handle already holds an advisory lock, the exact behavior is
700
+ /// unspecified and platform dependent, including the possibility that it will deadlock.
701
+ /// However, if this method returns, then an exclusive lock is held.
702
+ ///
703
+ /// If the file not open for writing, it is unspecified whether this function returns an error.
704
+ ///
705
+ /// Note, this is an advisory lock meant to interact with [`lock`], [`lock_shared`],
706
+ /// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
707
+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
708
+ ///
709
+ /// [`lock`]: File::lock
710
+ /// [`lock_shared`]: File::lock_shared
711
+ /// [`try_lock_shared`]: File::try_lock_shared
712
+ /// [`unlock`]: File::unlock
713
+ /// [`read`]: Read::read
714
+ /// [`write`]: Write::write
715
+ ///
716
+ /// # Examples
717
+ ///
718
+ /// ```no_run
719
+ /// #![feature(file_lock)]
720
+ /// use std::fs::File;
721
+ ///
722
+ /// fn main() -> std::io::Result<()> {
723
+ /// let f = File::open("foo.txt")?;
724
+ /// f.try_lock()?;
725
+ /// Ok(())
726
+ /// }
727
+ /// ```
728
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
729
+ pub fn try_lock ( & self ) -> io:: Result < bool > {
730
+ self . inner . try_lock ( )
731
+ }
732
+
733
+ /// Acquire a shared advisory lock on the file.
734
+ /// Returns `Ok(false)` if the file is exclusively locked.
735
+ ///
736
+ /// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
737
+ /// none may hold an exclusive lock.
738
+ ///
739
+ /// If this file handle already holds an advisory lock, the exact behavior is unspecified and
740
+ /// platform dependent, including the possibility that it will deadlock.
741
+ /// However, if this method returns, then a shared lock is held.
742
+ ///
743
+ /// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
744
+ /// [`try_lock`], and [`unlock`]. Its interactions with other methods, such as [`read`]
745
+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
746
+ ///
747
+ /// [`lock`]: File::lock
748
+ /// [`lock_shared`]: File::lock_shared
749
+ /// [`try_lock`]: File::try_lock
750
+ /// [`unlock`]: File::unlock
751
+ /// [`read`]: Read::read
752
+ /// [`write`]: Write::write
753
+ ///
754
+ /// # Examples
755
+ ///
756
+ /// ```no_run
757
+ /// #![feature(file_lock)]
758
+ /// use std::fs::File;
759
+ ///
760
+ /// fn main() -> std::io::Result<()> {
761
+ /// let f = File::open("foo.txt")?;
762
+ /// f.try_lock_shared()?;
763
+ /// Ok(())
764
+ /// }
765
+ /// ```
766
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
767
+ pub fn try_lock_shared ( & self ) -> io:: Result < bool > {
768
+ self . inner . try_lock_shared ( )
769
+ }
770
+
771
+ /// Release all locks on the file.
772
+ ///
773
+ /// All remaining locks are released when the file handle, and all clones of it, are dropped.
774
+ ///
775
+ /// # Examples
776
+ ///
777
+ /// ```no_run
778
+ /// #![feature(file_lock)]
779
+ /// use std::fs::File;
780
+ ///
781
+ /// fn main() -> std::io::Result<()> {
782
+ /// let f = File::open("foo.txt")?;
783
+ /// f.lock()?;
784
+ /// f.unlock()?;
785
+ /// Ok(())
786
+ /// }
787
+ /// ```
788
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
789
+ pub fn unlock ( & self ) -> io:: Result < ( ) > {
790
+ self . inner . unlock ( )
791
+ }
792
+
618
793
/// Truncates or extends the underlying file, updating the size of
619
794
/// this file to become `size`.
620
795
///
0 commit comments