Skip to content

Commit 2b21fa6

Browse files
committed
Auto merge of #7149 - alexcrichton:maybe-no-lock, r=ehuss
Don't fail if we can't acquire readonly lock This commit updates support from #6940 to not only gracefully handle situations where the lock can be acquired in readonly but not read/write mode but also handle situations where even a readonly lock can't be acquired. If a readonly lock can't be acquired (and the read/write failed) then we likely can't touch anything in the directory, so there's no value gained from locking anyway. Closes #7147
2 parents 10776bd + 650ae65 commit 2b21fa6

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/cargo/util/config.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub struct Config {
8383
updated_sources: LazyCell<RefCell<HashSet<SourceId>>>,
8484
/// Lock, if held, of the global package cache along with the number of
8585
/// acquisitions so far.
86-
package_cache_lock: RefCell<Option<(FileLock, usize)>>,
86+
package_cache_lock: RefCell<Option<(Option<FileLock>, usize)>>,
8787
}
8888

8989
impl Config {
@@ -887,28 +887,26 @@ impl Config {
887887
// First, attempt to open an exclusive lock which is in general
888888
// the purpose of this lock!
889889
//
890-
// If that fails because of a readonly filesystem, though, then
891-
// we don't want to fail because it's a readonly filesystem. In
892-
// some situations Cargo is prepared to have a readonly
893-
// filesystem yet still work since it's all been pre-downloaded
894-
// and/or pre-unpacked. In these situations we want to keep
895-
// Cargo running if possible, so if it's a readonly filesystem
896-
// switch to a shared lock which should hopefully succeed so we
897-
// can continue.
890+
// If that fails because of a readonly filesystem or a
891+
// permission error, though, then we don't really want to fail
892+
// just because of this. All files that this lock protects are
893+
// in subfolders, so they're assumed by Cargo to also be
894+
// readonly or have invalid permissions for us to write to. If
895+
// that's the case, then we don't really need to grab a lock in
896+
// the first place here.
898897
//
899-
// Note that the package cache lock protects files in the same
900-
// directory, so if it's a readonly filesystem we assume that
901-
// the entire package cache is readonly, so we're just acquiring
902-
// something to prove it works, we're not actually doing any
903-
// synchronization at that point.
898+
// Despite this we attempt to grab a readonly lock. This means
899+
// that if our read-only folder is shared read-write with
900+
// someone else on the system we should synchronize with them,
901+
// but if we can't even do that then we did our best and we just
902+
// keep on chugging elsewhere.
904903
match self.home_path.open_rw(path, self, desc) {
905-
Ok(lock) => *slot = Some((lock, 1)),
904+
Ok(lock) => *slot = Some((Some(lock), 1)),
906905
Err(e) => {
907906
if maybe_readonly(&e) {
908-
if let Ok(lock) = self.home_path.open_ro(path, self, desc) {
909-
*slot = Some((lock, 1));
910-
return Ok(PackageCacheLock(self));
911-
}
907+
let lock = self.home_path.open_ro(path, self, desc).ok();
908+
*slot = Some((lock, 1));
909+
return Ok(PackageCacheLock(self));
912910
}
913911

914912
Err(e).chain_err(|| "failed to acquire package cache lock")?;

0 commit comments

Comments
 (0)