Skip to content

Adds flock for concurrent access #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ is_executable = "0.1.2"
siphasher = "0.2.3"
tar = "0.4.16"
zip = "0.5.0"
fs2 = "0.4.3"

[dev-dependencies]
tempfile = "3.0.5"
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ extern crate curl;
extern crate failure;
extern crate dirs;
extern crate flate2;
extern crate fs2;
extern crate hex;
extern crate is_executable;
extern crate siphasher;
extern crate tar;
extern crate zip;

use failure::{Error, ResultExt};
use fs2::FileExt;
use siphasher::sip::SipHasher13;
use std::collections::HashSet;
use std::env;
use std::ffi;
use std::fs;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -48,6 +51,9 @@ impl Cache {
Some(home.join(&cache_name))
})
.ok_or_else(|| format_err!("couldn't find your home directory, is $HOME not set?"))?;
if !destination.exists() {
fs::create_dir_all(&destination)?;
}
Ok(Cache::at(&destination))
}

Expand Down Expand Up @@ -87,6 +93,9 @@ impl Cache {

let destination = self.destination.join(&dirname);

let flock = File::create(self.destination.join(&format!(".{}.lock", dirname)))?;
flock.lock_exclusive()?;

if destination.exists() {
return Ok(Some(Download { root: destination }));
}
Expand Down Expand Up @@ -119,6 +128,8 @@ impl Cache {
// Now that everything is ready move this over to our destination and
// we're good to go.
fs::rename(&temp, &destination)?;

flock.unlock()?;
Ok(Some(Download { root: destination }))
}

Expand Down