Skip to content

Commit 3759cfc

Browse files
committed
Expose a method to get the inner env file
1 parent 95a3907 commit 3759cfc

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

heed/src/envs/env.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use heed_traits::Comparator;
1212
use synchronoise::SignalEvent;
1313

1414
use super::{
15-
custom_key_cmp_wrapper, get_file_fd, metadata_from_fd, DefaultComparator, EnvClosingEvent,
16-
EnvInfo, FlagSetMode, IntegerComparator, OPENED_ENV,
15+
custom_key_cmp_wrapper, get_file_fd, DefaultComparator, EnvClosingEvent, EnvInfo, FlagSetMode,
16+
IntegerComparator, OPENED_ENV,
1717
};
1818
use crate::cursor::{MoveOperation, RoCursor};
1919
use crate::mdb::ffi::{self, MDB_env};
@@ -63,11 +63,27 @@ impl<T> Env<T> {
6363
/// # Ok(()) }
6464
/// ```
6565
pub fn real_disk_size(&self) -> Result<u64> {
66+
Ok(self.try_clone_inner_file()?.metadata()?.len())
67+
}
68+
69+
/// Try cloning the inner file used in the environment and return a `File`
70+
/// corresponding to the environment file.
71+
///
72+
/// # Safety
73+
///
74+
/// This function is safe as we are creating a cloned fd of the inner file the file
75+
/// is. Doing write operations on the file descriptor can lead to undefined behavior
76+
/// and only read-only operations while no write operations are in progress is safe.
77+
pub fn try_clone_inner_file(&self) -> Result<File> {
6678
let mut fd = mem::MaybeUninit::uninit();
6779
unsafe { mdb_result(ffi::mdb_env_get_fd(self.env_mut_ptr().as_mut(), fd.as_mut_ptr()))? };
68-
let fd = unsafe { fd.assume_init() };
69-
let metadata = unsafe { metadata_from_fd(fd)? };
70-
Ok(metadata.len())
80+
let raw_fd = unsafe { fd.assume_init() };
81+
#[cfg(unix)]
82+
let fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(raw_fd) };
83+
#[cfg(windows)]
84+
let fd = unsafe { std::os::windows::io::BorrowedHandle::borrow_raw(raw_fd) };
85+
let owned = fd.try_clone_to_owned()?;
86+
Ok(File::from(owned))
7187
}
7288

7389
/// Return the raw flags the environment was opened with.

heed/src/envs/mod.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::cmp::Ordering;
22
use std::collections::HashMap;
33
use std::ffi::c_void;
4-
use std::fs::{File, Metadata};
4+
use std::fs::File;
55
#[cfg(unix)]
6-
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
6+
use std::os::unix::io::{AsRawFd, RawFd};
77
use std::panic::catch_unwind;
88
use std::path::{Path, PathBuf};
99
use std::process::abort;
@@ -12,7 +12,7 @@ use std::time::Duration;
1212
#[cfg(windows)]
1313
use std::{
1414
ffi::OsStr,
15-
os::windows::io::{AsRawHandle as _, BorrowedHandle, RawHandle},
15+
os::windows::io::{AsRawHandle as _, RawHandle},
1616
};
1717
use std::{fmt, io};
1818

@@ -131,22 +131,6 @@ fn get_file_fd(file: &File) -> RawHandle {
131131
file.as_raw_handle()
132132
}
133133

134-
#[cfg(unix)]
135-
/// Get metadata from a file descriptor.
136-
unsafe fn metadata_from_fd(raw_fd: RawFd) -> io::Result<Metadata> {
137-
let fd = BorrowedFd::borrow_raw(raw_fd);
138-
let owned = fd.try_clone_to_owned()?;
139-
File::from(owned).metadata()
140-
}
141-
142-
#[cfg(windows)]
143-
/// Get metadata from a file descriptor.
144-
unsafe fn metadata_from_fd(raw_fd: RawHandle) -> io::Result<Metadata> {
145-
let fd = BorrowedHandle::borrow_raw(raw_fd);
146-
let owned = fd.try_clone_to_owned()?;
147-
File::from(owned).metadata()
148-
}
149-
150134
/// A helper function that transforms the LMDB types into Rust types (`MDB_val` into slices)
151135
/// and vice versa, the Rust types into C types (`Ordering` into an integer).
152136
///

0 commit comments

Comments
 (0)