# HG changeset patch # User Paper # Date 1700286869 18000 # Node ID 99fdf5a90b0f8a8a4c58c1c06a064a067495cc88 # Parent 44c5e6dd94885b9c8c347a70a783e613e0b9f0ff fd/linux: avoid reading buffers multiple times diff -r 44c5e6dd9488 -r 99fdf5a90b0f dep/animia/src/fd/linux.cc --- a/dep/animia/src/fd/linux.cc Sat Nov 18 00:47:40 2023 -0500 +++ b/dep/animia/src/fd/linux.cc Sat Nov 18 00:54:29 2023 -0500 @@ -52,21 +52,16 @@ static bool AreFlagsOk(pid_t pid, int fd) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); - std::stringstream buffer; - { - std::string data; - if (!util::ReadFile(path, data)) - return false; - buffer << data; - } + + std::ifstream file(path.c_str()); + if (!file) + return false; int flags = 0; - for (std::string line; std::getline(buffer, line);) { - /* FIXME: exception handling here!! */ - if (line.rfind("flags:", 0) == 0) { + for (std::string line; std::getline(file, line); ) + if (line.find("flags:", 0) == 0) flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1)); - } - } + if (flags & O_WRONLY || flags & O_RDWR) return false; return true;