| 1 |
use nix::sys::statfs::*; |
| 2 |
use nix::sys::statvfs::*; |
| 3 |
use std::fs::File; |
| 4 |
use std::path::Path; |
| 5 |
|
| 6 |
fn check_fstatfs(path: &str) { |
| 7 |
if !Path::new(path).exists() { |
| 8 |
return; |
| 9 |
} |
| 10 |
let vfs = statvfs(path.as_bytes()).unwrap(); |
| 11 |
let file = File::open(path).unwrap(); |
| 12 |
let fs = fstatfs(&file).unwrap(); |
| 13 |
assert_fs_equals(fs, vfs); |
| 14 |
} |
| 15 |
|
| 16 |
fn check_statfs(path: &str) { |
| 17 |
if !Path::new(path).exists() { |
| 18 |
return; |
| 19 |
} |
| 20 |
let vfs = statvfs(path.as_bytes()).unwrap(); |
| 21 |
let fs = statfs(path.as_bytes()).unwrap(); |
| 22 |
assert_fs_equals(fs, vfs); |
| 23 |
} |
| 24 |
|
| 25 |
fn check_fstatfs_strict(path: &str) { |
| 26 |
if !Path::new(path).exists() { |
| 27 |
return; |
| 28 |
} |
| 29 |
let vfs = statvfs(path.as_bytes()); |
| 30 |
let file = File::open(path).unwrap(); |
| 31 |
let fs = fstatfs(&file); |
| 32 |
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap()) |
| 33 |
} |
| 34 |
|
| 35 |
fn check_statfs_strict(path: &str) { |
| 36 |
if !Path::new(path).exists() { |
| 37 |
return; |
| 38 |
} |
| 39 |
let vfs = statvfs(path.as_bytes()); |
| 40 |
let fs = statfs(path.as_bytes()); |
| 41 |
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap()) |
| 42 |
} |
| 43 |
|
| 44 |
// The cast is not unnecessary on all platforms. |
| 45 |
#[allow(clippy::unnecessary_cast)] |
| 46 |
fn assert_fs_equals(fs: Statfs, vfs: Statvfs) { |
| 47 |
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64); |
| 48 |
assert_eq!(fs.block_size() as u64, vfs.fragment_size() as u64); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn statfs_call() { |
| 53 |
check_statfs("/tmp"); |
| 54 |
check_statfs("/dev"); |
| 55 |
check_statfs("/run"); |
| 56 |
check_statfs("/"); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn fstatfs_call() { |
| 61 |
check_fstatfs("/tmp"); |
| 62 |
check_fstatfs("/dev"); |
| 63 |
check_fstatfs("/run"); |
| 64 |
check_fstatfs("/"); |
| 65 |
} |
| 66 |
|
| 67 |
// This test is ignored because files_free/blocks_free can change after statvfs call and before |
| 68 |
// statfs call. |
| 69 |
#[test] |
| 70 |
#[ignore] |
| 71 |
fn statfs_call_strict() { |
| 72 |
check_statfs_strict("/tmp"); |
| 73 |
check_statfs_strict("/dev"); |
| 74 |
check_statfs_strict("/run"); |
| 75 |
check_statfs_strict("/"); |
| 76 |
} |
| 77 |
|
| 78 |
// This test is ignored because files_free/blocks_free can change after statvfs call and before |
| 79 |
// fstatfs call. |
| 80 |
#[test] |
| 81 |
#[ignore] |
| 82 |
fn fstatfs_call_strict() { |
| 83 |
check_fstatfs_strict("/tmp"); |
| 84 |
check_fstatfs_strict("/dev"); |
| 85 |
check_fstatfs_strict("/run"); |
| 86 |
check_fstatfs_strict("/"); |
| 87 |
} |
| 88 |
|
| 89 |
// The cast is not unnecessary on all platforms. |
| 90 |
#[allow(clippy::unnecessary_cast)] |
| 91 |
fn assert_fs_equals_strict(fs: Statfs, vfs: Statvfs) { |
| 92 |
assert_eq!(fs.files_free() as u64, vfs.files_free() as u64); |
| 93 |
assert_eq!(fs.blocks_free() as u64, vfs.blocks_free() as u64); |
| 94 |
assert_eq!(fs.blocks_available() as u64, vfs.blocks_available() as u64); |
| 95 |
assert_eq!(fs.files() as u64, vfs.files() as u64); |
| 96 |
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64); |
| 97 |
assert_eq!(fs.block_size() as u64, vfs.fragment_size() as u64); |
| 98 |
} |