Debian Codegraph @

macos/source/recurse.c

raw
1 /*
2 These functions are based on Jim Luther's IterateDirectory() found in MoreFiles
3 However, it's heavily modified by Dirk Haase
4 */
5
6 /*
7 ** IterateDirectory: File Manager directory iterator routines.
8 **
9 ** by Jim Luther
10 **
11 ** File: IterateDirectory.c
12 **
13 ** Copyright (c) 1995-1998 Jim Luther and Apple Computer, Inc.
14 ** All rights reserved.
15 **
16 ** You may incorporate this sample code into your applications without
17 ** restriction, though the sample code has been provided "AS IS" and the
18 ** responsibility for its operation is 100% yours.
19 **
20 ** IterateDirectory is designed to drop into the MoreFiles sample code
21 ** library I wrote while in Apple Developer Technical Support
22 */
23
24 /*****************************************************************************/
25 /* Includes */
26 /*****************************************************************************/
27
28 #include <Types.h>
29 #include <Errors.h>
30 #include <Files.h>
31 #include <stdio.h>
32 #include <string.h>
33
34
35 #include "zip.h"
36 #include "macstuff.h"
37 #include "helpers.h"
38 #include "recurse.h"
39 #include "macglob.h"
40 #include "pathname.h"
41
42
43
44
45 /*****************************************************************************/
46 /* Macros, typedefs */
47 /*****************************************************************************/
48
49 /* The RecurseGlobals structure is used to minimize the amount of
50 ** stack space used when recursively calling RecurseDirectoryLevel
51 ** and to hold global information that might be needed at any time.
52 */
53 struct RecurseGlobals
54 {
55 short vRefNum;
56 CInfoPBRec cPB; /* the parameter block used for
57 PBGetCatInfo calls */
58 unsigned char *itemName; /* the name of the current item */
59 char *FullPath;
60 short FullPathLen;
61 OSErr result; /* temporary holder of results -
62 saves 2 bytes of stack each level */
63 Boolean quitFlag; /* set to true if filter wants to
64 kill interation */
65 unsigned short maxLevels; /* Maximum levels to
66 iterate through */
67 unsigned short currentLevel; /* The current level
68 IterateLevel is on */
69 };
70
71 typedef struct RecurseGlobals RecurseGlobals;
72 typedef RecurseGlobals *RecurseGlobalsPtr;
73
74
75 /*****************************************************************************/
76 /* Global Vars */
77 /*****************************************************************************/
78
79 extern MacZipGlobals MacZip;
80 extern const char ResourceMark[13]; /* "XtraStuf.mac:" var is initialized in file pathname.c */
81 extern int extra_fields; /* do not create extra fields if false */
82
83 static RecurseGlobals theGlobals;
84
85 static unsigned long DirLevels = 0;
86 static char *buffer;
87 extern int verbose; /* 1=report oddities in zip file structure */
88
89 /*****************************************************************************/
90 /* Prototypes */
91 /*****************************************************************************/
92
93 int procname(char *filename, int caseflag);
94 int MatchWild( char *pPat, char *pStr, int case_sens);
95 Boolean IsZipFile(char *name);
96
97 static void RecurseDirectoryLevel(long DirID, RecurseGlobals *Globals);
98 static Boolean isRegularItem( RecurseGlobals *Globals);
99 static void ProcessFiles(RecurseGlobals *Globals,
100 Boolean hasDataFork, Boolean hasResourceFork);
101 static void ProcessDirectory(RecurseGlobals *Globals,
102 Boolean IncludeItem, long DirID);
103 static void ProcessItem(RecurseGlobals *Globals, long DirID);
104
105 /*****************************************************************************/
106 /* Functions */
107 /*****************************************************************************/
108
109 static void RecurseDirectoryLevel(long DirID, RecurseGlobals *Globals)
110 {
111 char buffer2[23];
112
113 /* if maxLevels is zero, we aren't checking levels */
114 if ( (Globals->maxLevels == 0) ||
115 /* if currentLevel < maxLevels, look at this level */
116 (Globals->currentLevel < Globals->maxLevels) )
117 {
118 short index = 1;
119
120 ++Globals->currentLevel; /* go to next level */
121 if (DirLevels < Globals->currentLevel) DirLevels = Globals->currentLevel;
122 sprintf(buffer2,"Globals->currentLevel: %d",Globals->currentLevel);
123
124 do
125 { /* Isn't C great... What I'd give for a "WITH
126 theGlobals DO" about now... */
127
128 /* Get next source item at the current directory level */
129 Globals->cPB.dirInfo.ioFDirIndex = index;
130 Globals->cPB.dirInfo.ioDrDirID = DirID;
131 Globals->result = PBGetCatInfoSync((CInfoPBPtr)&Globals->cPB);
132
133 ShowCounter(false);
134
135 if ( Globals->result == noErr )
136 {
137 ProcessItem(Globals, DirID);
138 } /* if ( Globals->result == noErr ) */
139
140 ++index; /* prepare to get next item */
141 /* time to fall back a level? */
142 } while ( (Globals->result == noErr) && (!Globals->quitFlag) );
143
144 if ( (Globals->result == fnfErr) || /* fnfErr is OK -
145 it only means we hit
146 the end of this level */
147 (Globals->result == afpAccessDenied) ) /* afpAccessDenied is OK,
148 too - it only means we cannot see inside a directory */
149 {
150 Globals->result = noErr;
151 }
152
153 --Globals->currentLevel; /* return to previous level as we leave */
154 }
155 }
156
157
158
159 /*****************************************************************************/
160
161 pascal OSErr RecurseDirectory(short vRefNum,
162 long thedirID,
163 ConstStr255Param name,
164 unsigned short maxLevels)
165 {
166 OSErr result;
167 short theVRefNum;
168 Boolean isDirectory;
169 long DirID;
170
171 /* Get the real directory ID and make sure it is a directory */
172 result = GetDirectoryID(vRefNum, thedirID, name, &DirID, &isDirectory);
173 if ( result == noErr )
174 {
175 if ( isDirectory == true )
176 {
177 /* Get the real vRefNum */
178 result = DetermineVRefNum(name, vRefNum, &theVRefNum);
179 if ( result == noErr )
180 {
181 /* Set up the globals we need to access from
182 the recursive routine. */
183 theGlobals.cPB.hFileInfo.ioNamePtr = theGlobals.itemName;
184 theGlobals.cPB.hFileInfo.ioVRefNum = theVRefNum;
185 theGlobals.itemName[0] = 0;
186 theGlobals.result = noErr;
187 theGlobals.quitFlag = false;
188 theGlobals.maxLevels = maxLevels;
189 theGlobals.currentLevel = 0; /* start at level 0 */
190
191 /* Here we go into recursion land... */
192 RecurseDirectoryLevel(DirID, &theGlobals);
193
194 result = theGlobals.result; /* set the result */
195 }
196 }
197 else
198 {
199 result = dirNFErr; /* a file was passed instead
200 of a directory */
201 }
202 }
203
204 return ( result );
205 }
206
207
208
209 /*****************************************************************************/
210
211 pascal OSErr FSpRecurseDirectory(const FSSpec *spec,
212 unsigned short maxLevels)
213 {
214 OSErr rc;
215
216 theGlobals.vRefNum = spec->vRefNum;
217
218 /* make room for pathnames */
219 theGlobals.itemName = (unsigned char *) StrCalloc(NAME_MAX);
220 theGlobals.FullPath = StrCalloc(NAME_MAX);
221 buffer = StrCalloc(NAME_MAX);
222
223
224 if ((noisy) && (MacZip.DataForkOnly))
225 printf("\n Warning: Datafork only \n");
226
227 /* reset the count to zero */
228 ShowCounter(true);
229
230 if (noisy) leftStatusString("Build File List; Items done:");
231 if (noisy) printf("\n Collecting Filenames ...");
232 rc = RecurseDirectory(spec->vRefNum, spec->parID, spec->name,maxLevels);
233 printerr("RecurseDirectory:",rc,rc,__LINE__,__FILE__,"");
234
235 if (noisy) printf("\n... done \n\n %6d matched files found \n",
236 MacZip.FoundFiles);
237 if (noisy) printf(" %6d folders found in %d Levels \n",
238 MacZip.FoundDirectories,DirLevels);
239
240 if (MacZip.BytesOfData > (1024*1024))
241 if (noisy) printf(" %4.3f MBytes unzipped size\n\n",
242 (float) MacZip.BytesOfData/(1024*1024));
243 else
244 if (noisy) printf(" %4.3f KBytes unzipped size\n\n",
245 (float) MacZip.BytesOfData/1024);
246
247 /* free all memory of pathnames */
248 theGlobals.itemName = (unsigned char *) StrFree((char *)theGlobals.itemName);
249 theGlobals.FullPath = StrFree(theGlobals.FullPath);
250 buffer = StrFree(buffer);
251
252 return rc;
253 }
254
255
256
257
258 /*
259 * Return true if filename == zipfile
260 * After the first match no further check will be done !
261 *
262 */
263 Boolean IsZipFile(char *filen)
264 {
265 static firstMatch = false;
266
267 if (filen == NULL)
268 firstMatch = false;
269
270 if (!firstMatch)
271 {
272 if (stricmp(filen, MacZip.ZipFullPath) == 0)
273 {
274 firstMatch = true;
275 return true;
276 }
277 }
278
279 return false;
280 }
281
282
283
284 static Boolean isRegularItem( RecurseGlobals *Globals)
285 {
286 Boolean isInvisible = false,
287 isAlias = false,
288 isSystem = false;
289
290 isSystem = !((Globals->cPB.hFileInfo.ioFlFndrInfo.fdFlags &
291 (1 << 12)) == 0 );
292 isInvisible = !((Globals->cPB.hFileInfo.ioFlFndrInfo.fdFlags &
293 (1 << 14)) == 0 );
294 isAlias = !((Globals->cPB.hFileInfo.ioFlFndrInfo.fdFlags &
295 (1 << 15)) == 0);
296
297 if (isAlias == true)
298 {
299 return false;
300 }
301
302 if (MacZip.IncludeInvisible == true)
303 {
304 return true;
305 }
306
307 if ((isSystem == true) ||
308 (isInvisible == true))
309 {
310 return false;
311 }
312
313 return true;
314 }
315
316
317
318
319 static void ProcessFiles(RecurseGlobals *Globals,
320 Boolean hasDataFork, Boolean hasResourceFork)
321 {
322 /* some file statistics */
323 MacZip.FoundFiles++;
324
325 if (hasDataFork == true)
326 {
327 MacZip.BytesOfData =
328 Globals->cPB.hFileInfo.ioFlLgLen +
329 MacZip.BytesOfData;
330 MacZip.CurrentFork = DataFork;
331 MacZip.RawCountOfItems++;
332
333 if (MacZip.DataForkOnly == true)
334 {
335 procname(Globals->FullPath, false);
336 hasResourceFork = false;
337 }
338 else
339 {
340 procname(Real2RfDfFilen(buffer,Globals->FullPath,
341 DataFork, MacZip.MacZipMode,
342 MacZip.DataForkOnly), false);
343 }
344 }
345
346 if (hasResourceFork == true)
347 {
348 MacZip.BytesOfData =
349 Globals->cPB.hFileInfo.ioFlRLgLen +
350 MacZip.BytesOfData;
351 MacZip.CurrentFork = ResourceFork;
352 MacZip.RawCountOfItems++;
353
354 procname(Real2RfDfFilen(buffer, Globals->FullPath,
355 ResourceFork, MacZip.MacZipMode,
356 MacZip.DataForkOnly), false);
357 }
358 }
359
360
361
362
363 static void ProcessDirectory(RecurseGlobals *Globals,
364 Boolean IncludeItem, long DirID)
365 {
366 OSErr rc;
367
368 MacZip.isDirectory = true;
369
370 GetFullPathFromID(Globals->FullPath,Globals->vRefNum, DirID,
371 Globals->itemName, &rc);
372
373 MacZip.RawCountOfItems++;
374 MacZip.FoundDirectories++;
375
376 if (MacZip.StoreFoldersAlso)
377 {
378 procname(Globals->FullPath, false);
379 }
380
381 /* We have a directory */
382 if ( !Globals->quitFlag && IncludeItem)
383 {
384 /* Dive again if the IterateFilterProc didn't say "quit" and dir is
385 not an alias */
386 RecurseDirectoryLevel(Globals->cPB.dirInfo.ioDrDirID,
387 Globals);
388 }
389 }
390
391
392
393 static void ProcessItem(RecurseGlobals *Globals, long DirID)
394 {
395 OSErr rc;
396 Boolean IncludeItem = false, hasDataFork = false;
397 Boolean hasResourceFork = false;
398
399 IncludeItem = isRegularItem(Globals);
400
401 /* Is it a File? */
402 if ( (Globals->cPB.hFileInfo.ioFlAttrib & ioDirMask) == 0 )
403 {
404 PToCCpy(Globals->itemName,MacZip.FileName);
405 MacZip.isDirectory = false;
406
407 hasDataFork = (Globals->cPB.hFileInfo.ioFlLgLen != 0);
408 hasResourceFork = (Globals->cPB.hFileInfo.ioFlRLgLen != 0);
409
410 /* include also files with zero recource- and data-fork */
411 if ((hasDataFork == 0) && (hasResourceFork == 0))
412 hasDataFork = true;
413
414 if ((hasDataFork == 0) &&
415 (hasResourceFork != 0) &&
416 (extra_fields == false))
417 {
418 IncludeItem = false;
419 }
420
421 GetFullPathFromID(Globals->FullPath,Globals->vRefNum,
422 DirID, Globals->itemName, &rc);
423 printerr("GetFullPathFromID:",rc,rc,__LINE__,
424 __FILE__,MacZip.FileName);
425
426 if (IncludeItem && /* don't include the zipfile itself */
427 (!IsZipFile(Globals->FullPath)) )
428 {
429 if (MATCH(MacZip.Pattern, MacZip.FileName, false) == true)
430 {
431 ProcessFiles(Globals, hasDataFork, hasResourceFork);
432 } /* if (MatchWild( MacZip.FileName,MacZip.Pattern ) ==
433 true) */
434 } /* if (!IsZipFile(Globals->FullPath)) */
435 } /* Is it a File? */
436
437 /* Is it a directory? */
438 if ( (Globals->cPB.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
439 {
440 ProcessDirectory(Globals,IncludeItem, DirID);
441 } /* Is it a directory? */
442 }