Skip to content

Commit 330f154

Browse files
committed
Reduced custom filesystem code.
1 parent 6d323ad commit 330f154

File tree

2 files changed

+39
-159
lines changed

2 files changed

+39
-159
lines changed

NativeCore/Unix/EnumerateProcesses.cpp

Lines changed: 31 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,45 @@
22
#include <string>
33
#include <sstream>
44
#include <fstream>
5-
6-
#include "NativeCore.hpp"
7-
8-
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
9-
//#define USE_STD_FILESYSTEM
10-
#ifdef USE_STD_FILESYSTEM
115
#include <experimental/filesystem>
12-
#else
13-
#include <dirent.h>
14-
#include <sys/stat.h>
15-
#include <unistd.h>
16-
17-
class path
18-
{
19-
public:
20-
path() = default;
21-
22-
path(const char* _path)
23-
: path(std::string(_path))
24-
{
25-
}
26-
27-
path(const std::string& _path)
28-
: buf(_path)
29-
{
30-
}
31-
32-
path(const path&) = default;
33-
34-
path& append(const std::string& part)
35-
{
36-
if (buf.back() != separator)
37-
{
38-
buf += separator;
39-
}
40-
buf += part;
416

42-
return *this;
43-
}
44-
45-
path& operator/=(const path& p)
46-
{
47-
append(p.buf);
48-
49-
return *this;
50-
}
51-
52-
const std::string& string() const
53-
{
54-
return buf;
55-
}
56-
57-
const char* c_str() const
58-
{
59-
return buf.c_str();
60-
}
61-
62-
private:
63-
static const char separator = '/';
64-
65-
std::string buf;
66-
};
67-
68-
inline path operator/(const path& lhs, const path& rhs)
69-
{
70-
return path(lhs) /= rhs;
71-
}
72-
73-
enum class FileType
74-
{
75-
Unknown,
7+
#include "NativeCore.hpp"
768

77-
File,
78-
Directory,
79-
Symlink
80-
};
9+
namespace fs = std::experimental::filesystem;
8110

82-
FileType file_type(const path& p)
83-
{
84-
struct stat path_stat = {};
85-
if (::lstat(p.c_str(), &path_stat) == 0)
86-
{
87-
if (S_ISREG(path_stat.st_mode))
88-
{
89-
return FileType::File;
90-
}
91-
else if (S_ISDIR(path_stat.st_mode))
92-
{
93-
return FileType::Directory;
94-
}
95-
else if (S_ISLNK(path_stat.st_mode))
96-
{
97-
return FileType::Symlink;
98-
}
99-
}
100-
101-
return FileType::Unknown;
102-
}
11+
// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
12+
#define USE_CUSTOM_READ_SYMLINK
10313

104-
inline bool is_directory(const path& p)
105-
{
106-
return file_type(p) == FileType::Directory;
107-
}
14+
#ifdef USE_CUSTOM_READ_SYMLINK
15+
#include <unistd.h>
10816

109-
inline bool is_symlink(const path& p)
17+
fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
11018
{
111-
return file_type(p) == FileType::Symlink;
112-
}
19+
fs::path symlink_path;
11320

114-
bool read_symlink(const path& p, path& out_p)
115-
{
11621
std::string temp(64, '\0');
117-
11822
for (;; temp.resize(temp.size() * 2))
11923
{
12024
ssize_t result;
12125
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
12226
{
123-
return false;
27+
ec.assign(errno, std::system_category());
28+
break;
12429
}
12530
else
12631
{
12732
if (result != (ssize_t)temp.size())
12833
{
129-
out_p = path(std::string(temp.begin(), temp.begin() + result));
34+
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));
35+
36+
ec.clear();
13037

131-
return true;
38+
break;
13239
}
13340
}
13441
}
42+
43+
return symlink_path;
13544
}
13645

13746
#endif
@@ -194,57 +103,33 @@ extern "C" void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
194103
return;
195104
}
196105

