XRootD
Loading...
Searching...
No Matches
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
39class XrdOucTList;
40class XrdSysError;
41
43{
44public:
45
46struct NSEnt
47{
48struct NSEnt *Next; // -> Next entry in the indexed directory
49char *Path; // Path name to file if opts & noPath (Path == File)
50char *File; // File name component
51int Plen; // strlen(Path)
52struct stat Stat; // stat() of Path if opts & retStat
53char *Link; // -> Link data if opts & retLink
54int Lksz; // Length of link data
55
57
58Etype 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//
79NSEnt *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//
88{public:
89virtual
90void isEmpty(struct stat *dStat, const char *dPath, const char *lkFn)=0;
91
93virtual ~CallBack() {}
94};
95
96void 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//
102void setMsgOn(const char *pfx) {mPfx = pfx;}
103
104// The following are processing options passed to the constructor
105//
106static const int retDir = 0x0001; // Return directories (implies retStat)
107static const int retFile= 0x0002; // Return files (implies retStat)
108static const int retLink= 0x0004; // Return link data (implies retStat)
109static const int retMisc= 0x0008; // Return other types (implies retStat)
110static const int retAll = 0x000f; // Return everything
111
112static const int retStat= 0x0010; // return stat() information
113static const int retIDLO= 0x0020; // Names returned in decreasing length order
114static const int retIILO= 0x0040; // Names returned in increasing length order
115static const int Recurse= 0x0080; // Recursive traversal, 1 Level per Index()
116static const int noPath = 0x0100; // Do not include the full directory path
117static 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
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
132private:
133void addEnt(XrdOucNSWalk::NSEnt *eP);
134int Build();
135int Emsg(const char *pfx, int rc, const char *tx1, const char *tx2=0);
136int getLink(XrdOucNSWalk::NSEnt *eP);
137int getStat(XrdOucNSWalk::NSEnt *eP, int doLstat=0);
138int getStat();
139int inXList(const char *dName);
140int isSymlink();
141int LockFile();
142void setPath(char *newpath);
143
144XrdSysError *eDest;
145XrdOucTList *DList;
146XrdOucTList *XList;
147struct NSEnt *DEnts;
148struct stat dStat;
149CallBack *edCB;
150const char *mPfx;
151char DPath[1032];
152char *File;
153char *LKFn;
154int LKfd;
155int DPfd;
156int Opts;
157int errOK;
158int isEmpty;
159};
160#endif
#define stat(a, b)
Definition XrdPosix.hh:96
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)
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)
static const int retMisc
NSEnt * Index(int &rc, const char **dPath=0)
static const int retDir
static const int Recurse
static const int retIILO
struct NSEnt * Next