@@ -615,6 +615,223 @@ impl File {
615615 self . inner . datasync ( )
616616 }
617617
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, or a clone of it, 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+ /// # Platform-specific behavior
634+ ///
635+ /// This function currently corresponds to the `flock` function on Unix with the `LOCK_EX` flag,
636+ /// and the `LockFileEx` function on Windows with the `LOCKFILE_EXCLUSIVE_LOCK` flag. Note that,
637+ /// this [may change in the future][changes].
638+ ///
639+ /// [changes]: io#platform-specific-behavior
640+ ///
641+ /// [`lock_shared`]: File::lock_shared
642+ /// [`try_lock`]: File::try_lock
643+ /// [`try_lock_shared`]: File::try_lock_shared
644+ /// [`unlock`]: File::unlock
645+ /// [`read`]: Read::read
646+ /// [`write`]: Write::write
647+ ///
648+ /// # Examples
649+ ///
650+ /// ```no_run
651+ /// #![feature(file_lock)]
652+ /// use std::fs::File;
653+ ///
654+ /// fn main() -> std::io::Result<()> {
655+ /// let f = File::open("foo.txt")?;
656+ /// f.lock()?;
657+ /// Ok(())
658+ /// }
659+ /// ```
660+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
661+ pub fn lock ( & self ) -> io:: Result < ( ) > {
662+ self . inner . lock ( )
663+ }
664+
665+ /// Acquire a shared advisory lock on the file. Blocks until the lock can be acquired.
666+ ///
667+ /// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
668+ /// none may hold an exclusive lock.
669+ ///
670+ /// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
671+ /// unspecified and platform dependent, including the possibility that it will deadlock.
672+ /// However, if this method returns, then a shared lock is held.
673+ ///
674+ /// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
675+ /// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
676+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
677+ ///
678+ /// # Platform-specific behavior
679+ ///
680+ /// This function currently corresponds to the `flock` function on Unix with the `LOCK_SH` flag,
681+ /// and the `LockFileEx` function on Windows. Note that, this
682+ /// [may change in the future][changes].
683+ ///
684+ /// [changes]: io#platform-specific-behavior
685+ ///
686+ /// [`lock`]: File::lock
687+ /// [`try_lock`]: File::try_lock
688+ /// [`try_lock_shared`]: File::try_lock_shared
689+ /// [`unlock`]: File::unlock
690+ /// [`read`]: Read::read
691+ /// [`write`]: Write::write
692+ ///
693+ /// # Examples
694+ ///
695+ /// ```no_run
696+ /// #![feature(file_lock)]
697+ /// use std::fs::File;
698+ ///
699+ /// fn main() -> std::io::Result<()> {
700+ /// let f = File::open("foo.txt")?;
701+ /// f.lock_shared()?;
702+ /// Ok(())
703+ /// }
704+ /// ```
705+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
706+ pub fn lock_shared ( & self ) -> io:: Result < ( ) > {
707+ self . inner . lock_shared ( )
708+ }
709+
710+ /// Acquire an exclusive advisory lock on the file. Returns `Ok(false)` if the file is locked.
711+ ///
712+ /// This acquires an exclusive advisory lock; no other file handle to this file may acquire
713+ /// another lock.
714+ ///
715+ /// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
716+ /// unspecified and platform dependent, including the possibility that it will deadlock.
717+ /// However, if this method returns, then an exclusive lock is held.
718+ ///
719+ /// If the file not open for writing, it is unspecified whether this function returns an error.
720+ ///
721+ /// Note, this is an advisory lock meant to interact with [`lock`], [`lock_shared`],
722+ /// [`try_lock_shared`], and [`unlock`]. Its interactions with other methods, such as [`read`]
723+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
724+ ///
725+ /// # Platform-specific behavior
726+ ///
727+ /// This function currently corresponds to the `flock` function on Unix with the `LOCK_EX` and
728+ /// `LOCK_NB` flags, and the `LockFileEx` function on Windows with the `LOCKFILE_EXCLUSIVE_LOCK`
729+ /// and `LOCKFILE_FAIL_IMMEDIATELY` flags. Note that, this
730+ /// [may change in the future][changes].
731+ ///
732+ /// [changes]: io#platform-specific-behavior
733+ ///
734+ /// [`lock`]: File::lock
735+ /// [`lock_shared`]: File::lock_shared
736+ /// [`try_lock_shared`]: File::try_lock_shared
737+ /// [`unlock`]: File::unlock
738+ /// [`read`]: Read::read
739+ /// [`write`]: Write::write
740+ ///
741+ /// # Examples
742+ ///
743+ /// ```no_run
744+ /// #![feature(file_lock)]
745+ /// use std::fs::File;
746+ ///
747+ /// fn main() -> std::io::Result<()> {
748+ /// let f = File::open("foo.txt")?;
749+ /// f.try_lock()?;
750+ /// Ok(())
751+ /// }
752+ /// ```
753+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
754+ pub fn try_lock ( & self ) -> io:: Result < bool > {
755+ self . inner . try_lock ( )
756+ }
757+
758+ /// Acquire a shared advisory lock on the file.
759+ /// Returns `Ok(false)` if the file is exclusively locked.
760+ ///
761+ /// This acquires a shared advisory lock; more than one file handle may hold a shared lock, but
762+ /// none may hold an exclusive lock.
763+ ///
764+ /// If this file handle, or a clone of it, already holds an advisory lock, the exact behavior is
765+ /// unspecified and platform dependent, including the possibility that it will deadlock.
766+ /// However, if this method returns, then a shared lock is held.
767+ ///
768+ /// Note, this is an advisory lock meant to interact with [`lock`], [`try_lock`],
769+ /// [`try_lock`], and [`unlock`]. Its interactions with other methods, such as [`read`]
770+ /// and [`write`] are platform specific, and it may or may not cause non-lockholders to block.
771+ ///
772+ /// # Platform-specific behavior
773+ ///
774+ /// This function currently corresponds to the `flock` function on Unix with the `LOCK_SH` and
775+ /// `LOCK_NB` flags, and the `LockFileEx` function on Windows with the
776+ /// `LOCKFILE_FAIL_IMMEDIATELY` flag. Note that, this
777+ /// [may change in the future][changes].
778+ ///
779+ /// [changes]: io#platform-specific-behavior
780+ ///
781+ /// [`lock`]: File::lock
782+ /// [`lock_shared`]: File::lock_shared
783+ /// [`try_lock`]: File::try_lock
784+ /// [`unlock`]: File::unlock
785+ /// [`read`]: Read::read
786+ /// [`write`]: Write::write
787+ ///
788+ /// # Examples
789+ ///
790+ /// ```no_run
791+ /// #![feature(file_lock)]
792+ /// use std::fs::File;
793+ ///
794+ /// fn main() -> std::io::Result<()> {
795+ /// let f = File::open("foo.txt")?;
796+ /// f.try_lock_shared()?;
797+ /// Ok(())
798+ /// }
799+ /// ```
800+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
801+ pub fn try_lock_shared ( & self ) -> io:: Result < bool > {
802+ self . inner . try_lock_shared ( )
803+ }
804+
805+ /// Release all locks on the file.
806+ ///
807+ /// All remaining locks are released when the file handle, and all clones of it, are dropped.
808+ ///
809+ /// # Platform-specific behavior
810+ ///
811+ /// This function currently corresponds to the `flock` function on Unix with the `LOCK_UN` flag,
812+ /// and the `UnlockFile` function on Windows. Note that, this
813+ /// [may change in the future][changes].
814+ ///
815+ /// [changes]: io#platform-specific-behavior
816+ ///
817+ /// # Examples
818+ ///
819+ /// ```no_run
820+ /// #![feature(file_lock)]
821+ /// use std::fs::File;
822+ ///
823+ /// fn main() -> std::io::Result<()> {
824+ /// let f = File::open("foo.txt")?;
825+ /// f.lock()?;
826+ /// f.unlock()?;
827+ /// Ok(())
828+ /// }
829+ /// ```
830+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
831+ pub fn unlock ( & self ) -> io:: Result < ( ) > {
832+ self . inner . unlock ( )
833+ }
834+
618835 /// Truncates or extends the underlying file, updating the size of
619836 /// this file to become `size`.
620837 ///
0 commit comments