Mercurial > minori
diff 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 |
line wrap: on
line diff
--- a/dep/animone/src/fd/proc.cc Tue Jul 16 21:15:59 2024 -0400 +++ b/dep/animone/src/fd/proc.cc Wed Oct 02 23:06:43 2024 -0400 @@ -21,7 +21,34 @@ namespace animone::internal::proc { -static bool IsRegularFile(std::string link) { +struct Fdinfo { + bool Parse(std::istream& istr); + std::unordered_map<std::string, std::string> data; +}; + +bool Fdinfo::Parse(std::istream& istr) { + /* shift to the start of the stream */ + istr.seekg(0); + + for (std::string line; std::getline(istr, line); ) { + if (line.empty()) /* huh? */ + continue; + + std::size_t colon = line.find(':'); + if (colon == std::string::npos) + return false; + + std::string key = line.substr(0, colon); + std::string value = line.substr(colon + 1); + util::TrimLeft(value, " "); + + data[key] = value; + } + + return true; +} + +static bool IsRegularFile(const std::string& link) { struct stat sb; if (stat(link.c_str(), &sb) == -1) return false; @@ -37,15 +64,22 @@ if (!file) return false; - int flags = 0; - for (std::string line; std::getline(file, line);) - if (line.find("flags:", 0) == 0) - flags = util::StringToInt(line.substr(line.find_last_not_of("0123456789") + 1)); + Fdinfo fdinfo; + if (!fdinfo.Parse(file)) + return false; + + if (fdinfo.data.find("flags") == fdinfo.data.end()) + return false; /* check if the file was opened in a write mode */ - int accflags = flags & O_ACCMODE; - if (accflags == O_WRONLY || accflags == O_RDWR) - return false; + try { + const long long accflags = std::stoll(fdinfo.data["flags"]) & O_ACCMODE; + + if (accflags == O_WRONLY || accflags == O_RDWR) + return false; + } catch (const std::exception& ex) { + return false; /* huh ? */ + } return true; }