Skip to content

Commit 27e439c

Browse files
committed
Open crate files readonly first
This allows Cargo to work with read-only `CARGO_HOME` directories where the cache was prepared ahead of time. Closes #3256
1 parent 8709867 commit 27e439c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/cargo/sources/registry/remote.rs

+13
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
116116
-> CargoResult<FileLock> {
117117
let filename = format!("{}-{}.crate", pkg.name(), pkg.version());
118118
let path = Path::new(&filename);
119+
120+
// Attempt to open an read-only copy first to avoid an exclusive write
121+
// lock and also work with read-only filesystems. Note that we check the
122+
// length of the file like below to handle interrupted downloads.
123+
//
124+
// If this fails then we fall through to the exclusive path where we may
125+
// have to redownload the file.
126+
if let Ok(dst) = self.cache_path.open_ro(path, self.config, &filename) {
127+
let meta = try!(dst.file().metadata());
128+
if meta.len() > 0 {
129+
return Ok(dst)
130+
}
131+
}
119132
let mut dst = try!(self.cache_path.open_rw(path, self.config, &filename));
120133
let meta = try!(dst.file().metadata());
121134
if meta.len() > 0 {

0 commit comments

Comments
 (0)