Skip to content

Commit 9e27acb

Browse files
David Tolnayfacebook-github-bot
David Tolnay
authored andcommitted
Disambiguate fs2 extension-trait locking calls
Summary: Some `fs2::FileExt` extension-trait methods overlap with new file locking API on `std::fs::File` in Rust 1.84 (D70601924). ```lang=text,counterexample error: a method with this name may be added to the standard library in the future --> fbcode/clifoundation/cli_target/validator/src/main.rs:279:20 | 279 | cache_file.lock_shared()?; | ^^^^^^^^^^^ | = warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! = note: for more information, see issue #48919 <rust-lang/rust#48919> = help: call with fully qualified syntax `fs4::FileExt::lock_shared(...)` to keep using the current method = note: requested on the command line with `-D unstable-name-collisions` help: add `#![feature(file_lock)]` to the crate attributes to enable `std::fs::File::lock_shared` | 1 + #![feature(file_lock)] | ``` This diff (still on Rust 1.83) qualifies all the extension-trait calls so that these do not become a factor in the Rust 1.84 diff. After D70601924, it would be reasonable to drop `fs2` and `fs4` dependencies and use the standard library APIs instead. Reviewed By: diliop Differential Revision: D70676686 fbshipit-source-id: ed9dc8fa139c56e1a6f8e2fc39b923a81e8711b3
1 parent 594065d commit 9e27acb

File tree

10 files changed

+25
-38
lines changed

10 files changed

+25
-38
lines changed

eden/scm/lib/commandserver/src/spawn.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::io;
1010
use std::process::Child;
1111
use std::process::Command;
1212

13-
use fs2::FileExt;
1413
use spawn_ext::CommandExt;
1514

1615
use crate::util;
@@ -24,7 +23,7 @@ pub fn spawn_pool(pool_size: usize) -> anyhow::Result<()> {
2423
.create(true)
2524
.write(true)
2625
.open(dir.join("spawn.lock"))?;
27-
spawn_lock.lock_exclusive()?;
26+
fs2::FileExt::lock_exclusive(&spawn_lock)?;
2827

2928
let existing = udsipc::pool::list_uds_paths(&dir, prefix)
3029
.take(pool_size)

eden/scm/lib/dag/src/iddagstore/indexedlog_store.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::sync::Mutex;
2121
use byteorder::BigEndian;
2222
use byteorder::ReadBytesExt;
2323
use byteorder::WriteBytesExt;
24-
use fs2::FileExt;
2524
use indexedlog::log;
2625
use indexedlog::log::Fold;
2726
use minibytes::Bytes;
@@ -500,7 +499,7 @@ impl Persist for IndexedLogStore {
500499
.create(true)
501500
.open(&path)?
502501
};
503-
lock_file.lock_exclusive()?;
502+
fs2::FileExt::lock_exclusive(&lock_file)?;
504503
Ok(lock_file)
505504
}
506505

eden/scm/lib/dag/src/idmap/indexedlog_idmap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::path::PathBuf;
1313

1414
use byteorder::BigEndian;
1515
use byteorder::ReadBytesExt;
16-
use fs2::FileExt;
1716
use indexedlog::log;
1817
use vlqencoding::VLQDecode;
1918
use vlqencoding::VLQEncode;
@@ -520,7 +519,7 @@ impl Persist for IdMap {
520519
.create(true)
521520
.open(&path)?
522521
};
523-
lock_file.lock_exclusive()?;
522+
fs2::FileExt::lock_exclusive(&lock_file)?;
524523
Ok(lock_file)
525524
}
526525

eden/scm/lib/indexedlog/src/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use byteorder::ByteOrder;
8989
use byteorder::LittleEndian;
9090
use byteorder::ReadBytesExt;
9191
use byteorder::WriteBytesExt;
92-
use fs2::FileExt;
92+
use fs2::FileExt as _;
9393
use minibytes::Bytes;
9494
use tracing::debug_span;
9595
use twox_hash::XxHash;

eden/scm/lib/indexedlog/src/lock.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::io;
1111
use std::path::Path;
1212
use std::path::PathBuf;
1313

