Skip to content

Commit

Permalink
Auto merge of #3259 - alexcrichton:more-ro, r=brson
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors authored Nov 8, 2016
2 parents a8510bb + 27e439c commit 0c155a4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cargo/sources/registry/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
-> CargoResult<FileLock> {
let filename = format!("{}-{}.crate", pkg.name(), pkg.version());
let path = Path::new(&filename);

// Attempt to open an read-only copy first to avoid an exclusive write
// lock and also work with read-only filesystems. Note that we check the
// length of the file like below to handle interrupted downloads.
//
// If this fails then we fall through to the exclusive path where we may
// have to redownload the file.
if let Ok(dst) = self.cache_path.open_ro(path, self.config, &filename) {
let meta = try!(dst.file().metadata());
if meta.len() > 0 {
return Ok(dst)
}
}
let mut dst = try!(self.cache_path.open_rw(path, self.config, &filename));
let meta = try!(dst.file().metadata());
if meta.len() > 0 {
Expand Down

0 comments on commit 0c155a4

Please sign in to comment.