Skip to content

Commit d6fdd59

Browse files
committed
On Linux-like operating systems, use statfs64 instead of statfs.
And fstatfs64 instead of fstatfs.
1 parent 0696987 commit d6fdd59

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/sys/statfs.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::os::unix::io::AsRawFd;
77
#[cfg(not(any(target_os = "linux", target_os = "android")))]
88
use std::ffi::CStr;
99

10+
use cfg_if::cfg_if;
11+
1012
use crate::{NixPath, Result, errno::Errno};
1113

1214
/// Identifies a mounted file system
@@ -18,10 +20,30 @@ pub type fsid_t = libc::__fsid_t;
1820
#[cfg_attr(docsrs, doc(cfg(all())))]
1921
pub type fsid_t = libc::fsid_t;
2022

23+
cfg_if! {
24+
if #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))] {
25+
type type_of_statfs = libc::statfs64;
26+
const LIBC_FSTATFS: unsafe extern fn
27+
(fd: libc::c_int, buf: *mut type_of_statfs) -> libc::c_int
28+
= libc::fstatfs64;
29+
const LIBC_STATFS: unsafe extern fn
30+
(path: *const libc::c_char, buf: *mut type_of_statfs) -> libc::c_int
31+
= libc::statfs64;
32+
} else {
33+
type type_of_statfs = libc::statfs;
34+
const LIBC_FSTATFS: unsafe extern fn
35+
(fd: libc::c_int, buf: *mut type_of_statfs) -> libc::c_int
36+
= libc::fstatfs;
37+
const LIBC_STATFS: unsafe extern fn
38+
(path: *const libc::c_char, buf: *mut type_of_statfs) -> libc::c_int
39+
= libc::statfs;
40+
}
41+
}
42+
2143
/// Describes a mounted file system
2244
#[derive(Clone, Copy)]
2345
#[repr(transparent)]
24-
pub struct Statfs(libc::statfs);
46+
pub struct Statfs(type_of_statfs);
2547

2648
#[cfg(target_os = "freebsd")]
2749
type fs_type_t = u32;
@@ -642,8 +664,8 @@ impl Debug for Statfs {
642664
/// `path` - Path to any file within the file system to describe
643665
pub fn statfs<P: ?Sized + NixPath>(path: &P) -> Result<Statfs> {
644666
unsafe {
645-
let mut stat = mem::MaybeUninit::<libc::statfs>::uninit();
646-
let res = path.with_nix_path(|path| libc::statfs(path.as_ptr(), stat.as_mut_ptr()))?;
667+
let mut stat = mem::MaybeUninit::<type_of_statfs>::uninit();
668+
let res = path.with_nix_path(|path| LIBC_STATFS(path.as_ptr(), stat.as_mut_ptr()))?;
647669
Errno::result(res).map(|_| Statfs(stat.assume_init()))
648670
}
649671
}
@@ -658,8 +680,8 @@ pub fn statfs<P: ?Sized + NixPath>(path: &P) -> Result<Statfs> {
658680
/// `fd` - File descriptor of any open file within the file system to describe
659681
pub fn fstatfs<T: AsRawFd>(fd: &T) -> Result<Statfs> {
660682
unsafe {
661-
let mut stat = mem::MaybeUninit::<libc::statfs>::uninit();
662-
Errno::result(libc::fstatfs(fd.as_raw_fd(), stat.as_mut_ptr()))
683+
let mut stat = mem::MaybeUninit::<type_of_statfs>::uninit();
684+
Errno::result(LIBC_FSTATFS(fd.as_raw_fd(), stat.as_mut_ptr()))
663685
.map(|_| Statfs(stat.assume_init()))
664686
}
665687
}

0 commit comments

Comments
 (0)