Switch to unified view

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.8 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.9 2006-12-21 08:22:35 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
/*
4
/*
5
 *   This program is free software; you can redistribute it and/or modify
5
 *   This program is free software; you can redistribute it and/or modify
6
 *   it under the terms of the GNU General Public License as published by
6
 *   it under the terms of the GNU General Public License as published by
7
 *   the Free Software Foundation; either version 2 of the License, or
7
 *   the Free Software Foundation; either version 2 of the License, or
...
...
38
38
39
class FsTreeWalker::Internal {
39
class FsTreeWalker::Internal {
40
    Options options;
40
    Options options;
41
    stringstream reason;
41
    stringstream reason;
42
    list<string> skippedNames;
42
    list<string> skippedNames;
43
    list<string> skippedPaths;
43
    int errors;
44
    int errors;
44
    void logsyserr(const char *call, const string &param) 
45
    void logsyserr(const char *call, const string &param) 
45
    {
46
    {
46
    errors++;
47
    errors++;
47
    reason << call << "(" << param << ") : " << errno << " : " << 
48
    reason << call << "(" << param << ") : " << errno << " : " << 
...
...
82
bool FsTreeWalker::setSkippedNames(const list<string> &patterns)
83
bool FsTreeWalker::setSkippedNames(const list<string> &patterns)
83
{
84
{
84
    data->skippedNames = patterns;
85
    data->skippedNames = patterns;
85
    return true;
86
    return true;
86
}
87
}
87
void FsTreeWalker::clearSkippedNames()
88
{
89
    data->skippedNames.clear();
90
}
91
88
89
bool FsTreeWalker::addSkippedPath(const string& path)
90
{
91
    data->skippedPaths.push_back(path_canon(path));
92
    return true;
93
}
94
bool FsTreeWalker::setSkippedPaths(const list<string> &paths)
95
{
96
    data->skippedPaths = paths;
97
    for (list<string>::iterator it = data->skippedPaths.begin();
98
   it != data->skippedPaths.end(); it++)
99
  *it = path_canon(*it);
100
    return true;
101
}
92
102
93
FsTreeWalker::Status FsTreeWalker::walk(const string &top, 
103
FsTreeWalker::Status FsTreeWalker::walk(const string &top, 
104
                  FsTreeWalkerCB& cb)
105
{
106
    return iwalk(path_canon(top), cb);
107
}
108
109
FsTreeWalker::Status FsTreeWalker::iwalk(const string &top, 
94
                    FsTreeWalkerCB& cb)
110
                    FsTreeWalkerCB& cb)
95
{
111
{
96
    Status status = FtwOk;
112
    Status status = FtwOk;
97
    struct stat st;
113
    struct stat st;
98
    int statret;
114
    int statret;
...
...
156
        if (statret == -1) {
172
        if (statret == -1) {
157
        data->logsyserr("stat", fn);
173
        data->logsyserr("stat", fn);
158
        continue;
174
        continue;
159
        }
175
        }
160
        if (S_ISDIR(st.st_mode)) {
176
        if (S_ISDIR(st.st_mode)) {
177
      if (!data->skippedPaths.empty()) {
178
          list<string>::const_iterator it;
179
          for (it = data->skippedPaths.begin(); 
180
           it != data->skippedPaths.end(); it++) {
181
          if (fn == *it)
182
              goto skip;
183
          }
184
      }
185
161
        if (data->options & FtwNoRecurse) {
186
        if (data->options & FtwNoRecurse) {
162
            status = cb.processone(fn, &st, FtwDirEnter);
187
            status = cb.processone(fn, &st, FtwDirEnter);
163
        } else {
188
        } else {
164
            status=walk(fn, cb);
189
            status = iwalk(fn, cb);
165
        }
190
        }
166
        if (status & (FtwStop|FtwError))
191
        if (status & (FtwStop|FtwError))
167
            goto out;
192
            goto out;
168
        if ((status = cb.processone(top, &st, FtwDirReturn)) 
193
        if ((status = cb.processone(top, &st, FtwDirReturn)) 
169
            & (FtwStop|FtwError))
194
            & (FtwStop|FtwError))
...
...
211
    }
236
    }
212
    return FsTreeWalker::FtwOk;
237
    return FsTreeWalker::FtwOk;
213
    }
238
    }
214
};
239
};
215
240
241
static const char *thisprog;
242
243
static char usage [] =
244
"trfstreewalk [-p pattern] [-P ignpath] topdir\n\n"
245
;
246
static void
247
Usage(void)
248
{
249
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
250
    exit(1);
251
}
252
253
static int     op_flags;
254
#define OPT_MOINS 0x1
255
#define OPT_p   0x2 
256
#define OPT_P   0x4 
257
216
int main(int argc, const char **argv)
258
int main(int argc, const char **argv)
217
{
259
{
218
    if (argc < 2) {
260
    list<string> patterns;
219
  cerr << "Usage: fstreewalk <topdir> [ignpat [ignpat] ...]" << endl;
261
    list<string> paths;
220
  exit(1);
262
    thisprog = argv[0];
263
    argc--; argv++;
264
265
  while (argc > 0 && **argv == '-') {
266
    (*argv)++;
267
    if (!(**argv))
268
      /* Cas du "adb - core" */
269
      Usage();
270
    while (**argv)
271
      switch (*(*argv)++) {
272
      case 'p':   op_flags |= OPT_p; if (argc < 2)  Usage();
273
    patterns.push_back(*(++argv));
274
    argc--; 
275
    goto b1;
276
      case 'P':   op_flags |= OPT_P; if (argc < 2)  Usage();
277
    paths.push_back(*(++argv));
278
  argc--; 
279
  goto b1;
280
      default: Usage();   break;
221
    }
281
      }
222
    argv++;argc--;
282
  b1: argc--; argv++;
283
  }
284
285
  if (argc != 1)
286
    Usage();
223
    string topdir = *argv++;argc--;
287
  string topdir = *argv++;argc--;
224
    list<string> ignpats;
288
225
    while (argc > 0) {
226
  ignpats.push_back(*argv++);argc--;
227
    }
228
    FsTreeWalker walker;
289
  FsTreeWalker walker;
229
    walker.setSkippedNames(ignpats);
290
  walker.setSkippedNames(patterns);
291
  walker.setSkippedPaths(paths);
230
    myCB cb;
292
  myCB cb;
231
    walker.walk(topdir, cb);
293
  walker.walk(topdir, cb);
232
    if (walker.getErrCnt() > 0)
294
  if (walker.getErrCnt() > 0)
233
  cout << walker.getReason();
295
      cout << walker.getReason();
234
}
296
}
235
297
236
#endif // TEST_FSTREEWALK
298
#endif // TEST_FSTREEWALK