-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.cpp
More file actions
92 lines (77 loc) · 1.87 KB
/
fs.cpp
File metadata and controls
92 lines (77 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "fs.h"
using namespace std;
using namespace pas;
inline bool HasEnding (string const & fullString, string const & ending)
{
bool rv = false;
if (fullString.length() >= ending.length())
{
rv = (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
}
return rv;
}
bool HasAllowedExtension(const char * file_name, const vector<string> & allowable_extensions)
{
bool ok_extension = false;
string s(file_name);
for (auto it = allowable_extensions.begin(); it < allowable_extensions.end(); it++)
{
if (HasEnding(s, *it))
{
ok_extension = true;
break;
}
}
return ok_extension;
}
bool CausesLoop(const char * path)
{
return (strcmp(path, ".") == 0 || strcmp(path, "..") == 0);
}
void Discover(string path, const vector<string> & allowable_extensions, vector<DIRENT> & t, int parent, int & c, bool is_root, Logger & _log_)
{
//sleep(1);
LOG(_log_, "Discover entered: " + path);
const string slash("/");
DIR * d = nullptr;
dirent * entry;
int me = c++;
DIRENT r;
r.name = (is_root) ? path.c_str() : string(basename((char *) path.c_str()));
r.type = DT_DIR;
r.up = parent;
r.me = me;
t.push_back(r);
if (!(d = opendir(path.c_str())))
throw LOG(_log_, "opendir failed: " + path);
while ((entry = readdir(d)) != nullptr)
{
if (entry->d_type == DT_DIR)
{
if (CausesLoop(entry->d_name))
{
continue;
}
string next_path = path + slash + string(entry->d_name);
Discover(next_path, allowable_extensions, t, r.me, c, false, _log_);
}
else if (entry->d_type == DT_REG)
{
if (entry->d_name[0] == '.')
continue;
if (!HasAllowedExtension(entry->d_name, allowable_extensions))
{
continue;
}
DIRENT f;
f.name = string(entry->d_name);
f.fullpath = path + "/" + f.name;
f.type = DT_REG;
f.up = me;
f.me = -1;
t.push_back(f);
}
}
if (d != nullptr)
closedir(d);
}