From a32ecfe60e5b8c3d33e5de6244cce6523eb88130 Mon Sep 17 00:00:00 2001 From: dockes Date: Thu, 21 Dec 2006 08:22:35 +0000 Subject: [PATCH] add skipped paths --- src/utils/fstreewalk.cpp | 104 +++++++++++++++++++++++++++++++-------- src/utils/fstreewalk.h | 12 +++-- 2 files changed, 92 insertions(+), 24 deletions(-) diff --git a/src/utils/fstreewalk.cpp b/src/utils/fstreewalk.cpp index e676b1db..48b3f230 100644 --- a/src/utils/fstreewalk.cpp +++ b/src/utils/fstreewalk.cpp @@ -1,5 +1,5 @@ #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 /* * This program is free software; you can redistribute it and/or modify @@ -40,6 +40,7 @@ class FsTreeWalker::Internal { Options options; stringstream reason; list skippedNames; + list skippedPaths; int errors; void logsyserr(const char *call, const string ¶m) { @@ -84,14 +85,29 @@ bool FsTreeWalker::setSkippedNames(const list &patterns) data->skippedNames = patterns; 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 &paths) +{ + data->skippedPaths = paths; + for (list::iterator it = data->skippedPaths.begin(); + it != data->skippedPaths.end(); it++) + *it = path_canon(*it); + return true; } - FsTreeWalker::Status FsTreeWalker::walk(const string &top, FsTreeWalkerCB& cb) +{ + return iwalk(path_canon(top), cb); +} + +FsTreeWalker::Status FsTreeWalker::iwalk(const string &top, + FsTreeWalkerCB& cb) { Status status = FtwOk; struct stat st; @@ -158,10 +174,19 @@ FsTreeWalker::Status FsTreeWalker::walk(const string &top, continue; } if (S_ISDIR(st.st_mode)) { + if (!data->skippedPaths.empty()) { + list::const_iterator it; + for (it = data->skippedPaths.begin(); + it != data->skippedPaths.end(); it++) { + if (fn == *it) + goto skip; + } + } + if (data->options & FtwNoRecurse) { status = cb.processone(fn, &st, FtwDirEnter); } else { - status=walk(fn, cb); + status = iwalk(fn, cb); } if (status & (FtwStop|FtwError)) 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) { - if (argc < 2) { - cerr << "Usage: fstreewalk [ignpat [ignpat] ...]" << endl; - exit(1); - } - argv++;argc--; - string topdir = *argv++;argc--; - list ignpats; - while (argc > 0) { - ignpats.push_back(*argv++);argc--; - } - FsTreeWalker walker; - walker.setSkippedNames(ignpats); - myCB cb; - walker.walk(topdir, cb); - if (walker.getErrCnt() > 0) - cout << walker.getReason(); + list patterns; + list paths; + thisprog = argv[0]; + argc--; argv++; + + while (argc > 0 && **argv == '-') { + (*argv)++; + if (!(**argv)) + /* Cas du "adb - core" */ + Usage(); + while (**argv) + switch (*(*argv)++) { + case 'p': op_flags |= OPT_p; if (argc < 2) Usage(); + patterns.push_back(*(++argv)); + argc--; + 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 diff --git a/src/utils/fstreewalk.h b/src/utils/fstreewalk.h index 46bb94a6..d0f467be 100644 --- a/src/utils/fstreewalk.h +++ b/src/utils/fstreewalk.h @@ -16,7 +16,7 @@ */ #ifndef _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 #include @@ -56,6 +56,7 @@ class FsTreeWalker { /** Get explanation for error */ string getReason(); int getErrCnt(); + /** * Add a pattern to the list of things (file or dir) to be ignored * (ie: #* , *~) @@ -63,10 +64,15 @@ class FsTreeWalker { bool addSkippedName(const string &pattern); /** Set the ignored patterns list */ bool setSkippedNames(const list &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 &pathlist); private: + Status iwalk(const string &dir, FsTreeWalkerCB& cb); class Internal; Internal *data; };