add skipped paths

This commit is contained in:
dockes 2006-12-21 08:22:35 +00:00
parent 9b1bef11af
commit a32ecfe60e
2 changed files with 92 additions and 24 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.8 2006-01-23 13:32:28 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.9 2006-12-21 08:22:35 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -40,6 +40,7 @@ class FsTreeWalker::Internal {
Options options; Options options;
stringstream reason; stringstream reason;
list<string> skippedNames; list<string> skippedNames;
list<string> skippedPaths;
int errors; int errors;
void logsyserr(const char *call, const string &param) void logsyserr(const char *call, const string &param)
{ {
@ -84,14 +85,29 @@ bool FsTreeWalker::setSkippedNames(const list<string> &patterns)
data->skippedNames = patterns; data->skippedNames = patterns;
return true; return true;
} }
void FsTreeWalker::clearSkippedNames()
bool FsTreeWalker::addSkippedPath(const string& path)
{ {
data->skippedNames.clear(); data->skippedPaths.push_back(path_canon(path));
return true;
}
bool FsTreeWalker::setSkippedPaths(const list<string> &paths)
{
data->skippedPaths = paths;
for (list<string>::iterator it = data->skippedPaths.begin();
it != data->skippedPaths.end(); it++)
*it = path_canon(*it);
return true;
} }
FsTreeWalker::Status FsTreeWalker::walk(const string &top, FsTreeWalker::Status FsTreeWalker::walk(const string &top,
FsTreeWalkerCB& cb) FsTreeWalkerCB& cb)
{
return iwalk(path_canon(top), cb);
}
FsTreeWalker::Status FsTreeWalker::iwalk(const string &top,
FsTreeWalkerCB& cb)
{ {
Status status = FtwOk; Status status = FtwOk;
struct stat st; struct stat st;
@ -158,10 +174,19 @@ FsTreeWalker::Status FsTreeWalker::walk(const string &top,
continue; continue;
} }
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
if (!data->skippedPaths.empty()) {
list<string>::const_iterator it;
for (it = data->skippedPaths.begin();
it != data->skippedPaths.end(); it++) {
if (fn == *it)
goto skip;
}
}
if (data->options & FtwNoRecurse) { if (data->options & FtwNoRecurse) {
status = cb.processone(fn, &st, FtwDirEnter); status = cb.processone(fn, &st, FtwDirEnter);
} else { } else {
status=walk(fn, cb); status = iwalk(fn, cb);
} }
if (status & (FtwStop|FtwError)) if (status & (FtwStop|FtwError))
goto out; goto out;
@ -213,24 +238,61 @@ class myCB : public FsTreeWalkerCB {
} }
}; };
static const char *thisprog;
static char usage [] =
"trfstreewalk [-p pattern] [-P ignpath] topdir\n\n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_p 0x2
#define OPT_P 0x4
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
if (argc < 2) { list<string> patterns;
cerr << "Usage: fstreewalk <topdir> [ignpat [ignpat] ...]" << endl; list<string> paths;
exit(1); thisprog = argv[0];
} argc--; argv++;
argv++;argc--;
string topdir = *argv++;argc--; while (argc > 0 && **argv == '-') {
list<string> ignpats; (*argv)++;
while (argc > 0) { if (!(**argv))
ignpats.push_back(*argv++);argc--; /* Cas du "adb - core" */
} Usage();
FsTreeWalker walker; while (**argv)
walker.setSkippedNames(ignpats); switch (*(*argv)++) {
myCB cb; case 'p': op_flags |= OPT_p; if (argc < 2) Usage();
walker.walk(topdir, cb); patterns.push_back(*(++argv));
if (walker.getErrCnt() > 0) argc--;
cout << walker.getReason(); goto b1;
case 'P': op_flags |= OPT_P; if (argc < 2) Usage();
paths.push_back(*(++argv));
argc--;
goto b1;
default: Usage(); break;
}
b1: argc--; argv++;
}
if (argc != 1)
Usage();
string topdir = *argv++;argc--;
FsTreeWalker walker;
walker.setSkippedNames(patterns);
walker.setSkippedPaths(paths);
myCB cb;
walker.walk(topdir, cb);
if (walker.getErrCnt() > 0)
cout << walker.getReason();
} }
#endif // TEST_FSTREEWALK #endif // TEST_FSTREEWALK

View File

@ -16,7 +16,7 @@
*/ */
#ifndef _FSTREEWALK_H_INCLUDED_ #ifndef _FSTREEWALK_H_INCLUDED_
#define _FSTREEWALK_H_INCLUDED_ #define _FSTREEWALK_H_INCLUDED_
/* @(#$Id: fstreewalk.h,v 1.5 2006-01-30 11:15:28 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: fstreewalk.h,v 1.6 2006-12-21 08:22:35 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
#include <list> #include <list>
@ -56,6 +56,7 @@ class FsTreeWalker {
/** Get explanation for error */ /** Get explanation for error */
string getReason(); string getReason();
int getErrCnt(); int getErrCnt();
/** /**
* Add a pattern to the list of things (file or dir) to be ignored * Add a pattern to the list of things (file or dir) to be ignored
* (ie: #* , *~) * (ie: #* , *~)
@ -63,10 +64,15 @@ class FsTreeWalker {
bool addSkippedName(const string &pattern); bool addSkippedName(const string &pattern);
/** Set the ignored patterns list */ /** Set the ignored patterns list */
bool setSkippedNames(const list<string> &patlist); bool setSkippedNames(const list<string> &patlist);
/** Clear the ignored patterns list */
void clearSkippedNames(); /** Same for skipped paths: this are paths, not names, under which we
do not descend (ie: /home/me/.recoll) */
bool addSkippedPath(const string &path);
/** Set the ignored paths list */
bool setSkippedPaths(const list<string> &pathlist);
private: private:
Status iwalk(const string &dir, FsTreeWalkerCB& cb);
class Internal; class Internal;
Internal *data; Internal *data;
}; };