Retrieve free disk space macOS, Linux, and Windows.#119
Retrieve free disk space macOS, Linux, and Windows.#119lfreist merged 4 commits intolfreist:mainfrom
Conversation
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.
d0722d9 to
86bd670
Compare
|
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. |
|
I added a PR to this PR that supports getting the list of volumes for each disk as well: @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
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}) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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...
reverted changes
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
macOS
statfs()to fetch free space.disk0or 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.statvfs()for retrieving free space.GetLogicalDriveStrings()+GetDiskFreeSpaceEx()approach for free size.