From 0f0a35b390e99270573c4dbd69030a9d3fcf322e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 4 Jul 2019 08:17:30 +0200 Subject: [PATCH] src/backup/dynamic_index.rs: lock file inside new, code cleanup --- src/backup/dynamic_index.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backup/dynamic_index.rs b/src/backup/dynamic_index.rs index db733459..ce6269f4 100644 --- a/src/backup/dynamic_index.rs +++ b/src/backup/dynamic_index.rs @@ -61,18 +61,18 @@ impl DynamicIndexReader { pub fn open(path: &Path) -> Result { - let file = std::fs::File::open(&path) - .map_err(|err| format_err!("Unable to open dynamic index {:?} - {}", path, err))?; - - if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) { - bail!("unable to get shared lock on {:?} - {}", path, err); - } - - Self::new(file) + File::open(path) + .map_err(Error::from) + .and_then(|file| Self::new(file)) + .map_err(|err| format_err!("Unable to open dynamic index {:?} - {}", path, err)) } pub fn new(mut file: std::fs::File) -> Result { + 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))?; let header_size = std::mem::size_of::();