Skip to content

Commit 8372d87

Browse files
authored
better error reporting on mmap failure (#5)
1 parent ebc5a81 commit 8372d87

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ pub enum Error {
66
InvalidFileSize,
77
InvalidHeader,
88
IoError(std::io::Error),
9-
MMapError(usize),
9+
MMapError(std::io::Error),
1010
}

src/init.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn create(
4444
let file = create_file(path, file_size)?;
4545
let mmap = open_mmap(&file, file_size)?;
4646

47-
let header = NonNull::new(mmap.cast::<Header>()).ok_or(Error::MMapError(0))?;
47+
let header = NonNull::new(mmap.cast::<Header>()).expect("mmap already checked for null");
4848

4949
// Initialize the header.
5050
// SAFETY: The header is valid for any byte pattern.
@@ -61,7 +61,7 @@ pub fn join(path: impl AsRef<Path>) -> Result<NonNull<Header>, Error> {
6161
let file = open_file(path)?;
6262
let file_size = file.metadata().map_err(Error::IoError)?.len() as usize;
6363
let mmap = open_mmap(&file, file_size)?;
64-
let header = NonNull::new(mmap.cast::<Header>()).ok_or(Error::MMapError(0))?;
64+
let header = NonNull::new(mmap.cast::<Header>()).expect("mmap already checked for null");
6565

6666
// Verify header
6767
{
@@ -120,7 +120,7 @@ fn open_mmap(file: &File, size: usize) -> Result<*mut c_void, Error> {
120120
};
121121

122122
if mmap == libc::MAP_FAILED {
123-
return Err(Error::MMapError(mmap as usize));
123+
return Err(Error::MMapError(std::io::Error::last_os_error()));
124124
}
125125

126126
Ok(mmap)

0 commit comments

Comments
 (0)