Skip to content

Commit 3b13f9f

Browse files
Merge pull request #3 from xtuc/feat-flock
Adds flock for concurrent access
2 parents 8a2f517 + 800815e commit 3b13f9f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ is_executable = "0.1.2"
1818
siphasher = "0.2.3"
1919
tar = "0.4.16"
2020
zip = "0.5.0"
21+
fs2 = "0.4.3"
2122

2223
[dev-dependencies]
2324
tempfile = "3.0.5"

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ extern crate curl;
55
extern crate failure;
66
extern crate dirs;
77
extern crate flate2;
8+
extern crate fs2;
89
extern crate hex;
910
extern crate is_executable;
1011
extern crate siphasher;
1112
extern crate tar;
1213
extern crate zip;
1314

1415
use failure::{Error, ResultExt};
16+
use fs2::FileExt;
1517
use siphasher::sip::SipHasher13;
1618
use std::collections::HashSet;
1719
use std::env;
1820
use std::ffi;
1921
use std::fs;
22+
use std::fs::File;
2023
use std::hash::{Hash, Hasher};
2124
use std::io;
2225
use std::path::{Path, PathBuf};
@@ -48,6 +51,9 @@ impl Cache {
4851
Some(home.join(&cache_name))
4952
})
5053
.ok_or_else(|| format_err!("couldn't find your home directory, is $HOME not set?"))?;
54+
if !destination.exists() {
55+
fs::create_dir_all(&destination)?;
56+
}
5157
Ok(Cache::at(&destination))
5258
}
5359

@@ -87,6 +93,9 @@ impl Cache {
8793

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

96+
let flock = File::create(self.destination.join(&format!(".{}.lock", dirname)))?;
97+
flock.lock_exclusive()?;
98+
9099
if destination.exists() {
91100
return Ok(Some(Download { root: destination }));
92101
}
@@ -119,6 +128,8 @@ impl Cache {
119128
// Now that everything is ready move this over to our destination and
120129
// we're good to go.
121130
fs::rename(&temp, &destination)?;
131+
132+
flock.unlock()?;
122133
Ok(Some(Download { root: destination }))
123134
}
124135

0 commit comments

Comments
 (0)