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.12 2007-07-12 10:53:07 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: fstreewalk.cpp,v 1.13 2007-08-28 08:08:38 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
...
...
128
        return true;
128
        return true;
129
    }
129
    }
130
    return false;
130
    return false;
131
}
131
}
132
132
133
FsTreeWalker::Status FsTreeWalker::walk(const string &top, 
133
FsTreeWalker::Status FsTreeWalker::walk(const string& _top, 
134
                    FsTreeWalkerCB& cb)
134
                    FsTreeWalkerCB& cb)
135
{
135
{
136
    return iwalk(path_canon(top), cb);
136
    string top = path_canon(_top);
137
}
138
137
139
FsTreeWalker::Status FsTreeWalker::iwalk(const string &top, 
140
                  FsTreeWalkerCB& cb)
141
{
142
    Status status = FtwOk;
143
    struct stat st;
138
    struct stat st;
144
    int statret;
139
    int statret;
145
140
    // We always follow symlinks at this point. Makes more sense.
146
    // Handle the top entry
147
    statret = (data->options & FtwFollow) ? stat(top.c_str(), &st) :
148
  lstat(top.c_str(), &st);
141
    statret = stat(top.c_str(), &st);
149
    if (statret == -1) {
142
    if (statret == -1) {
150
    data->logsyserr("stat", top);
143
    data->logsyserr("stat", top);
151
    return FtwError;
144
    return FtwError;
152
    }
145
    }
146
    return iwalk(top, &st, cb);
147
}
148
149
// Note that the 'norecurse' flag is handled as part of the directory read. 
150
// This means that we always go into the top 'walk()' parameter if it is a 
151
// directory, even if norecurse is set. Bug or Feature ?
152
FsTreeWalker::Status FsTreeWalker::iwalk(const string &top, 
153
                   struct stat *stp,
154
                   FsTreeWalkerCB& cb)
155
{
156
    Status status = FtwOk;
157
158
    // Handle the parameter
153
    if (S_ISDIR(st.st_mode)) {
159
    if (S_ISDIR(stp->st_mode)) {
154
    if ((status = cb.processone(top, &st, FtwDirEnter)) & 
160
    if ((status = cb.processone(top, stp, FtwDirEnter)) & 
155
        (FtwStop|FtwError)) {
161
        (FtwStop|FtwError)) {
156
        return status;
162
        return status;
157
    }
163
    }
158
    } else if (S_ISREG(st.st_mode)) {
164
    } else if (S_ISREG(stp->st_mode)) {
159
    return cb.processone(top, &st, FtwRegular);
165
    return cb.processone(top, stp, FtwRegular);
160
    } else {
166
    } else {
161
    return status;
167
    return status;
162
    }
168
    }
163
169
164
    // Handle directory entries
170
    // This is a directory, read it and process entries:
165
    DIR *d = opendir(top.c_str());
171
    DIR *d = opendir(top.c_str());
166
    if (d == 0) {
172
    if (d == 0) {
167
    data->logsyserr("opendir", top);
173
    data->logsyserr("opendir", top);
168
    switch (errno) {
174
    switch (errno) {
169
    case EPERM:
175
    case EPERM:
...
...
204
210
205
        if (S_ISDIR(st.st_mode)) {
211
        if (S_ISDIR(st.st_mode)) {
206
        if (data->options & FtwNoRecurse) {
212
        if (data->options & FtwNoRecurse) {
207
            status = cb.processone(fn, &st, FtwDirEnter);
213
            status = cb.processone(fn, &st, FtwDirEnter);
208
        } else {
214
        } else {
209
            status = iwalk(fn, cb);
215
            status = iwalk(fn, &st, cb);
210
        }
216
        }
211
        if (status & (FtwStop|FtwError))
217
        if (status & (FtwStop|FtwError))
212
            goto out;
218
            goto out;
213
        if ((status = cb.processone(top, &st, FtwDirReturn)) 
219
        if ((status = cb.processone(top, &st, FtwDirReturn)) 
214
            & (FtwStop|FtwError))
220
            & (FtwStop|FtwError))