src/backup/dynamic_index.rs: lock file inside new, code cleanup

This commit is contained in:
Dietmar Maurer 2019-07-04 08:17:30 +02:00
parent a7c72ad9eb
commit 0f0a35b390

View File

@ -61,18 +61,18 @@ impl DynamicIndexReader {
pub fn open(path: &Path) -> Result<Self, Error> { pub fn open(path: &Path) -> Result<Self, Error> {
let file = std::fs::File::open(&path) File::open(path)
.map_err(|err| format_err!("Unable to open dynamic index {:?} - {}", path, err))?; .map_err(Error::from)
.and_then(|file| Self::new(file))
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) { .map_err(|err| format_err!("Unable to open dynamic index {:?} - {}", path, err))
bail!("unable to get shared lock on {:?} - {}", path, err);
}
Self::new(file)
} }
pub fn new(mut file: std::fs::File) -> Result<Self, Error> { pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) {
bail!("unable to get shared lock - {}", err);
}
file.seek(SeekFrom::Start(0))?; file.seek(SeekFrom::Start(0))?;
let header_size = std::mem::size_of::<DynamicIndexHeader>(); let header_size = std::mem::size_of::<DynamicIndexHeader>();