14-
use fs2::FileExt;
1514
use memmap2::MmapMut;
1615
use memmap2::MmapOptions;
1716

@@ -27,9 +26,9 @@ pub struct ScopedFileLock<'a> {
2726
impl<'a> ScopedFileLock<'a> {
2827
pub fn new(file: &'a mut File, exclusive: bool) -> io::Result<Self> {
2928
if exclusive {
30-
file.lock_exclusive()?;
29+
fs2::FileExt::lock_exclusive(file)?;
3130
} else {
32-
file.lock_shared()?;
31+
fs2::FileExt::lock_shared(file)?;
3332
}
3433
Ok(ScopedFileLock { file })
3534
}
@@ -49,7 +48,7 @@ impl<'a> AsMut<File> for ScopedFileLock<'a> {
4948

5049
impl<'a> Drop for ScopedFileLock<'a> {
5150
fn drop(&mut self) {
52-
self.file.unlock().expect("unlock");
51+
fs2::FileExt::unlock(self.file).expect("unlock");
5352
}
5453
}
5554

@@ -241,10 +240,10 @@ fn lock_file(file: &File, exclusive: bool, non_blocking: bool) -> io::Result<()>
241240
}
242241
#[cfg(not(windows))]
243242
match (exclusive, non_blocking) {
244-
(true, false) => file.lock_exclusive()?,
245-
(true, true) => file.try_lock_exclusive()?,
246-
(false, false) => file.lock_shared()?,
247-
(false, true) => file.try_lock_shared()?,
243+
(true, false) => fs2::FileExt::lock_exclusive(file)?,
244+
(true, true) => fs2::FileExt::try_lock_exclusive(file)?,
245+
(false, false) => fs2::FileExt::lock_shared(file)?,
246+
(false, true) => fs2::FileExt::try_lock_shared(file)?,
248247
}
249248
Ok(())
250249
}
@@ -264,7 +263,7 @@ fn unlock_file(file: &File) -> io::Result<()> {
264263
}
265264
#[cfg(not(windows))]
266265
{
267-
file.unlock()?;
266+
fs2::FileExt::unlock(file)?;
268267
}
269268
Ok(())
270269
}

