|
a/src/utils/fstreewalk.cpp |
|
b/src/utils/fstreewalk.cpp |
1 |
#ifndef lint
|
1 |
#ifndef lint
|
2 |
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.3 2005-02-10 15:21:12 dockes Exp $ (C) 2004 J.F.Dockes";
|
2 |
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.4 2005-04-04 13:18:47 dockes Exp $ (C) 2004 J.F.Dockes";
|
3 |
#endif
|
3 |
#endif
|
4 |
|
4 |
|
5 |
#ifndef TEST_FSTREEWALK
|
5 |
#ifndef TEST_FSTREEWALK
|
6 |
|
6 |
|
7 |
#include <dirent.h>
|
7 |
#include <dirent.h>
|
8 |
#include <sys/stat.h>
|
8 |
#include <sys/stat.h>
|
9 |
#include <errno.h>
|
9 |
#include <errno.h>
|
|
|
10 |
#include <fnmatch.h>
|
10 |
|
11 |
|
11 |
#include <sstream>
|
12 |
#include <sstream>
|
|
|
13 |
#include <list>
|
12 |
|
14 |
|
13 |
#include "debuglog.h"
|
15 |
#include "debuglog.h"
|
14 |
#include "pathut.h"
|
16 |
#include "pathut.h"
|
15 |
#include "fstreewalk.h"
|
17 |
#include "fstreewalk.h"
|
16 |
|
18 |
|
17 |
using namespace std;
|
19 |
using namespace std;
|
18 |
|
20 |
|
19 |
class FsTreeWalker::Internal {
|
21 |
class FsTreeWalker::Internal {
|
20 |
Options options;
|
22 |
Options options;
|
21 |
stringstream reason;
|
23 |
stringstream reason;
|
|
|
24 |
list<string> skippedNames;
|
22 |
int errors;
|
25 |
int errors;
|
23 |
void logsyserr(const char *call, const string ¶m)
|
26 |
void logsyserr(const char *call, const string ¶m)
|
24 |
{
|
27 |
{
|
25 |
errors++;
|
28 |
errors++;
|
26 |
reason << call << "(" << param << ") : " << errno << " : " <<
|
29 |
reason << call << "(" << param << ") : " << errno << " : " <<
|
|
... |
|
... |
50 |
|
53 |
|
51 |
int FsTreeWalker::getErrCnt()
|
54 |
int FsTreeWalker::getErrCnt()
|
52 |
{
|
55 |
{
|
53 |
return data->errors;
|
56 |
return data->errors;
|
54 |
}
|
57 |
}
|
|
|
58 |
|
|
|
59 |
bool FsTreeWalker::addSkippedName(const string& pattern)
|
|
|
60 |
{
|
|
|
61 |
data->skippedNames.push_back(pattern);
|
|
|
62 |
return true;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
void FsTreeWalker::clearSkippedNames()
|
|
|
66 |
{
|
|
|
67 |
data->skippedNames.clear();
|
|
|
68 |
}
|
|
|
69 |
|
55 |
|
70 |
|
56 |
FsTreeWalker::Status FsTreeWalker::walk(const string &top,
|
71 |
FsTreeWalker::Status FsTreeWalker::walk(const string &top,
|
57 |
FsTreeWalkerCB& cb)
|
72 |
FsTreeWalkerCB& cb)
|
58 |
{
|
73 |
{
|
59 |
Status status = FtwOk;
|
74 |
Status status = FtwOk;
|
|
... |
|
... |
92 |
}
|
107 |
}
|
93 |
}
|
108 |
}
|
94 |
|
109 |
|
95 |
struct dirent *ent;
|
110 |
struct dirent *ent;
|
96 |
while ((ent = readdir(d)) != 0) {
|
111 |
while ((ent = readdir(d)) != 0) {
|
97 |
// We do process hidden files for now
|
112 |
// We do process hidden files for now, only skip . and ..
|
98 |
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
|
113 |
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
|
99 |
continue;
|
114 |
continue;
|
100 |
|
115 |
|
101 |
string fn = top;
|
116 |
if (!data->skippedNames.empty()) {
|
102 |
path_cat(fn, ent->d_name);
|
117 |
list<string>::const_iterator it;
|
103 |
|
118 |
for (it = data->skippedNames.begin();
|
104 |
struct stat st;
|
119 |
it != data->skippedNames.end(); it++) {
|
105 |
int statret = (data->options & FtwFollow) ? stat(fn.c_str(), &st) :
|
120 |
if (fnmatch(it->c_str(), ent->d_name, 0) == 0) {
|
106 |
lstat(fn.c_str(), &st);
|
121 |
//fprintf(stderr,
|
107 |
if (statret == -1) {
|
122 |
//"Skipping [%s] because of pattern match\n", ent->d_name);
|
108 |
data->logsyserr("stat", fn);
|
123 |
goto skip;
|
109 |
continue;
|
|
|
110 |
}
|
124 |
}
|
111 |
if (S_ISDIR(st.st_mode)) {
|
|
|
112 |
if (data->options & FtwNoRecurse) {
|
|
|
113 |
status = cb.processone(fn, &st, FtwDirEnter);
|
|
|
114 |
} else {
|
|
|
115 |
status=walk(fn, cb);
|
|
|
116 |
}
|
125 |
}
|
117 |
if (status & (FtwStop|FtwError))
|
126 |
}
|
118 |
goto out;
|
127 |
|
119 |
if ((status = cb.processone(top, &st, FtwDirReturn))
|
128 |
{
|
120 |
& (FtwStop|FtwError))
|
129 |
string fn = top;
|
121 |
goto out;
|
130 |
path_cat(fn, ent->d_name);
|
122 |
} else if (S_ISREG(st.st_mode)) {
|
131 |
|
123 |
if ((status = cb.processone(fn, &st, FtwRegular)) &
|
132 |
struct stat st;
|
124 |
(FtwStop|FtwError)) {
|
133 |
int statret = (data->options & FtwFollow) ? stat(fn.c_str(), &st) :
|
125 |
goto out;
|
134 |
lstat(fn.c_str(), &st);
|
|
|
135 |
if (statret == -1) {
|
|
|
136 |
data->logsyserr("stat", fn);
|
|
|
137 |
continue;
|
126 |
}
|
138 |
}
|
|
|
139 |
if (S_ISDIR(st.st_mode)) {
|
|
|
140 |
if (data->options & FtwNoRecurse) {
|
|
|
141 |
status = cb.processone(fn, &st, FtwDirEnter);
|
|
|
142 |
} else {
|
|
|
143 |
status=walk(fn, cb);
|
127 |
}
|
144 |
}
|
|
|
145 |
if (status & (FtwStop|FtwError))
|
|
|
146 |
goto out;
|
|
|
147 |
if ((status = cb.processone(top, &st, FtwDirReturn))
|
|
|
148 |
& (FtwStop|FtwError))
|
|
|
149 |
goto out;
|
|
|
150 |
} else if (S_ISREG(st.st_mode)) {
|
|
|
151 |
if ((status = cb.processone(fn, &st, FtwRegular)) &
|
|
|
152 |
(FtwStop|FtwError)) {
|
|
|
153 |
goto out;
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
skip: ;
|
|
|
159 |
|
128 |
// We skip other file types (devices etc...)
|
160 |
// We skip other file types (devices etc...)
|
129 |
}
|
161 |
}
|
130 |
|
162 |
|
131 |
out:
|
163 |
out:
|
132 |
if (d)
|
164 |
if (d)
|