Skip to content

Commit

Permalink
style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lfreist committed Oct 30, 2023
1 parent 0b74830 commit f0c2327
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
28 changes: 11 additions & 17 deletions include/hwinfo/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@

#pragma once

#include <hwinfo/platform.h>

#include <cstdint>
#include <string>
#include <vector>

namespace hwinfo {

const unsigned short block_size = 512; // Linux always considers sectors to be 512 bytes long independently of the devices real block size.
const unsigned short block_size =
512; // Linux always considers sectors to be 512 bytes long independently of the devices real block size.

class Disk {
friend std::vector<Disk> getAllDisks();

public:
~Disk() = default;

const std::string& vendor() const;
const std::string& model() const;
const std::string& serialNumber() const;
int64_t size_Bytes() const;
int id() const;
bool empty() const;
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 int id() const;

private:
Disk() {
_vendor = "<unknown>";
_model = "<unknown>";
_serialNumber = "<unknown>";
}
Disk() = default;

std::string _vendor;
std::string _model;
std::string _serialNumber;
Expand All @@ -38,10 +37,5 @@ class Disk {
};

std::vector<Disk> getAllDisks();
std::string getDiskVendor(const std::string& path);
std::string getDiskModel(const std::string& path);
std::string getDiskSerialNumber(const std::string& path);
int64_t getDiskSize_Bytes(const std::string& path);
bool isPartition(const std::string& path);

} // namespace hwinfo
2 changes: 1 addition & 1 deletion include/hwinfo/utils/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

#include "hwinfo/cpu.h"
#include <hwinfo/cpu.h>

namespace hwinfo {
namespace filesystem {
Expand Down
4 changes: 0 additions & 4 deletions src/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ int64_t Disk::size_Bytes() const { return _size_Bytes; }
// _____________________________________________________________________________________________________________________
int Disk::id() const { return _id; }

// _____________________________________________________________________________________________________________________
bool Disk::empty() const { return (_vendor == "<unknown>" && _model == "<unknown>" && _serialNumber == "<unknown>" &&
_size_Bytes == -1 && _id == -1); }

} // namespace hwinfo
31 changes: 17 additions & 14 deletions src/linux/disk.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
// Copyright Leon Freist
// Author Leon Freist <freist@informatik.uni-freiburg.de>

#include "hwinfo/platform.h"
#include <hwinfo/platform.h>

#ifdef HWINFO_UNIX

#include <hwinfo/disk.h>
#include <hwinfo/utils/filesystem.h>
#include <hwinfo/utils/stringutils.h>

#include <fstream>
#include <regex>

#include "hwinfo/disk.h"
#include "hwinfo/utils/filesystem.h"
#include "hwinfo/utils/stringutils.h"

namespace hwinfo {

// _____________________________________________________________________________________________________________________
bool isPartition(const std::string& path) {
std::regex partitionRegex(R"((sda|sdb|sdc|nvme\d+n\d+)p?\d+$)");
return std::regex_search(path, partitionRegex);
std::regex partitionRegex(R"((sd[a-z]|nvme\d+n\d+)p?\d+$)");
return std::regex_search(path, partitionRegex);
}

// _____________________________________________________________________________________________________________________
std::string getDiskVendor(const std::string& path) {
// nvme devices are in /sys/class/nvme/ and /sys/class/block/nvme*
// but the vendor file is only in /sys/class/nvme/
// so we need to check if the device is in /sys/class/block/nvme* and if so, we need to change the path
// TODO: use utils::get_specs_by_file
std::string vendor_path = path;
std::string::size_type nvme_pos = path.find("nvme");
if (nvme_pos != std::string::npos) {
std::string nvme_name = path.substr(nvme_pos, 5); // Exemple : "nvme0"
std::string nvme_name = path.substr(nvme_pos, 5); // Exemple : "nvme0"
vendor_path = path.substr(0, nvme_pos - 6) + "nvme/" + nvme_name;
}

std::ifstream f(vendor_path + "/device/vendor");
if (f.is_open()) {
std::string vendor;
Expand All @@ -40,7 +41,7 @@ std::string getDiskVendor(const std::string& path) {
f.close();
return vendor;
}

return "<unknown>";
}

Expand Down Expand Up @@ -89,16 +90,18 @@ std::vector<Disk> getAllDisks() {
const std::string base_path("/sys/class/block/");
for (const auto& entry : filesystem::getDirectoryEntries(base_path)) {
std::string path(base_path + entry);
if (!filesystem::exists(path))
continue;
if (isPartition(path)) // Check if it's a partition
if (!filesystem::exists(path)) continue;
if (isPartition(path)) { // Check if it's a partition
continue;
}
Disk disk = Disk();
disk._vendor = getDiskVendor(path);
disk._model = getDiskModel(path);
disk._serialNumber = getDiskSerialNumber(path);
if (disk.empty()) // Check before get size because size is always define in /sys/class/block/...
// Check before get size because size is always define in /sys/class/block/...
if (disk._vendor == "<unknown>" && disk._model == "<unknown>" && disk._serialNumber == "<unknown>") {
continue;
}
disk._size_Bytes = getDiskSize_Bytes(path);

disks.push_back(std::move(disk));
Expand Down

0 comments on commit f0c2327

Please sign in to comment.