Skip to content

Commit

Permalink
Use File::metadata to get file size and not libc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Aug 14, 2022
1 parent c7dd140 commit d46b239
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 49 deletions.
22 changes: 7 additions & 15 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate libc;

use std::mem::MaybeUninit;
use std::os::unix::io::RawFd;
use std::fs::File;
use std::mem::ManuallyDrop;
use std::os::unix::io::{FromRawFd, RawFd};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{io, ptr};

Expand Down Expand Up @@ -304,19 +305,10 @@ fn page_size() -> usize {
}

pub fn file_len(file: RawFd) -> io::Result<u64> {
#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "l4re")))]
use libc::{fstat, stat};
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
use libc::{fstat64 as fstat, stat64 as stat};

// SAFETY: We must not close the passed-in fd by dropping the File we create,
// we ensure this by immediately wrapping it in a ManuallyDrop.
unsafe {
let mut stat = MaybeUninit::<stat>::uninit();

let result = fstat(file, stat.as_mut_ptr());
if result == 0 {
Ok(stat.assume_init().st_size as u64)
} else {
Err(io::Error::last_os_error())
}
let file = ManuallyDrop::new(File::from_raw_fd(file));
Ok(file.metadata()?.len())
}
}
43 changes: 9 additions & 34 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::fs::File;
use std::mem::ManuallyDrop;
use std::os::raw::c_void;
use std::os::windows::io::RawHandle;
use std::os::windows::io::{FromRawHandle, RawHandle};
use std::{io, mem, ptr};

type BOOL = i32;
Expand Down Expand Up @@ -82,22 +84,6 @@ pub struct FILETIME {
pub dwHighDateTime: DWORD,
}

#[repr(C)]
struct BY_HANDLE_FILE_INFORMATION {
dwFileAttributes: DWORD,
ftCreationTime: FILETIME,
ftLastAccessTime: FILETIME,
ftLastWriteTime: FILETIME,
dwVolumeSerialNumber: DWORD,
nFileSizeHigh: DWORD,
nFileSizeLow: DWORD,
nNumberOfLinks: DWORD,
nFileIndexHigh: DWORD,
nFileIndexLow: DWORD,
}

type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;

extern "system" {
fn GetCurrentProcess() -> HANDLE;

Expand All @@ -124,11 +110,6 @@ extern "system" {

fn FlushFileBuffers(hFile: HANDLE) -> BOOL;

fn GetFileInformationByHandle(
hFile: HANDLE,
lpFileInformation: LPBY_HANDLE_FILE_INFORMATION,
) -> BOOL;

fn FlushViewOfFile(lpBaseAddress: LPCVOID, dwNumberOfBytesToFlush: SIZE_T) -> BOOL;

fn UnmapViewOfFile(lpBaseAddress: LPCVOID) -> BOOL;
Expand Down Expand Up @@ -526,16 +507,10 @@ fn allocation_granularity() -> usize {
}

pub fn file_len(handle: RawHandle) -> io::Result<u64> {
let info = unsafe {
let mut info = mem::MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::uninit();

let ok = GetFileInformationByHandle(handle, info.as_mut_ptr());
if ok == 0 {
return Err(io::Error::last_os_error());
}

info.assume_init()
};

Ok((info.nFileSizeHigh as u64) << 32 | info.nFileSizeLow as u64)
// SAFETY: We must not close the passed-in fd by dropping the File we create,
// we ensure this by immediately wrapping it in a ManuallyDrop.
unsafe {
let file = ManuallyDrop::new(File::from_raw_handle(handle));
Ok(file.metadata()?.len())
}
}

0 comments on commit d46b239

Please sign in to comment.