diff dep/animia/src/fd/linux.cc @ 152:8700806c2cc2

dep/animia: awesome new breaking changes! I'm so tired
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 02:34:59 -0500
parents 54744a48a7d7
children d43d68408d3c
line wrap: on
line diff
--- a/dep/animia/src/fd/linux.cc	Tue Nov 14 16:31:21 2023 -0500
+++ b/dep/animia/src/fd/linux.cc	Wed Nov 15 02:34:59 2023 -0500
@@ -1,10 +1,10 @@
 #include "animia/util.h"
 #include "animia/fd/linux.h"
+#include "animia.h"
 
 #include <algorithm>
 #include <filesystem>
 #include <fstream>
-#include <iostream>
 #include <sstream>
 #include <string>
 #include <unordered_map>
@@ -99,7 +99,17 @@
 	return ret.c_str();
 }
 
-bool LinuxFdTools::GetAllPids(std::set<pid_t>& pids) {
+static bool GetProcessName(pid_t pid, std::string& result) {
+	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
+
+	if (!util::ReadFile(path, result))
+		return false;
+
+	result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
+	return true;
+}
+
+bool LinuxFdTools::EnumerateOpenProcesses(process_proc_t process_proc) {
 	bool success = false;
 	for (const auto& dir : GetAllFilesInDir(PROC_LOCATION)) {
 		pid_t pid;
@@ -109,22 +119,16 @@
 		} catch (std::invalid_argument) {
 			continue;
 		}
-		pids.insert(pid);
+		if (!process_proc({pid, GetProcessName(pid)}))
+			return false;
 	}
 	return success;
 }
 
-bool LinuxFdTools::GetProcessName(pid_t pid, std::string& result) {
-	const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
-
-	if (!util::ReadFile(path, result))
+bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, open_file_proc_t open_file_proc) {
+	if (!open_file_proc)
 		return false;
 
-	result.erase(std::remove(result.begin(), result.end(), '\n'), result.end());
-	return true;
-}
-
-bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) {
 	for (const auto& pid : pids) {
 		const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
 
@@ -137,7 +141,8 @@
 			if (!IsRegularFile(name))
 				continue;
 
-			files.push_back({pid, name});
+			if (!open_file_proc({pid, name}))
+				return false;
 		}
 	}
 	return true;