Parent: [063727] (diff)

Child: [50b927] (diff)

Download this file

fstreewalk.cpp    177 lines (152 with data), 3.6 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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifndef lint
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.3 2005-02-10 15:21:12 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#ifndef TEST_FSTREEWALK
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <sstream>
#include "debuglog.h"
#include "pathut.h"
#include "fstreewalk.h"
using namespace std;
class FsTreeWalker::Internal {
Options options;
stringstream reason;
int errors;
void logsyserr(const char *call, const string &param)
{
errors++;
reason << call << "(" << param << ") : " << errno << " : " <<
strerror(errno) << endl;
}
friend class FsTreeWalker;
};
FsTreeWalker::FsTreeWalker(Options opts)
{
data = new Internal;
if (data) {
data->options = opts;
data->errors = 0;
}
}
FsTreeWalker::~FsTreeWalker()
{
delete data;
}
string FsTreeWalker::getReason()
{
return data->reason.str();
}
int FsTreeWalker::getErrCnt()
{
return data->errors;
}
FsTreeWalker::Status FsTreeWalker::walk(const string &top,
FsTreeWalkerCB& cb)
{
Status status = FtwOk;
struct stat st;
int statret;
// Handle the top entry
statret = (data->options & FtwFollow) ? stat(top.c_str(), &st) :
lstat(top.c_str(), &st);
if (statret == -1) {
data->logsyserr("stat", top);
return FtwError;
}
if (S_ISDIR(st.st_mode)) {
if ((status = cb.processone(top, &st, FtwDirEnter)) &
(FtwStop|FtwError)) {
return status;
}
} else if (S_ISREG(st.st_mode)) {
return cb.processone(top, &st, FtwRegular);
} else {
return status;
}
// Handle directory entries
DIR *d = opendir(top.c_str());
if (d == 0) {
data->logsyserr("opendir", top);
switch (errno) {
case EPERM:
case EACCES:
goto out;
default:
status = FtwError;
goto out;
}
}
struct dirent *ent;
while ((ent = readdir(d)) != 0) {
// We do process hidden files for now
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
string fn = top;
path_cat(fn, ent->d_name);
struct stat st;
int statret = (data->options & FtwFollow) ? stat(fn.c_str(), &st) :
lstat(fn.c_str(), &st);
if (statret == -1) {
data->logsyserr("stat", fn);
continue;
}
if (S_ISDIR(st.st_mode)) {
if (data->options & FtwNoRecurse) {
status = cb.processone(fn, &st, FtwDirEnter);
} else {
status=walk(fn, cb);
}
if (status & (FtwStop|FtwError))
goto out;
if ((status = cb.processone(top, &st, FtwDirReturn))
& (FtwStop|FtwError))
goto out;
} else if (S_ISREG(st.st_mode)) {
if ((status = cb.processone(fn, &st, FtwRegular)) &
(FtwStop|FtwError)) {
goto out;
}
}
// We skip other file types (devices etc...)
}
out:
if (d)
closedir(d);
return status;
}
#else // TEST_FSTREEWALK
#include <sys/stat.h>
#include <iostream>
#include "fstreewalk.h"
using namespace std;
class myCB : public FsTreeWalkerCB {
public:
FsTreeWalker::Status processone(const string &path,
const struct stat *st,
FsTreeWalker::CbFlag flg)
{
if (flg == FsTreeWalker::FtwDirEnter) {
cout << "[Entering " << path << "]" << endl;
} else if (flg == FsTreeWalker::FtwDirReturn) {
cout << "[Returning to " << path << "]" << endl;
} else if (flg == FsTreeWalker::FtwRegular) {
cout << path << endl;
}
return FsTreeWalker::FtwOk;
}
};
int main(int argc, const char **argv)
{
if (argc != 2) {
cerr << "Usage: fstreewalk <topdir>" << endl;
exit(1);
}
FsTreeWalker walker;
myCB cb;
walker.walk(argv[1], cb);
if (walker.getErrCnt() > 0)
cout << walker.getReason();
}
#endif // TEST_FSTREEWALK