197-
#ifdef USE_STD_FILESYSTEM
198-
using namespace std::experimental::filesystem;
199-
using namespace std;
200-
201-
path procPath("/proc");
202-
if (is_directory(procPath))
106+
fs::path procPath("/proc");
107+
if (fs::is_directory(procPath))
203108
{
204-
for (auto& d : directory_iterator(procPath))
109+
for (auto& d : fs::directory_iterator(procPath))
205110
{
206-
if (is_directory(d))
111+
if (fs::is_directory(d))
207112
{
208-
auto pidPath = d.path();
113+
auto processPath = d.path();
209114

210115
auto name = processPath.filename().string();
211116
if (is_number(name))
212117
{
213118
auto exeSymLink = processPath / "exe";
214-
if (is_symlink(symlink_status(exeSymLink)))
119+
if (fs::is_symlink(fs::symlink_status(exeSymLink)))
215120
{
216-
error_code ec;
217-
auto linkPath = read_symlink(exeSymLink, ec).string();
121+
std::error_code ec;
122+
auto linkPath =
123+
#ifdef USE_CUSTOM_READ_SYMLINK
124+
my_read_symlink
125+
#else
126+
read_symlink
127+
#endif
128+
(exeSymLink, ec).string();
218129
if (!ec)
219130
{
220-
#else
221-
path procPath("/proc");
222-
if (is_directory(procPath))
223-
{
224-
auto directory = opendir(procPath.c_str());
225-
if (directory == nullptr)
226-
{
227-
return;
228-
}
131+
auto auxvPath = processPath / "auxv";
229132

230-
struct dirent *entry;
231-
while ((entry = readdir(directory)) != nullptr)
232-
{
233-
auto pidPath = procPath / entry->d_name;
234-
if (is_directory(pidPath))
235-
{
236-
std::string name(entry->d_name);
237-
if (is_number(name))
238-
{
239-
auto exePath = pidPath / "exe";
240-
if (is_symlink(exePath))
241-
{
242-
path linkPath;
243-
if (read_symlink(exePath, linkPath))
244-
{
245-
#endif
246-
auto auxvPath = pidPath / "auxv";
247-
248133
auto platform = GetProcessPlatform(auxvPath.string());
249134
#ifdef NATIVE_CORE_64
250135
if (platform == Platform::X64)

NativeCore/Unix/NativeCore.Unix.vcxproj

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@
132132
<CppLanguageStandard>c++1y</CppLanguageStandard>
133133
</ClCompile>
134134
<Link>
135-
<LibraryDependencies>
136-
</LibraryDependencies>
135+
<LibraryDependencies>stdc++fs</LibraryDependencies>
137136
</Link>
138137
</ItemDefinitionGroup>
139138
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
@@ -142,8 +141,7 @@
142141
<CppLanguageStandard>c++1y</CppLanguageStandard>
143142
</ClCompile>
144143
<Link>
145-
<LibraryDependencies>
146-
</LibraryDependencies>
144+
<LibraryDependencies>stdc++fs</LibraryDependencies>
147145
</Link>
148146
</ItemDefinitionGroup>
149147
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -155,9 +153,8 @@
155153
<AdditionalOptions>-m64 %(AdditionalOptions)</AdditionalOptions>
156154
</ClCompile>
157155
<Link>
158-
<AdditionalOptions>%(AdditionalOptions)</AdditionalOptions>
159-
<LibraryDependencies>
160-
</LibraryDependencies>
156+
<AdditionalOptions>-m64 %(AdditionalOptions)</AdditionalOptions>
157+
<LibraryDependencies>stdc++fs</LibraryDependencies>
161158
</Link>
162159
</ItemDefinitionGroup>
163160
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -169,8 +166,8 @@
169166
<AdditionalOptions>-m64 %(AdditionalOptions)</AdditionalOptions>
170167
</ClCompile>
171168
<Link>
172-
<LibraryDependencies>
173-
</LibraryDependencies>
169+
<LibraryDependencies>stdc++fs</LibraryDependencies>
170+
<AdditionalOptions>-m64 %(AdditionalOptions)</AdditionalOptions>
174171
</Link>
175172
</ItemDefinitionGroup>
176173
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
@@ -182,8 +179,7 @@
182179
</ClCompile>
183180
<Link>
184181
<AdditionalOptions>-m32 %(AdditionalOptions)</AdditionalOptions>
185-
<LibraryDependencies>
186-
</LibraryDependencies>
182+
<LibraryDependencies>stdc++fs</LibraryDependencies>
187183
</Link>
188184
</ItemDefinitionGroup>
189185
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
@@ -195,8 +191,7 @@
195191
</ClCompile>
196192
<Link>
197193
<AdditionalOptions>-m32 %(AdditionalOptions)</AdditionalOptions>
198-
<LibraryDependencies>
199-
</LibraryDependencies>
194+
<LibraryDependencies>stdc++fs</LibraryDependencies>
200195
</Link>
201196
</ItemDefinitionGroup>
202197
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

0 commit comments

Comments
 (0)