| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/common/fileutils.cpp`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fileutils.h"cxx . . $ `<file>/src/common/fileutils.h`/ |
| 5 | |
| 6 | #include <QFile> |
| 7 | #include <exception> |
| 8 | #include <fstream> |
| 9 | #include <spdlog/spdlog.h> |
| 10 | #include <utility> |
| 11 | |
| 12 | namespace fs = stdcxx . . $ std/::filesystem; |
| 13 | |
| 14 | namespace Utilscxx . . $ Utils/::Filecxx . . $ Utils/File/ { |
| 15 | |
| 16 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> readFilecxx . . $ Utils/File/readFile(ca87ac95f82c0a15).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 17 | { |
| 18 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> datacxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 19 | |
| 20 | if (isFilePathValidcxx . . $ Utils/File/isFilePathValid(c53a6ef3278968b1).(path)) { |
| 21 | stdcxx . . $ std/::ifstreamcxx . . $ std/ifstream# filecxx . . $ std/basic_ifstream#basic_ifstream(fbe7c9ddfb647a62).(path, stdcxx . . $ std/::ioscxx . . $ std/ios#::binarycxx . . $ std/ios_base#binary.); |
| 22 | if (file.is_opencxx . . $ std/basic_ifstream#is_open(c9857a73d68e59ba).()) { |
| 23 | data.resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(fs::file_sizecxx . . $ std/filesystem/file_size(fef9a1c7f0154599).(path)); |
| 24 | file.readcxx . . $ std/basic_istream#read(ec7d6f01d31dcb96).(data.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(), data.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 25 | } |
| 26 | else |
| 27 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot open file {}", path.c_str()); |
| 28 | } |
| 29 | else |
| 30 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Invalid file path {}", path.c_str()); |
| 31 | |
| 32 | return datacxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 33 | } |
| 34 | |
| 35 | bool writeFilecxx . . $ Utils/File/writeFile(231cf0731af4b996).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path, stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> const &data) |
| 36 | { |
| 37 | stdcxx . . $ std/::ofstreamcxx . . $ std/ofstream# filecxx . . $ std/basic_ofstream#basic_ofstream(fbe7c9ddfb647a62).(path, stdcxx . . $ std/::ioscxx . . $ std/ios#::binarycxx . . $ std/ios_base#binary.); |
| 38 | if (file.is_opencxx . . $ std/basic_ofstream#is_open(c9857a73d68e59ba).()) { |
| 39 | file.writecxx . . $ std/basic_ostream#write(3c866fa06c201852).(data.datacxx . . $ std/vector#data(8e614c7de0b1573e).(), data.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); |
| 40 | return true; |
| 41 | } |
| 42 | else |
| 43 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot open file {}", path.c_str()); |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> readFileLinescxx . . $ Utils/File/readFileLines(951e54d16e2206f).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path, |
| 49 | char delim) |
| 50 | { |
| 51 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> entriescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 52 | |
| 53 | if (isFilePathValidcxx . . $ Utils/File/isFilePathValid(c53a6ef3278968b1).(path)) { |
| 54 | stdcxx . . $ std/::ifstreamcxx . . $ std/ifstream# filecxx . . $ std/basic_ifstream#basic_ifstream(fbe7c9ddfb647a62).(path); |
| 55 | if (file.is_opencxx . . $ std/basic_ifstream#is_open(c9857a73d68e59ba).()) { |
| 56 | for (stdcxx . . $ std/::stringcxx . . $ std/string# entrycxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; stdcxx . . $ std/::getlinecxx . . $ std/getline(30074676001a27e3).(file, entry, delim);) |
| 57 | entries.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(entry)); |
| 58 | } |
| 59 | else |
| 60 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot open file {}", path.c_str()); |
| 61 | } |
| 62 | else |
| 63 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Invalid file path {}", path.c_str()); |
| 64 | |
| 65 | return entriescxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 66 | } |
| 67 | |
| 68 | bool isFilePathValidcxx . . $ Utils/File/isFilePathValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 69 | { |
| 70 | try { |
| 71 | return fs::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(path) && fs::is_regular_filecxx . . $ std/filesystem/is_regular_file(c53a6ef3278968b1).(path); |
| 72 | } |
| 73 | catch (stdcxx . . $ std/::exception const &e) { |
| 74 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool isDirectoryPathValidcxx . . $ Utils/File/isDirectoryPathValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 81 | { |
| 82 | try { |
| 83 | return fs::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(path) && fs::is_directorycxx . . $ std/filesystem/is_directory(c53a6ef3278968b1).(path); |
| 84 | } |
| 85 | catch (stdcxx . . $ std/::exception const &e) { |
| 86 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> searchcxx . . $ Utils/File/search(3ad90eba8943f9ac).(stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex# const ®ex, |
| 93 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 94 | { |
| 95 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> pathscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 96 | |
| 97 | if (isDirectoryPathValidcxx . . $ Utils/File/isDirectoryPathValid(c53a6ef3278968b1).(path)) { |
| 98 | for (auto const &entry : fscxx . . $ std/filesystem/__cxx11/directory_iterator#directory_iterator(3f42e2c2fac0d5fc).::directory_iteratorcxx . . $ std/filesystem/__cxx11/directory_iterator#(path)) { |
| 99 | auto const &entryDirPath = entry.pathcxx . . $ std/filesystem/__cxx11/directory_entry#path(3df3a2cdf6f4d926).(); |
| 100 | stdcxx . . $ std/::stringcxx . . $ std/string# entryDirName((*cxx . . $ std/filesystem/__cxx11/path#iterator#`operator*`(3df3a2cdf6f4d926).(--cxx . . $ std/filesystem/__cxx11/path#iterator#operator--(7781942b74913a94).entryDirPath.endcxx . . $ std/filesystem/__cxx11/path#end(616f028af7126e1e).())).stringcxx . . $ std/filesystem/__cxx11/path#string(219584694fe2b248).()); |
| 101 | if (stdcxx . . $ std/::regex_searchcxx . . $ std/regex_search(dbfa5bd607d7b136).(entryDirName, regex)) |
| 102 | paths.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(entryDirPath); |
| 103 | } |
| 104 | } |
| 105 | else |
| 106 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Invalid directory path {}", path.c_str()); |
| 107 | |
| 108 | return pathscxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 109 | } |
| 110 | |
| 111 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> |
| 112 | findHWMonXDirectorycxx . . $ Utils/File/findHWMonXDirectory(9f128ed55dd5b6be).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 113 | { |
| 114 | stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex# const hwmonXRegexcxx . . $ std/__cxx11/basic_regex#basic_regex(d1adbc785ae219ab).(R"(hwmon[0-9]+)"); |
| 115 | auto paths = Utilscxx . . $ Utils/::Filecxx . . $ Utils/File/::searchcxx . . $ Utils/File/search(3ad90eba8943f9ac).(hwmonXRegex, path); |
| 116 | if (!paths.emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 117 | if (paths.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() > 1) { |
| 118 | SPDLOG_WARNcxx . . $ `<external>/13613935168682455490/spdlog.h:329:13`!("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 pathscxx . . $ std/optional#optional(e3f0d6382bd52f24)..frontcxx . . $ std/vector#front(6baabfa6fc7b3a8e).(); |
| 124 | } |
| 125 | |
| 126 | return {cxx . . $ std/optional#optional(ced63f7c635d850d).}; |
| 127 | } |
| 128 | |
| 129 | bool isSysFSEntryValidcxx . . $ Utils/File/isSysFSEntryValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) |
| 130 | { |
| 131 | if (!isFilePathValidcxx . . $ Utils/File/isFilePathValid(c53a6ef3278968b1).(path)) |
| 132 | return false; |
| 133 | |
| 134 | if (readFileLinescxx . . $ Utils/File/readFileLines(951e54d16e2206f).(path).emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 135 | SPDLOG_WARNcxx . . $ `<external>/13613935168682455490/spdlog.h:329:13`!("Empty sysfs entry {}", path.c_str()); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> readQrcFilecxx . . $ Utils/File/readQrcFile(cf0abd84c180245d).(stdcxx . . $ std/::string_viewcxx . . $ std/string_view# qrcPath) |
| 143 | { |
| 144 | QFilecxx . . $ QFile# filecxx . . $ QFile#QFile(3520e46b33348bf6).(qrcPathcxx . . $ QString#QString(85c52e162fed56f9)..datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).()); |
| 145 | if (file.opencxx . . $ QFile#open(da34f43c1eae080c).(QFilecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODeviceBase#ReadOnly.)) { |
| 146 | auto fileData = file.readAllcxx . . $ QIODevice#readAll(b3c286cec66e09f4).(); |
| 147 | return stdcxx . . $ std/vector#vector(13e0640cd3842f28).::vectorcxx . . $ std/vector#<char>(fileData.cbegincxx . . $ QByteArray#cbegin(ba32b354807b2b81).(), fileData.cendcxx . . $ QByteArray#cend(ba32b354807b2b81).()); |
| 148 | } |
| 149 | |
| 150 | return {cxx . . $ std/vector#vector(49f6e7a06ebc5aa8).}; |
| 151 | } |
| 152 | |
| 153 | } // namespace Utils::File |