comparison dep/animone/src/fd/proc.cc @ 365:f81bed4e04ac

*: megacommit that probably breaks things
author Paper <paper@paper.us.eu.org>
date Wed, 02 Oct 2024 23:06:43 -0400
parents a7d4e5107531
children
comparison
equal deleted inserted replaced
364:99c961c91809 365:f81bed4e04ac
19 19
20 static constexpr std::string_view PROC_LOCATION = "/proc"; 20 static constexpr std::string_view PROC_LOCATION = "/proc";
21 21
22 namespace animone::internal::proc { 22 namespace animone::internal::proc {
23 23
24 static bool IsRegularFile(std::string link) { 24 struct Fdinfo {
25 bool Parse(std::istream& istr);
26 std::unordered_map<std::string, std::string> data;
27 };
28
29 bool Fdinfo::Parse(std::istream& istr) {
30 /* shift to the start of the stream */
31 istr.seekg(0);
32
33 for (std::string line; std::getline(istr, line); ) {
34 if (line.empty()) /* huh? */
35 continue;
36
37 std::size_t colon = line.find(':');
38 if (colon == std::string::npos)
39 return false;
40
41 std::string key = line.substr(0, colon);
42 std::string value = line.substr(colon + 1);
43 util::TrimLeft(value, " ");
44
45 data[key] = value;
46 }
47
48 return true;
49 }
50
51 static bool IsRegularFile(const std::string& link) {
25 struct stat sb; 52 struct stat sb;
26 if (stat(link.c_str(), &sb) == -1) 53 if (stat(link.c_str(), &sb) == -1)
27 return false; 54 return false;
28 55
29 return S_ISREG(sb.st_mode); 56 return S_ISREG(sb.st_mode);
35 62
36 std::ifstream file(path); 63 std::ifstream file(path);
37 if (!file) 64 if (!file)
38 return false; 65 return false;
39 66
40 int flags = 0; 67 Fdinfo fdinfo;
41 for (std::string line; std::getline(file, line);) 68 if (!fdinfo.Parse(file))
42 if (line.find("flags:", 0) == 0) 69 return false;
43 flags = util::StringToInt(line.substr(line.find_last_not_of("0123456789") + 1)); 70
71 if (fdinfo.data.find("flags") == fdinfo.data.end())
72 return false;
44 73
45 /* check if the file was opened in a write mode */ 74 /* check if the file was opened in a write mode */
46 int accflags = flags & O_ACCMODE; 75 try {
47 if (accflags == O_WRONLY || accflags == O_RDWR) 76 const long long accflags = std::stoll(fdinfo.data["flags"]) & O_ACCMODE;
48 return false; 77
78 if (accflags == O_WRONLY || accflags == O_RDWR)
79 return false;
80 } catch (const std::exception& ex) {
81 return false; /* huh ? */
82 }
49 83
50 return true; 84 return true;
51 } 85 }
52 86
53 static bool GetFilenameFromFd(std::string link, std::string& out) { 87 static bool GetFilenameFromFd(std::string link, std::string& out) {