xrootd
XrdOucNSWalk.hh
Go to the documentation of this file.
1 #ifndef __XRDOUCNSWALK_HH
2 #define __XRDOUCNSWALK_HH
3 /******************************************************************************/
4 /* */
5 /* X r d O u c N S W a l k . h h */
6 /* */
7 /* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /******************************************************************************/
12 
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 
18 // $Id$
19 
20 class XrdOucTList;
21 class XrdSysError;
22 
24 {
25 public:
26 
27 struct NSEnt
28 {
29 struct NSEnt *Next; // -> Next entry in the indexed directory
30 char *Path; // Path name to file if opts & noPath (Path == File)
31 char *File; // File name component
32 int Plen; // strlen(Path)
33 struct stat Stat; // stat() of Path if opts & retStat
34 char *Link; // -> Link data if opts & retLink
35 int Lksz; // Length of link data
36 
38 
39 Etype Type; // One of the above. If isLink then Link is invalid!
40 
41  NSEnt() : Next(0), Path(0), Plen(0), Link(0), Lksz(0) {}
42  ~NSEnt() {if (Path) free(Path);
43  if (Link) free(Link);
44  }
45 };
46 
47 // Calling Index() provides the requested directory entries with a return code:
48 // NSEnts != 0 && rc == 0: Normal ending.
49 // NSEnts != 0 && rc != 0: Potentially short list as indexing aborted w/ err.
50 // NSEnts == 0 && rc == 0: End of indexing no more entries can be returned.
51 // NSEnts == 0 && rc != 0: Abort occured before any entries could be returned.
52 //
53 // When opts & skpErrs is true, then rc may be zero even when an error occured.
54 //
55 // If opts & Recurse, indexing will traverse the directory tree, one directory
56 // at a time. For a complete traversal you sould keep calling Index() until
57 // it returns 0 with rc == 0. When dPath is supplied, a pointer to the base
58 // directory is returned as well (see noPath).
59 //
60 NSEnt *Index(int &rc, const char **dPath=0);
61 
62 // The CallBack class is used to intercept empty directories. When set by a
63 // call to setCallBack(); should an empty directory (i.e., one with no entries
64 // or only with a lock file) in encountered a call is made to to the isEmpty()
65 // method. If lkFn is zero, the directory is empty; otherwise, lkFn is the name
66 // of the singleton lock file. To unset the callback use setCallBack(0);
67 //
68 class CallBack
69 {public:
70 virtual
71 void isEmpty(struct stat *dStat, const char *dPath, const char *lkFn)=0;
72 
73  CallBack() {}
74 virtual ~CallBack() {}
75 };
76 
77 void setCallBack(CallBack *cbP=0) {edCB = cbP;}
78 
79 // The following are processing options passed to the constructor
80 //
81 static const int retDir = 0x0001; // Return directories (implies retStat)
82 static const int retFile= 0x0002; // Return files (implies retStat)
83 static const int retLink= 0x0004; // Return link data (implies retStat)
84 static const int retMisc= 0x0008; // Return other types (implies retStat)
85 static const int retAll = 0x000f; // Return everything
86 
87 static const int retStat= 0x0010; // return stat() information
88 static const int retIDLO= 0x0020; // Names returned in decreasing length order
89 static const int retIILO= 0x0040; // Names returned in increasing length order
90 static const int Recurse= 0x0080; // Recursive traversal, 1 Level per Index()
91 static const int noPath = 0x0100; // Do not include the full directory path
92 static const int skpErrs= 0x8000; // Skip any entry causing an error
93 
94  XrdOucNSWalk(XrdSysError *erp, // Error msg object. If 0->silent
95  const char *dname, // Initial directory path
96  const char *LKfn=0, // Lock file name (see note below)
97  int opts=retAll, // Options (see above)
98  XrdOucTList *xP=0); // 1st Level dir exclude list
99  ~XrdOucNSWalk();
100 
101 // Note: When Lkfn is supplied and it exists in a directory about to be indexed
102 // then the file is opened in r/w mode and an exclusive lock is obtained.
103 // If either fails, the the directory is not indexed and Index() will
104 // return null pointer with rc != 0. Note that the lkfn is not returned
105 // as a directory entry if an empty directory call back has been set.
106 
107 private:
108 void addEnt(XrdOucNSWalk::NSEnt *eP);
109 int Build();
111 int getStat(XrdOucNSWalk::NSEnt *eP, int doLstat=0);
112 int getStat();
113 int inXList(const char *dName);
114 int isSymlink();
115 int LockFile();
116 void setPath(char *newpath);
117 
121 struct NSEnt *DEnts;
122 struct stat dStat;
124 char DPath[1032];
125 char *File;
126 char *LKFn;
127 int LKfd;
128 int DPfd;
129 int Opts;
130 int errOK;
132 };
133 #endif