Skip to content

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

Merged
lfreist merged 4 commits intolfreist:mainfrom
BohdanBuinich:feat/disk_free_size
Apr 23, 2025
Merged

Retrieve free disk space macOS, Linux, and Windows.#119
lfreist merged 4 commits intolfreist:mainfrom
BohdanBuinich:feat/disk_free_size

Conversation

@BohdanBuinich
Copy link
Contributor

@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.

@aminya
Copy link
Contributor

aminya commented Mar 17, 2025

I added a PR to this PR that supports getting the list of volumes for each disk as well:
BohdanBuinich#2

@BohdanBuinich Could you review?

* fix: use the latest supported standard instead of forcing C++-11

* feat: support getting volume paths

* docs: update readme to add the volumes support
Copy link
Owner

@lfreist lfreist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the valuable contribution! Only thing to complain is the CMakeLists change, that I'd ask you to revert for the stated reason.

Edit: Nevermind. Just realized, I can directly commit to this pull request.

CMakeLists.txt Outdated

set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${HWINFO_CMAKE_BINARY_DIR})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${HWINFO_CMAKE_BINARY_DIR})
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra variable ${HWINFO_CMAKE_BINARY_DIR} was added for the following reason:

If hwinfo is used as subproject, the main project developer can easily define, where hwinfo targets are built to. If we directly use ${CMAKE_BINARY_DIR}, the hwinfo targets are built into the main projects binary directory.

Thus, I would like to keep it as is.

// guess the vendor from the model
if (model.find("APPLE") != std::string::npos || model.find("Apple") != std::string::npos) {
// Guess vendor based on model
if (disk._model.find("APPLE") != std::string::npos || disk._model.find("Apple") != std::string::npos) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is worth converting disk._model to lower case and just search for "apple". However, I don't know, what case style may be contained. If only "Apple" and "APPLE" are used, it is fine...

@lfreist lfreist merged commit 9107f80 into lfreist:main Apr 23, 2025
6 checks passed
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.

4 participants