changeset 78:1ce00c1c8ddc

dep/animia: update to upstream
author Paper <mrpapersonic@gmail.com>
date Wed, 11 Oct 2023 12:16:15 -0400
parents 6f7385bd334c
children c489dd4434af
files dep/animia/CMakeLists.txt dep/animia/src/bsd.cpp dep/animia/src/linux.cpp src/track/media.cpp
diffstat 4 files changed, 88 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/dep/animia/CMakeLists.txt	Fri Oct 06 06:18:53 2023 -0400
+++ b/dep/animia/CMakeLists.txt	Wed Oct 11 12:16:15 2023 -0400
@@ -11,14 +11,10 @@
 	list(APPEND SRC_FILES src/win32.cpp)
 endif()
 add_library(animia SHARED ${SRC_FILES})
-set_target_properties(animia PROPERTIES CXX_STANDARD 17)
+set_target_properties(animia PROPERTIES
+    PUBLIC_HEADER animia/animia.h CXX_STANDARD 11)
 target_include_directories(animia PRIVATE include)
 
-install(TARGETS animia
-        ARCHIVE DESTINATION lib
-        LIBRARY DESTINATION lib
-        RUNTIME DESTINATION bin)
-
 if(BUILD_TESTS)
 	project(test LANGUAGES CXX)
 	add_executable(test test/main.cpp)
--- a/dep/animia/src/bsd.cpp	Fri Oct 06 06:18:53 2023 -0400
+++ b/dep/animia/src/bsd.cpp	Wed Oct 11 12:16:15 2023 -0400
@@ -78,8 +78,18 @@
 	/* note: this is OS X only right now. eventually, I'll find a way
 	   to do this in FreeBSD, OpenBSD and the like */
 	std::vector<std::string> ret;
+
+	if (pid == 0)
+		return ret;
+
 	int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
+	if (bufsz == -1)
+		return ret;
+
 	struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz);
+	if (!info)
+		return ret;
+
 	proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz);
 
 	// iterate over stuff
@@ -87,7 +97,14 @@
 	for (int i = 0; i < bufsz / sizeof(info[0]); i++) {
 		if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) {
 			struct vnode_fdinfowithpath vnodeInfo;
-			proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
+
+			int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE);
+			if (sz != PROC_PIDFDVNODEPATHINFO_SIZE)
+				continue;
+
+			if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR)
+				continue;
+
 			ret.push_back(vnodeInfo.pvip.vip_path);
 		}
 	}
--- a/dep/animia/src/linux.cpp	Fri Oct 06 06:18:53 2023 -0400
+++ b/dep/animia/src/linux.cpp	Wed Oct 11 12:16:15 2023 -0400
@@ -9,22 +9,52 @@
 #include <unistd.h>
 #include <unordered_map>
 #include <vector>
+#include <cstring>
+#include <dirent.h>
 
 #define PROC_LOCATION "/proc"
 
 namespace Animia::Linux {
 
+std::vector<std::string> get_all_files_in_dir(const std::string& _dir) {
+	std::vector<std::string> ret;
+
+	DIR* dir = opendir(_dir.c_str());
+	if (!dir)
+		return ret;
+
+	struct dirent* dp;
+	while ((dp = readdir(dir)) != NULL) {
+		if (!(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")))
+			ret.push_back(_dir + "/" + dp->d_name);
+	}
+
+	closedir(dir);
+	return ret;
+}
+
+std::string basename(const std::string& path) {
+	return path.substr(path.find_last_of("/") + 1, path.length());
+}
+
+std::string stem(const std::string& path) {
+	std::string bn = basename(path);
+	return bn.substr(0, path.find_last_of("."));
+}
+
 std::vector<int> get_all_pids() {
 	std::vector<int> ret;
-	for (const auto& dir : std::filesystem::directory_iterator(PROC_LOCATION)) {
+
+	for (const auto& dir : get_all_files_in_dir(PROC_LOCATION)) {
 		int pid;
 		try {
-			pid = std::stoi(dir.path().stem());
+			pid = std::stoi(basename(dir));
 		} catch (std::invalid_argument) {
 			continue;
 		}
 		ret.push_back(pid);
 	}
+
 	return ret;
 }
 
@@ -63,37 +93,44 @@
 	return true;
 }
 
-static int get_size_of_link(std::string link) {
-	struct stat sb;
-	if (lstat(link.c_str(), &sb) == -1)
-		return -1;
-	return sb.st_size + 1;
+static std::string get_name_from_fd(std::string link) {
+	size_t  exe_size = 1024;
+	ssize_t exe_used;
+	std::string ret;
+	while (1) {
+		ret = std::string(exe_size, '\0');
+		exe_used = readlink(link.c_str(), &ret.front(), ret.length());
+		if (exe_used == (ssize_t)-1)
+			return NULL;
+
+		if (exe_used < (ssize_t)1) {
+			errno = ENOENT;
+			return NULL;
+		}
+
+		if (exe_used < (ssize_t)(exe_size - 1))
+			break;
+
+		exe_size += 1024;
+	}
+
+	return ret.c_str();
 }
 
 std::vector<std::string> get_open_files(int pid) {
 	std::vector<std::string> ret;
 	std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd";
-	try {
-		for (const auto& dir : std::filesystem::directory_iterator(path)) {
-			if (!are_flags_ok(pid, std::stoi(dir.path().stem())))
-				continue;
-			/* get filename size */
-			int size = get_size_of_link(dir.path().string());
-			/* allocate buffer */
-			std::string buf(size, '\0');
-			// std::string buf('\0', size);
-			/* read filename to buffer */
-			int r = readlink(dir.path().c_str(), &buf.front(), buf.length());
+
+	for (const auto& dir : get_all_files_in_dir(path)) {
+		if (!are_flags_ok(pid, std::stoi(basename(dir))))
+			continue;
 
-			if (r < 0)
-				continue;
-			if (!is_regular_file(buf))
-				continue;
-			ret.push_back(buf);
-		}
-	} catch (std::filesystem::filesystem_error const& ex) {
-		if (ex.code().value() != 13) // 13 == permissions error, common with /proc, ignore
-			throw;
+		std::string buf = get_name_from_fd(dir);
+
+		if (!is_regular_file(buf))
+			continue;
+
+		ret.push_back(buf);
 	}
 	return ret;
 }
--- a/src/track/media.cpp	Fri Oct 06 06:18:53 2023 -0400
+++ b/src/track/media.cpp	Wed Oct 11 12:16:15 2023 -0400
@@ -5,6 +5,7 @@
 #include "core/strings.h"
 #include <string>
 #include <vector>
+#include <QDebug>
 
 namespace Track {
 namespace Media {
@@ -13,11 +14,12 @@
 	/* getting all open files */
 	std::vector<int> pids = Animia::get_all_pids();
 	for (int i : pids) {
-		if (Animia::get_process_name(i) == "mpc-hc64.exe") {
+		if (Animia::get_process_name(i) == "vlc") {
 			std::vector<std::string> files = Animia::filter_system_files(Animia::get_open_files(i));
 			for (std::string s : files) {
+				qDebug() << Strings::ToQString(s);
 				Filesystem::Path p(s);
-				if (p.Extension() == "mkv")
+				if (p.Extension() == "mp4")
 					return p;
 			}
 		}