Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve free disk space macOS, Linux, and Windows. #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BohdanBuinich
Copy link

@BohdanBuinich BohdanBuinich commented Feb 14, 2025

This commit (and PR) introduces a new function to retrieve free disk space across macOS, Linux, and Windows.

It refactors existing logic to ensure consistency, readability, and maintainability.

  • Common

    • Introduced a new function/method to retrieve free disk size in a consistent manner across platforms.
  • macOS

    • Implemented a mount-based approach using statfs() to fetch free space.
    • Physical or container disks (like disk0 or APFS containers) may return -1 if they have no mounted filesystem. For example, APFS containers are not directly mounted but contain APFS volumes; only the volumes have valid mount points.

macos(1)

  • Linux
    • Leveraged existing logic with statvfs() for retrieving free space.
    • Minor refactoring.

Linux(1)

  • Windows
    • Maintained the GetLogicalDriveStrings() + GetDiskFreeSpaceEx() approach for free size.

win(1)


  • Returning -1 indicates an unreachable or unmounted filesystem. This behavior is most commonly seen on macOS with APFS containers or physical devices that are not directly mounted.
  • In all scenarios, if the disk cannot be located or if the OS functions fail, -1 is returned to signify no valid free-space calculation.

This commit (and PR) introduces a **new function** to retrieve free disk space in a unified manner across macOS, Linux, and Windows.
It refactors existing logic to ensure consistency, readability, and maintainability.

- **Common**
  - Introduced a new function/method to retrieve free disk size in a consistent manner across platforms.

- **macOS**
  - Implemented a mount-based approach using `statfs()` to fetch free space.
  - Physical or container disks (like `disk0` or APFS containers) may return **-1** if they have **no** mounted filesystem.
    For example, APFS containers are not directly mounted but contain APFS volumes; only the volumes have valid mount points.

- **Linux**
  - Leveraged existing logic with `statvfs()` for retrieving free space.
  - Minor refactoring.

- **Windows**
  - Maintained the `GetLogicalDriveStrings()` + `GetDiskFreeSpaceEx()` approach for free size.

- Returning **-1** indicates an **unreachable** or **unmounted** filesystem. This behavior is most commonly seen on **macOS** with APFS containers or physical devices that are **not** directly mounted.
- In all scenarios, if the disk cannot be located or if the OS functions fail, **-1** is returned to signify no valid free-space calculation.
@BohdanBuinich BohdanBuinich changed the title This commit (and PR) introduces a **new function** to retrieve free disk space in a unified manner across macOS, Linux, and Windows. Retrieve free disk space macOS, Linux, and Windows. Feb 14, 2025
@BohdanBuinich BohdanBuinich marked this pull request as ready for review February 14, 2025 20:08
@DriftSolutions
Copy link

Thanks for this, I just started using this lib and wished it had the disk free space too. Not to be greedy but it would be nice to have the mountpoint/drive letter available too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants