File: FXDirVisitor.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (254 lines) | stat: -rw-r--r-- 7,342 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/********************************************************************************
*                                                                               *
*                     D i r e c t o r y   V i s i t o r                         *
*                                                                               *
*********************************************************************************
* Copyright (C) 2008,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXIO.h"
#include "FXStat.h"
#include "FXFile.h"
#include "FXPath.h"
#include "FXDir.h"
#include "FXDirVisitor.h"

/*
  Notes:

  - There are three return codes to influence processing:

      0 : Skip item, move to next
      1 : Continue with processing.
      2 : Bail on the whole thing.

  - Automatically skip directories already being visited to avoid circular symlinks
    from causing infinite recursion.
  - Also skip directories with insufficient permissions.
  - Recursion limiter feature added; allows one to stop below a certain level.
*/


using namespace FX;

/*******************************************************************************/

namespace FX {


// Keep track of visited directories, avoiding infinite recursion
struct FXDirVisitor::Seen {
  FXStat  stat;                 // File status
  Seen**  current;              // Current one
  Seen*   last;                 // Link last visited directory

  // Save old value of current, point it here
  Seen(Seen** cur):current(cur),last(*cur){ *current=this; }

  // Restore old value of current
 ~Seen(){ *current=last; }
  };



// Return info on current file
const FXStat& FXDirVisitor::info() const {
  return current->stat;
  }


// Recursively traverse starting from path
FXuint FXDirVisitor::traverse(const FXString& path,FXint depth){
  if(0<depth){
    Seen node(&current);

    // Stat (target of sym-linked) path
    if(FXStat::statFile(path,node.stat)){

      // Is directory
      if(node.stat.isDirectory()){
        FXuint code;

        // Bail if visiting recursive sym-link
        for(Seen *s=node.last; s; s=s->last){
          if(node.stat.index()==s->stat.index() && node.stat.volume()==s->stat.volume()) return 0;
          }

        // Conditionally enter subdirectories
        if((code=enter(path))==1){
          FXDir directory(path);
          FXString name;

          // Traverse items in directory
          while(directory.next(name)){

            // Non-navigational directory item
            if(!(name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0')))){

              // Traverse sub-item, decreasing recursion depth by one
              if(traverse(path+(ISPATHSEP(path.tail())?"":PATHSEPSTRING)+name,depth-1)==2){

                // Leave directory
                leave(path);

                // Return bail code
                return 2;
                }
              }
            }

          // Leave directory
          return leave(path);
          }
        return code;
        }

      // Regular file
      return visit(path);
      }
    }
  return 0;
  }


// Enter directory
FXuint FXDirVisitor::enter(const FXString&){
  return 1;
  }


// Handle file
FXuint FXDirVisitor::visit(const FXString&){
  return 1;
  }


// Leave directory
FXuint FXDirVisitor::leave(const FXString&){
  return 1;
  }


// Destructor
FXDirVisitor::~FXDirVisitor(){
  }


/*******************************************************************************/


// Recursively traverse starting from path
FXuint FXGlobVisitor::traverse(const FXString& path,const FXString& wild,FXuint opts,FXint depth){
  wildcard=wild;
  options=opts;
  return FXDirVisitor::traverse(path,depth);
  }


// Enter directory
FXuint FXGlobVisitor::enter(const FXString& path){
  FXuint mode=(options&FXDir::CaseFold)?(FXPath::NoEscape|FXPath::CaseFold):(FXPath::NoEscape);
#ifdef WIN32
  return !(options&FXDir::NoDirs) && ((options&FXDir::HiddenDirs) || !FXStat::isHidden(path)) && ((options&FXDir::AllDirs) || FXPath::match(path,wildcard,mode));
#else
  return !(options&FXDir::NoDirs) && ((options&FXDir::HiddenDirs) || !FXPath::isHidden(path)) && ((options&FXDir::AllDirs) || FXPath::match(path,wildcard,mode));
#endif
  }


// Handle file
FXuint FXGlobVisitor::visit(const FXString& path){
  FXuint mode=(options&FXDir::CaseFold)?(FXPath::NoEscape|FXPath::CaseFold):(FXPath::NoEscape);
#ifdef WIN32
  return !(options&FXDir::NoFiles) && ((options&FXDir::HiddenFiles) || !FXStat::isHidden(path)) && ((options&FXDir::AllFiles) || FXPath::match(path,wildcard,mode));
#else
  return !(options&FXDir::NoFiles) && ((options&FXDir::HiddenFiles) || !FXPath::isHidden(path)) && ((options&FXDir::AllFiles) || FXPath::match(path,wildcard,mode));
#endif
  }


// Leave directory
FXuint FXGlobVisitor::leave(const FXString&){
  return 1;
  }


// Destructor
FXGlobVisitor::~FXGlobVisitor(){
  }


/*******************************************************************************/


// Create new glob counting visitor
FXGlobCountVisitor::FXGlobCountVisitor():countFolders(0),countFiles(0),countBytes(0),maxDepth(0),depth(0){
  }


// Start traversal of path
FXuint FXGlobCountVisitor::traverse(const FXString& path,const FXString& wild,FXuint opts,FXint limit){
  countFolders=countFiles=countBytes=maxDepth=depth=0;
  return FXGlobVisitor::traverse(path,wild,opts,limit);
  }


// Enter directory
FXuint FXGlobCountVisitor::enter(const FXString& path){
  if(FXGlobVisitor::enter(path)){
    countFolders++;
    depth++;
    return 1;
    }
  return 0;
  }


// He mister tally man, tally me banana...
FXuint FXGlobCountVisitor::visit(const FXString& path){
  if(FXGlobVisitor::visit(path)){
    countBytes+=info().size();
    countFiles++;
    return 1;
    }
  return 0;
  }


// Leave directory
FXuint FXGlobCountVisitor::leave(const FXString& path){
  if(FXGlobVisitor::leave(path)){
    maxDepth=FXMAX(maxDepth,depth);
    depth--;
    return 1;
    }
  return 0;
  }


// Destructor
FXGlobCountVisitor::~FXGlobCountVisitor(){
  }


}