comparison dep/animia/src/linux.cpp @ 62:4c6dd5999b39

*: update 1. updated animia 2. use widestrings for filesystem on Windows
author Paper <mrpapersonic@gmail.com>
date Sun, 01 Oct 2023 06:16:06 -0400
parents 6ff7aabeb9d7
children 1ce00c1c8ddc
comparison
equal deleted inserted replaced
61:327568ad9be9 62:4c6dd5999b39
1 #include <string> 1 #include <algorithm>
2 #include <vector> 2 #include <fcntl.h>
3 #include <filesystem>
3 #include <fstream> 4 #include <fstream>
4 #include <filesystem>
5 #include <unordered_map>
6 #include <iostream> 5 #include <iostream>
7 #include <sstream> 6 #include <sstream>
7 #include <string>
8 #include <sys/stat.h>
8 #include <unistd.h> 9 #include <unistd.h>
9 #include <sys/stat.h> 10 #include <unordered_map>
10 #include <fcntl.h> 11 #include <vector>
11 #include <algorithm>
12 12
13 #define PROC_LOCATION "/proc" 13 #define PROC_LOCATION "/proc"
14 14
15 namespace Animia::Linux { 15 namespace Animia::Linux {
16 16
31 std::string get_process_name(int pid) { 31 std::string get_process_name(int pid) {
32 std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm"; 32 std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/comm";
33 std::ifstream t(path); 33 std::ifstream t(path);
34 std::stringstream buf; 34 std::stringstream buf;
35 buf << t.rdbuf(); 35 buf << t.rdbuf();
36
36 std::string str = buf.str(); 37 std::string str = buf.str();
37 str.erase(std::remove(str.begin(), str.end(), '\n'), str.end()); 38 str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
38 return str; 39 return str;
39 } 40 }
40 41
77 if (!are_flags_ok(pid, std::stoi(dir.path().stem()))) 78 if (!are_flags_ok(pid, std::stoi(dir.path().stem())))
78 continue; 79 continue;
79 /* get filename size */ 80 /* get filename size */
80 int size = get_size_of_link(dir.path().string()); 81 int size = get_size_of_link(dir.path().string());
81 /* allocate buffer */ 82 /* allocate buffer */
82 char* buf = (char*)calloc(size, sizeof(char)); 83 std::string buf(size, '\0');
83 //std::string buf('\0', size); 84 // std::string buf('\0', size);
84 /* read filename to buffer */ 85 /* read filename to buffer */
85 int r = readlink(dir.path().c_str(), buf, size); 86 int r = readlink(dir.path().c_str(), &buf.front(), buf.length());
86 87
87 if (r < 0) 88 if (r < 0)
88 continue; 89 continue;
89 if (!is_regular_file(buf)) 90 if (!is_regular_file(buf))
90 continue; 91 continue;
91 ret.push_back(buf); 92 ret.push_back(buf);
92 free(buf);
93 } 93 }
94 } catch (std::filesystem::filesystem_error const& ex) { 94 } catch (std::filesystem::filesystem_error const& ex) {
95 if (ex.code().value() != 13) // 13 == permissions error, common with /proc, ignore 95 if (ex.code().value() != 13) // 13 == permissions error, common with /proc, ignore
96 throw; 96 throw;
97 } 97 }
99 } 99 }
100 100
101 std::unordered_map<int, std::vector<std::string>> get_all_open_files() { 101 std::unordered_map<int, std::vector<std::string>> get_all_open_files() {
102 std::unordered_map<int, std::vector<std::string>> map; 102 std::unordered_map<int, std::vector<std::string>> map;
103 std::vector<int> pids = get_all_pids(); 103 std::vector<int> pids = get_all_pids();
104 for (int i: pids) 104 for (int i : pids)
105 map[i] = get_open_files(i); 105 map[i] = get_open_files(i);
106 return map; 106 return map;
107 } 107 }
108 108
109 } 109 } // namespace Animia::Linux