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 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <cstdlib>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 
38 
39 class XrdOucTList;
40 class XrdSysError;
41 
43 {
44 public:
45 
46 struct NSEnt
47 {
48 struct NSEnt *Next; // -> Next entry in the indexed directory
49 char *Path; // Path name to file if opts & noPath (Path == File)
50 char *File; // File name component
51 int Plen; // strlen(Path)
52 struct stat Stat; // stat() of Path if opts & retStat
53 char *Link; // -> Link data if opts & retLink
54 int Lksz; // Length of link data
55 
57 
58 Etype Type; // One of the above. If isLink then Link is invalid!
59 
60  NSEnt() : Next(0), Path(0), Plen(0), Link(0), Lksz(0) {}
61  ~NSEnt() {if (Path) free(Path);
62  if (Link) free(Link);
63  }
64 };
65 
66 // Calling Index() provides the requested directory entries with a return code:
67 // NSEnts != 0 && rc == 0: Normal ending.
68 // NSEnts != 0 && rc != 0: Potentially short list as indexing aborted w/ err.
69 // NSEnts == 0 && rc == 0: End of indexing no more entries can be returned.
70 // NSEnts == 0 && rc != 0: Abort occurred before any entries could be returned.
71 //
72 // When opts & skpErrs is true, then rc may be zero even when an error occurred.
73 //
74 // If opts & Recurse, indexing will traverse the directory tree, one directory
75 // at a time. For a complete traversal you sould keep calling Index() until
76 // it returns 0 with rc == 0. When dPath is supplied, a pointer to the base
77 // directory is returned as well (see noPath).
78 //
79 NSEnt *Index(int &rc, const char **dPath=0);
80 
81 // The CallBack class is used to intercept empty directories. When set by a
82 // call to setCallBack(); should an empty directory (i.e., one with no entries
83 // or only with a lock file) in encountered a call is made to to the isEmpty()
84 // method. If lkFn is zero, the directory is empty; otherwise, lkFn is the name
85 // of the singleton lock file. To unset the callback use setCallBack(0);
86 //
87 class CallBack
88 {public:
89 virtual
90 void isEmpty(struct stat *dStat, const char *dPath, const char *lkFn)=0;
91 
92  CallBack() {}
93 virtual ~CallBack() {}
94 };
95 
96 void setCallBack(CallBack *cbP=0) {edCB = cbP;}
97 
98 // When erp in the constructor is null, no error messages are printed. Such
99 // messages can be routed to "std::cerr" if setMsgOn is called to establish a
100 // messages prefix (e.g. "<cmd>:") for use by command line commands.
101 //
102 void setMsgOn(const char *pfx) {mPfx = pfx;}
103 
104 // The following are processing options passed to the constructor
105 //
106 static const int retDir = 0x0001; // Return directories (implies retStat)
107 static const int retFile= 0x0002; // Return files (implies retStat)
108 static const int retLink= 0x0004; // Return link data (implies retStat)
109 static const int retMisc= 0x0008; // Return other types (implies retStat)
110 static const int retAll = 0x000f; // Return everything
111 
112 static const int retStat= 0x0010; // return stat() information
113 static const int retIDLO= 0x0020; // Names returned in decreasing length order
114 static const int retIILO= 0x0040; // Names returned in increasing length order
115 static const int Recurse= 0x0080; // Recursive traversal, 1 Level per Index()
116 static const int noPath = 0x0100; // Do not include the full directory path
117 static const int skpErrs= 0x8000; // Skip any entry causing an error
118 
119  XrdOucNSWalk(XrdSysError *erp, // Error msg object. If 0->silent
120  const char *dname, // Initial directory path
121  const char *LKfn=0, // Lock file name (see note below)
122  int opts=retAll, // Options (see above)
123  XrdOucTList *xP=0); // 1st Level dir exclude list
124  ~XrdOucNSWalk();
125 
126 // Note: When Lkfn is supplied and it exists in a directory about to be indexed
127 // then the file is opened in r/w mode and an exclusive lock is obtained.
128 // If either fails, the the directory is not indexed and Index() will
129 // return null pointer with rc != 0. Note that the lkfn is not returned
130 // as a directory entry if an empty directory call back has been set.
131 
132 private:
133 void addEnt(XrdOucNSWalk::NSEnt *eP);
134 int Build();
135 int Emsg(const char *pfx, int rc, const char *tx1, const char *tx2=0);
136 int getLink(XrdOucNSWalk::NSEnt *eP);
137 int getStat(XrdOucNSWalk::NSEnt *eP, int doLstat=0);
138 int getStat();
139 int inXList(const char *dName);
140 int isSymlink();
141 int LockFile();
142 void setPath(char *newpath);
143 
144 XrdSysError *eDest;
145 XrdOucTList *DList;
146 XrdOucTList *XList;
147 struct NSEnt *DEnts;
148 struct stat dStat;
149 CallBack *edCB;
150 const char *mPfx;
151 char DPath[1032];
152 char *File;
153 char *LKFn;
154 int LKfd;
155 int DPfd;
156 int Opts;
157 int errOK;
158 int isEmpty;
159 };
160 #endif
int stat(const char *path, struct stat *buf)
XrdOucString File
struct myOpts opts
virtual void isEmpty(struct stat *dStat, const char *dPath, const char *lkFn)=0
static const int retFile
static const int retAll
XrdOucNSWalk(XrdSysError *erp, const char *dname, const char *LKfn=0, int opts=retAll, XrdOucTList *xP=0)
Definition: XrdOucNSWalk.cc:48
static const int retIDLO
void setMsgOn(const char *pfx)
static const int retLink
static const int skpErrs
static const int noPath
static const int retStat
void setCallBack(CallBack *cbP=0)
Definition: XrdOucNSWalk.hh:96
static const int retMisc
NSEnt * Index(int &rc, const char **dPath=0)
Definition: XrdOucNSWalk.cc:93
static const int retDir
static const int Recurse
static const int retIILO
int Opts
Definition: XrdMpxStats.cc:58
struct NSEnt * Next
Definition: XrdOucNSWalk.hh:48