comparison dep/animia/src/fd/win32.cc @ 150:ffa535b6d630

*: avoid usage of std::[pair,tuple] https://arne-mertz.de/2017/03/smelly-pair-tuple/ it's better to use real structures and such where variables are easily known... also apparently using [] on structs is actually valid? I had no idea.
author Paper <mrpapersonic@gmail.com>
date Tue, 14 Nov 2023 16:27:33 -0500
parents aa4df5a84338
children 54744a48a7d7
comparison
equal deleted inserted replaced
149:e41505d24733 150:ffa535b6d630
222 } 222 }
223 223
224 bool Win32FdTools::GetProcessName(pid_t pid, std::string& result) { 224 bool Win32FdTools::GetProcessName(pid_t pid, std::string& result) {
225 unsigned long ret_size = 0; // size given by GetModuleBaseNameW 225 unsigned long ret_size = 0; // size given by GetModuleBaseNameW
226 Handle handle(::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); 226 Handle handle(::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
227 if (!handle.get()) 227 if (handle.get() == INVALID_HANDLE_VALUE)
228 return false; 228 return false;
229 229
230 /* agh... */ 230 /* agh... */
231 std::wstring ret(256, L'\0'); 231 std::wstring ret(256, L'\0');
232 for (; ret.length() < 32768; ret.resize(ret.length() * 2)) { 232 for (; ret.length() < 32768; ret.resize(ret.length() * 2)) {
241 241
242 return true; 242 return true;
243 } 243 }
244 244
245 /* this could be changed to being a callback, but... I'm too lazy right now :) */ 245 /* this could be changed to being a callback, but... I'm too lazy right now :) */
246 bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) { 246 bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) {
247 std::unordered_map<pid_t, Handle> proc_handles; 247 std::unordered_map<pid_t, Handle> proc_handles;
248 248
249 for (const pid_t& pid : pids) { 249 for (const pid_t& pid : pids) {
250 const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid); 250 const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid);
251 if (handle) 251 if (handle != INVALID_HANDLE_VALUE)
252 proc_handles[pid] = Handle(handle); 252 proc_handles[pid] = Handle(handle);
253 } 253 }
254 254
255 if (proc_handles.empty()) 255 if (proc_handles.empty())
256 return false; 256 return false;
267 267
268 if (!IsFileMaskOk(h.GrantedAccess)) 268 if (!IsFileMaskOk(h.GrantedAccess))
269 continue; 269 continue;
270 270
271 Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue)); 271 Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue));
272 if (!handle.get()) 272 if (handle.get() == INVALID_HANDLE_VALUE)
273 continue; 273 continue;
274 274
275 if (GetFileType(handle.get()) != FILE_TYPE_DISK) 275 if (GetFileType(handle.get()) != FILE_TYPE_DISK)
276 continue; 276 continue;
277 277
278 const std::string path = GetFinalPathNameByHandle(handle.get()); 278 const std::string path = GetFinalPathNameByHandle(handle.get());
279 if (!IsFilePathOk(path)) 279 if (!IsFilePathOk(path))
280 continue; 280 continue;
281 281
282 /* create an empty vector if it doesn't exist, probably unnecessary */ 282 OpenFile file;
283 files.push_back({pid, path}); 283 file.pid = pid;
284 file.path = path;
285
286 files.push_back(file);
284 } 287 }
285 288
286 return true; 289 return true;
287 } 290 }
288 291