Debian Codegraph @

src/common/fileutils.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "fileutils.h"
5
6 #include <QFile>
7 #include <exception>
8 #include <fstream>
9 #include <spdlog/spdlog.h>
10 #include <utility>
11
12 namespace fs = std::filesystem;
13
14 namespace Utils::File {
15
16 std::vector<char> readFile(std::filesystem::path const &path)
17 {
18 std::vector<char> data;
19
20 if (isFilePathValid(path)) {
21 std::ifstream file(path, std::ios::binary);
22 if (file.is_open()) {
23 data.resize(fs::file_size(path));
24 file.read(data.data(), data.size());
25 }
26 else
27 SPDLOG_DEBUG("Cannot open file {}", path.c_str());
28 }
29 else
30 SPDLOG_DEBUG("Invalid file path {}", path.c_str());
31
32 return data;
33 }
34
35 bool writeFile(std::filesystem::path const &path, std::vector<char> const &data)
36 {
37 std::ofstream file(path, std::ios::binary);
38 if (file.is_open()) {
39 file.write(data.data(), data.size());
40 return true;
41 }
42 else
43 SPDLOG_DEBUG("Cannot open file {}", path.c_str());
44
45 return false;
46 }
47
48 std::vector<std::string> readFileLines(std::filesystem::path const &path,
49 char delim)
50 {
51 std::vector<std::string> entries;
52
53 if (isFilePathValid(path)) {
54 std::ifstream file(path);
55 if (file.is_open()) {
56 for (std::string entry; std::getline(file, entry, delim);)
57 entries.emplace_back(std::move(entry));
58 }
59 else
60 SPDLOG_DEBUG("Cannot open file {}", path.c_str());
61 }
62 else
63 SPDLOG_DEBUG("Invalid file path {}", path.c_str());
64
65 return entries;
66 }
67
68 bool isFilePathValid(std::filesystem::path const &path)
69 {
70 try {
71 return fs::exists(path) && fs::is_regular_file(path);
72 }
73 catch (std::exception const &e) {
74 SPDLOG_DEBUG(e.what());
75 }
76
77 return false;
78 }
79
80 bool isDirectoryPathValid(std::filesystem::path const &path)
81 {
82 try {
83 return fs::exists(path) && fs::is_directory(path);
84 }
85 catch (std::exception const &e) {
86 SPDLOG_DEBUG(e.what());
87 }
88
89 return false;
90 }
91
92 std::vector<std::filesystem::path> search(std::regex const &regex,
93 std::filesystem::path const &path)
94 {
95 std::vector<std::filesystem::path> paths;
96
97 if (isDirectoryPathValid(path)) {
98 for (auto const &entry : fs::directory_iterator(path)) {
99 auto const &entryDirPath = entry.path();
100 std::string entryDirName((*(--entryDirPath.end())).string());
101 if (std::regex_search(entryDirName, regex))
102 paths.push_back(entryDirPath);
103 }
104 }
105 else
106 SPDLOG_DEBUG("Invalid directory path {}", path.c_str());
107
108 return paths;
109 }
110
111 std::optional<std::filesystem::path>
112 findHWMonXDirectory(std::filesystem::path const &path)
113 {
114 std::regex const hwmonXRegex(R"(hwmon[0-9]+)");
115 auto paths = Utils::File::search(hwmonXRegex, path);
116 if (!paths.empty()) {
117 if (paths.size() > 1) {
118 SPDLOG_WARN("Multiple hwmon directories detected on {}.\nUsing {}",
119 path.c_str(), paths.front().c_str());
120 }
121
122 // use the first hwmon[0-*] directory from gpu sysfs/hwmon path
123 return paths.front();
124 }
125
126 return {};
127 }
128
129 bool isSysFSEntryValid(std::filesystem::path const &path)
130 {
131 if (!isFilePathValid(path))
132 return false;
133
134 if (readFileLines(path).empty()) {
135 SPDLOG_WARN("Empty sysfs entry {}", path.c_str());
136 return false;
137 }
138
139 return true;
140 }
141
142 std::vector<char> readQrcFile(std::string_view qrcPath)
143 {
144 QFile file(qrcPath.data());
145 if (file.open(QFile::ReadOnly)) {
146 auto fileData = file.readAll();
147 return std::vector<char>(fileData.cbegin(), fileData.cend());
148 }
149
150 return {};
151 }
152
153 } // namespace Utils::File