2 unstable releases

0.2.0 Mar 19, 2020
0.1.0 May 3, 2019

#170 in Operating systems

Download history 11046/week @ 2024-07-23 11112/week @ 2024-07-30 13820/week @ 2024-08-06 9227/week @ 2024-08-13 11185/week @ 2024-08-20 8453/week @ 2024-08-27 10572/week @ 2024-09-03 11863/week @ 2024-09-10 12002/week @ 2024-09-17 14317/week @ 2024-09-24 10910/week @ 2024-10-01 12336/week @ 2024-10-08 11804/week @ 2024-10-15 12452/week @ 2024-10-22 11510/week @ 2024-10-29 10086/week @ 2024-11-05

47,150 downloads per month
Used in 38 crates (16 directly)

MIT license

12KB
85 lines

Cargo Documentation Build Status CI

filesize

Cross-platform physical disk space use retrieval for Rust.

Synopsis

pub trait PathExt {
    fn size_on_disk(&self) -> std::io::Result<u64>;
    fn size_on_disk_fast(&self, metadata: &Metadata) -> std::io::Result<u64>;
}
impl PathExt for std::path::Path;

pub fn file_real_size<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<u64>;
pub fn file_real_size_fast<P: AsRef<std::path::Path>>(
    path: P,
    metadata: &Metadata
) -> std::io::Result<u64>;

Description

filesize abstracts platform-specific methods of determining the real space used by files, taking into account filesystem compression and sparse files.

It provides two standalone functions, file_real_size, and file_real_size_fast, and as of version 0.2, a std::path::Path extension trait offering identical functions named size_on_disk and size_on_disk_fast.

The _fast variants accept a std::fs::Metadata reference which will be used to cheaply calculate the size on disk if the platform supports that. This is intended for cases such as directory traversal, where metadata is available anyway, and where metadata is needed for other purposes.

Example

use std::path::Path;
use filesize::PathExt;

let path = Path::new("Cargo.toml");
let metadata = path.symlink_metadata()?;

let realsize = path.size_on_disk()?;
let realsize = path.size_on_disk_fast(&metadata)?;

// Older interface
use filesize::{file_real_size, file_real_size_fast};

let realsize = file_real_size(path)?;
let realsize = file_real_size_fast(path, &metadata)?;

Platform-specific Behaviour

On Unix platforms this is a thin wrapper around std::fs::symlink_metadata() and std::os::unix::fs::MetadataExt, simply returning blocks() * 512. The _fast functions disregard the file path entirely and use the passed metadata directly.

On Windows, it wraps GetCompressedFileSizeW(), and the _fast functions disregard the passed metadata entirely.

On any other platforms, it wraps std::fs::symlink_metadata() and only returns len(), while the _fast variants also disregard the path and use the passed metadata directly.

Dependencies

~175KB