eden/scm/lib/indexedlog/src/log/repair.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl OpenOptions {
106106
Ok(meta) => {
107107
// If metadata can be read, trust it.
108108
if meta.primary_len > primary_len {
109-
use fs2::FileExt;
109+
use fs2::FileExt as _;
110110
// Log was truncated for some reason...
111111
// (This should be relatively rare)
112112
// Fill Log with 0s.

eden/scm/lib/repolock/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use std::time::Instant;
2525

2626
use configmodel::Config;
2727
use configmodel::ConfigExt;
28-
use fs2::FileExt;
2928
use parking_lot::Mutex;
3029
use progress_model::ProgressBar;
3130
use sysutil::hostname;
@@ -438,8 +437,8 @@ pub fn try_lock(dir: &Path, name: &str, contents: &[u8]) -> Result<LockHandle, L
438437
#[cfg(unix)]
439438
let _ = lock_file.set_permissions(Permissions::from_mode(0o666));
440439

441-
match lock_file.try_lock_exclusive() {
442-
Ok(_) => {}
440+
match fs2::FileExt::try_lock_exclusive(&lock_file) {
441+
Ok(()) => {}
443442
Err(err) if err.kind() == fs2::lock_contended_error().kind() => {
444443
let contents = fs_err::read(&lock_paths.data)?;
445444
return Err(LockContendedError {
@@ -493,9 +492,7 @@ pub struct LockHandle {
493492
impl LockHandle {
494493
pub fn unlock(&mut self) -> io::Result<()> {
495494
self.unlink_legacy();
496-
self.lock
497-
.unlock()
498-
.path_context("error unlocking lock file", &self.path)
495+
fs2::FileExt::unlock(&self.lock).path_context("error unlocking lock file", &self.path)
499496
}
500497

501498
fn unlink_legacy(&mut self) {

eden/scm/lib/runlog/src/filestore.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::time::SystemTime;
1515

1616
use anyhow::Error;
1717
use anyhow::Result;
18-
use fs2::FileExt;
1918
use util::lock::PathLock;
2019
use util::path::create_shared_dir;
2120

@@ -168,7 +167,7 @@ fn remove_file_ignore_missing<P: AsRef<Path>>(path: P) -> io::Result<()> {
168167
// locked (by running command). Return false if lock file doesn't exist.
169168
fn is_locked<P: AsRef<Path>>(path: P) -> Result<bool> {
170169
match fs::File::open(path.as_ref().with_extension(LOCK_EXT)) {
171-
Ok(f) => Ok(f.try_lock_shared().is_err()),
170+
Ok(f) => Ok(fs2::FileExt::try_lock_shared(&f).is_err()),
172171
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(false),
173172
Err(err) => Err(Error::new(err)),
174173
}

eden/scm/lib/treestate/src/filereadwrite.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use std::io::SeekFrom;
1515
use std::io::Write;
1616
use std::path::Path;
1717

18-
use fs2::FileExt; // fs2 requires StdFile
19-
2018
pub trait FileSync {
2119
fn sync_all(&mut self) -> io::Result<()>;
2220
}
@@ -111,8 +109,8 @@ impl FileLock for FileReaderWriter {
111109
fn lock_exclusive(&mut self) -> io::Result<()> {
112110
if self.locked == 0 {
113111
match self.lock_file.as_mut() {
114-
Some(file) => file.lock_exclusive()?,
115-
None => self.writer.get_mut().lock_exclusive()?,
112+
Some(file) => fs2::FileExt::lock_exclusive(file)?,
113+
None => fs2::FileExt::lock_exclusive(self.writer.get_mut())?,
116114
}
117115
}
118116
self.locked += 1;
@@ -122,8 +120,8 @@ impl FileLock for FileReaderWriter {
122120
fn unlock(&mut self) -> io::Result<()> {
123121
if self.locked == 1 {
124122
match self.lock_file.as_mut() {
125-
Some(file) => file.unlock()?,
126-
None => self.writer.get_mut().unlock()?,
123+
Some(file) => fs2::FileExt::unlock(file)?,
124+
None => fs2::FileExt::unlock(self.writer.get_mut())?,
127125
}
128126
}
129127
if self.locked > 0 {
@@ -195,15 +193,15 @@ impl FileSync for MemReaderWriter {
195193
impl FileLock for MemReaderWriter {
196194
fn lock_exclusive(&mut self) -> io::Result<()> {
197195
if self.locked == 0 {
198-
self.lock_file.lock_exclusive()?;
196+
fs2::FileExt::lock_exclusive(&self.lock_file)?;
199197
}
200198
self.locked += 1;
201199
Ok(())
202200
}
203201

204202
fn unlock(&mut self) -> io::Result<()> {
205203
if self.locked == 1 {
206-
self.lock_file.unlock()?;
204+
fs2::FileExt::unlock(&self.lock_file)?;
207205
}
208206
if self.locked > 0 {
209207
self.locked -= 1;

eden/scm/lib/util/src/lock.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use std::fs::File;
99
use std::io;
1010
use std::path::Path;
1111

12-
use fs2::FileExt;
13-
1412
use crate::errors::IOContext;
1513
use crate::file::open;
1614

@@ -25,8 +23,7 @@ impl PathLock {
2523
/// demand.
2624
pub fn exclusive<P: AsRef<Path>>(path: P) -> io::Result<Self> {
2725
let file = open(path.as_ref(), "wc").io_context("lock file")?;
28-
file.lock_exclusive()
29-
.path_context("error locking file", path.as_ref())?;
26+
fs2::FileExt::lock_exclusive(&file).path_context("error locking file", path.as_ref())?;
3027
Ok(PathLock { file })
3128
}
3229

@@ -37,7 +34,7 @@ impl PathLock {
3734

3835
impl Drop for PathLock {
3936
fn drop(&mut self) {
40-
self.file.unlock().expect("unlock");
37+
fs2::FileExt::unlock(&self.file).expect("unlock");
4138
}
4239
}
4340

0 commit comments

Comments
 (0)