in src/flock.rs [186:223]
fn open(
&self,
path: &Path,
opts: &OpenOptions,
state: State,
msg: &str,
) -> Result<FileLock, FlockError> {
let path = self.root.join(path);
// If we want an exclusive lock then if we fail because of NotFound it's
// likely because an intermediate directory didn't exist, so try to
// create the directory and then continue.
let f = opts.open(&path).or_else(|e| {
if e.kind() == io::ErrorKind::NotFound && state == State::Exclusive {
let parent = path.parent().unwrap();
fs::create_dir_all(parent)?;
Ok(opts.open(&path)?)
} else {
Err(e)
}
})?;
match state {
State::Exclusive => {
acquire(msg, &path, &|| try_lock_exclusive(&f), &|| {
lock_exclusive(&f)
})?;
}
State::Shared => {
acquire(msg, &path, &|| try_lock_shared(&f), &|| lock_shared(&f))?;
}
State::Unlocked => {}
}
Ok(FileLock {
f: Some(f),
path,
state,
})
}