-
Notifications
You must be signed in to change notification settings - Fork 92
/
flock.rs
215 lines (186 loc) · 4.89 KB
/
flock.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Copy paste of Cargo's src/util/flock.rs with modifications to not depend on
//! other Cargo stuff
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Display, Path, PathBuf};
use std::{fs, io};
use fs2::FileExt;
use fs2;
#[derive(PartialEq)]
enum State {
Exclusive,
Shared,
}
pub struct FileLock {
file: File,
path: PathBuf,
}
impl FileLock {
pub fn parent(&self) -> &Path {
self.path.parent().unwrap()
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn remove_siblings(&self) -> io::Result<()> {
let path = self.path();
for entry in path.parent().unwrap().read_dir()? {
let entry = entry?;
if Some(&entry.file_name()[..]) == path.file_name() {
continue;
}
let kind = entry.file_type()?;
if kind.is_dir() {
fs::remove_dir_all(entry.path())?;
} else {
fs::remove_file(entry.path())?;
}
}
Ok(())
}
}
pub struct Filesystem {
path: PathBuf,
}
impl Filesystem {
pub fn new(path: PathBuf) -> Filesystem {
Filesystem { path: path }
}
pub fn join<T>(&self, other: T) -> Filesystem
where
T: AsRef<Path>,
{
Filesystem::new(self.path.join(other))
}
pub fn open_ro<P>(&self, path: P, msg: &str) -> io::Result<FileLock>
where
P: AsRef<Path>,
{
self.open(
path.as_ref(),
OpenOptions::new().read(true),
State::Shared,
msg,
)
}
pub fn open_rw<P>(&self, path: P, msg: &str) -> io::Result<FileLock>
where
P: AsRef<Path>,
{
self.open(
path.as_ref(),
OpenOptions::new().read(true).write(true).create(true),
State::Exclusive,
msg,
)
}
fn open(
&self,
path: &Path,
opts: &OpenOptions,
state: State,
msg: &str,
) -> io::Result<FileLock> {
let path = self.path.join(path);
let f = opts.open(&path).or_else(|e| {
if e.kind() == io::ErrorKind::NotFound && state == State::Exclusive {
create_dir_all(path.parent().unwrap())?;
opts.open(&path)
} else {
Err(e)
}
})?;
match state {
State::Exclusive => {
acquire(
msg,
&path,
&|| f.try_lock_exclusive(),
&|| f.lock_exclusive(),
)?;
}
State::Shared => {
acquire(msg, &path, &|| f.try_lock_shared(), &|| f.lock_shared())?;
}
}
Ok(FileLock {
file: f,
path: path,
})
}
pub fn display(&self) -> Display {
self.path.display()
}
}
impl Drop for FileLock {
fn drop(&mut self) {
self.file.unlock().ok();
}
}
fn acquire(
msg: &str,
path: &Path,
try: &dyn Fn() -> io::Result<()>,
block: &dyn Fn() -> io::Result<()>,
) -> io::Result<()> {
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn is_on_nfs_mount(path: &Path) -> bool {
use std::ffi::CString;
use std::mem;
use std::os::unix::prelude::*;
let path = match CString::new(path.as_os_str().as_bytes()) {
Ok(path) => path,
Err(_) => return false,
};
unsafe {
let mut buf: ::libc::statfs = mem::zeroed();
let r = ::libc::statfs(path.as_ptr(), &mut buf);
r == 0 && buf.f_type as u32 == ::libc::NFS_SUPER_MAGIC as u32
}
}
#[cfg(any(not(target_os = "linux"), target_env = "musl"))]
fn is_on_nfs_mount(_path: &Path) -> bool {
false
}
if is_on_nfs_mount(path) {
return Ok(());
}
match try() {
Ok(_) => return Ok(()),
#[cfg(target_os = "macos")]
Err(ref e) if e.raw_os_error() == Some(::libc::ENOTSUP) =>
{
return Ok(())
}
Err(e) => if e.raw_os_error() != fs2::lock_contended_error().raw_os_error() {
return Err(e);
},
}
writeln!(
io::stderr(),
"{:>12} waiting for file lock on {}",
"Blocking",
msg
).ok();
block()
}
fn create_dir_all(path: &Path) -> io::Result<()> {
match create_dir(path) {
Ok(()) => Ok(()),
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
if let Some(p) = path.parent() {
return create_dir_all(p).and_then(|()| create_dir(path));
}
}
Err(e)
}
}
}
fn create_dir(path: &Path) -> io::Result<()> {
match fs::create_dir(path) {
Ok(()) => Ok(()),
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
Err(e) => Err(e),
}
}