comparison dep/animia/src/fd/linux.cc @ 164:99fdf5a90b0f

fd/linux: avoid reading buffers multiple times
author Paper <mrpapersonic@gmail.com>
date Sat, 18 Nov 2023 00:54:29 -0500
parents 44c5e6dd9488
children
comparison
equal deleted inserted replaced
163:44c5e6dd9488 164:99fdf5a90b0f
50 return S_ISREG(sb.st_mode); 50 return S_ISREG(sb.st_mode);
51 } 51 }
52 52
53 static bool AreFlagsOk(pid_t pid, int fd) { 53 static bool AreFlagsOk(pid_t pid, int fd) {
54 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd); 54 const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fdinfo/" + std::to_string(fd);
55 std::stringstream buffer; 55
56 { 56 std::ifstream file(path.c_str());
57 std::string data; 57 if (!file)
58 if (!util::ReadFile(path, data)) 58 return false;
59 return false;
60 buffer << data;
61 }
62 59
63 int flags = 0; 60 int flags = 0;
64 for (std::string line; std::getline(buffer, line);) { 61 for (std::string line; std::getline(file, line); )
65 /* FIXME: exception handling here!! */ 62 if (line.find("flags:", 0) == 0)
66 if (line.rfind("flags:", 0) == 0) {
67 flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1)); 63 flags = std::stoi(line.substr(line.find_last_not_of("0123456789") + 1));
68 } 64
69 }
70 if (flags & O_WRONLY || flags & O_RDWR) 65 if (flags & O_WRONLY || flags & O_RDWR)
71 return false; 66 return false;
72 return true; 67 return true;
73 } 68 }
74 69