Files
BohdanBuinich 9107f80434 Retrieve free disk space macOS, Linux, and Windows. (#119)
* Retrieve free disk space macOS, Linux, and Windows.

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.

* feat: support getting volume paths (#2)

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

* feat: support getting volume paths

* docs: update readme to add the volumes support

* Update CMakeLists.txt

reverted changes

* Update CMakeLists.txt

---------

Co-authored-by: Amin Ya <aminyahyaabadi74@gmail.com>
Co-authored-by: Leon Freist <freist.leon@gmail.com>
2025-04-23 21:11:35 +02:00

46 lines
1.0 KiB
C++

// Copyright Leon Freist
// Author Leon Freist <freist@informatik.uni-freiburg.de>
#pragma once
#include <hwinfo/platform.h>
#include <cstdint>
#include <string>
#include <vector>
namespace hwinfo {
// Linux always considers sectors to be 512 bytes long independently of the devices real block size.
const unsigned short block_size = 512;
class HWINFO_API Disk {
friend std::vector<Disk> getAllDisks();
public:
~Disk() = default;
HWI_NODISCARD const std::string& vendor() const;
HWI_NODISCARD const std::string& model() const;
HWI_NODISCARD const std::string& serialNumber() const;
HWI_NODISCARD int64_t size_Bytes() const;
HWI_NODISCARD int64_t free_size_Bytes() const;
HWI_NODISCARD const std::vector<std::string>& volumes() const;
HWI_NODISCARD int id() const;
private:
Disk() = default;
std::string _vendor;
std::string _model;
std::string _serialNumber;
int64_t _size_Bytes{-1};
int64_t _free_size_Bytes{-1};
std::vector<std::string> _volumes;
int _id{-1};
};
std::vector<Disk> getAllDisks();
} // namespace hwinfo