Skip to content

Commit

Permalink
Fix premature fd close bug
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Smet <[email protected]>
  • Loading branch information
LeeSmet committed Aug 16, 2021
1 parent 790f375 commit b4627ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions zstor/src/zdbfs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::OpenOptions,
os::unix::prelude::{AsRawFd, RawFd},
fs::{File, OpenOptions},
os::unix::prelude::{FromRawFd, IntoRawFd, RawFd},
path::Path,
};

Expand All @@ -27,6 +27,17 @@ pub struct ZdbFsStats {
fd: RawFd,
}

impl Drop for ZdbFsStats {
fn drop(&mut self) {
// Don't assing the return value as that is pointless anyway, we just care about taking
// ownership of the Fd again in a proper struct so it's drop can properly clean up
// associated resources.
// SAFETY: This is safe as the fd is private and was aquired by calling `into_raw_fd` in
// the constructor.
let _ = unsafe { File::from_raw_fd(self.fd) };
}
}

impl ZdbFsStats {
/// Attempt to create a new ZdbFsStats with the given possible mountpoint.
pub fn try_new(mountpoint: &Path) -> Result<ZdbFsStats, std::io::Error> {
Expand All @@ -36,7 +47,7 @@ impl ZdbFsStats {
.create(false)
.truncate(false)
.open(&mountpoint)?
.as_raw_fd();
.into_raw_fd();
Ok(ZdbFsStats { fd })
}

Expand Down

0 comments on commit b4627ba

Please sign in to comment.