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:
134int Build();
135int Emsg(const char *pfx, int rc, const char *tx1, const char *tx2=0);
137int getStat(XrdOucNSWalk::NSEnt *eP, int doLstat=0);
139int inXList(const char *dName);
142void setPath(char *newpath);
143
147struct NSEnt *DEnts;
148struct stat dStat;
150const char *mPfx;
151char DPath[1032];
152char *File;
153char *LKFn;
159};
160#endif
#define stat(a, b)
Definition XrdPosix.hh:96
Definition XrdOucNSWalk.hh:88
virtual ~CallBack()
Definition XrdOucNSWalk.hh:93
CallBack()
Definition XrdOucNSWalk.hh:92
virtual void isEmpty(struct stat *dStat, const char *dPath, const char *lkFn)=0
Definition XrdOucNSWalk.hh:43
static const int retFile
Definition XrdOucNSWalk.hh:107
char * File
Definition XrdOucNSWalk.hh:152
void addEnt(XrdOucNSWalk::NSEnt *eP)
static const int retAll
Definition XrdOucNSWalk.hh:110
const char * mPfx
Definition XrdOucNSWalk.hh:150
char DPath[1032]
Definition XrdOucNSWalk.hh:151
int getLink(XrdOucNSWalk::NSEnt *eP)
int errOK
Definition XrdOucNSWalk.hh:157
XrdOucNSWalk(XrdSysError *erp, const char *dname, const char *LKfn=0, int opts=retAll, XrdOucTList *xP=0)
static const int retIDLO
Definition XrdOucNSWalk.hh:113
CallBack * edCB
Definition XrdOucNSWalk.hh:149
int LKfd
Definition XrdOucNSWalk.hh:154
void setMsgOn(const char *pfx)
Definition XrdOucNSWalk.hh:102
static const int retLink
Definition XrdOucNSWalk.hh:108
static const int skpErrs
Definition XrdOucNSWalk.hh:117
struct stat dStat
Definition XrdOucNSWalk.hh:148
static const int noPath
Definition XrdOucNSWalk.hh:116
int Opts
Definition XrdOucNSWalk.hh:156
static const int retStat
Definition XrdOucNSWalk.hh:112
XrdOucTList * XList
Definition XrdOucNSWalk.hh:146
struct NSEnt * DEnts
Definition XrdOucNSWalk.hh:147
void setCallBack(CallBack *cbP=0)
Definition XrdOucNSWalk.hh:96
static const int retMisc
Definition XrdOucNSWalk.hh:109
char * LKFn
Definition XrdOucNSWalk.hh:153
int isEmpty
Definition XrdOucNSWalk.hh:158
int getStat(XrdOucNSWalk::NSEnt *eP, int doLstat=0)
NSEnt * Index(int &rc, const char **dPath=0)
int inXList(const char *dName)
XrdOucTList * DList
Definition XrdOucNSWalk.hh:145
int Emsg(const char *pfx, int rc, const char *tx1, const char *tx2=0)
static const int retDir
Definition XrdOucNSWalk.hh:106
static const int Recurse
Definition XrdOucNSWalk.hh:115
void setPath(char *newpath)
int DPfd
Definition XrdOucNSWalk.hh:155
XrdSysError * eDest
Definition XrdOucNSWalk.hh:144
static const int retIILO
Definition XrdOucNSWalk.hh:114
Definition XrdOucTList.hh:42
Definition XrdSysError.hh:90
Definition XrdOucNSWalk.hh:47
NSEnt()
Definition XrdOucNSWalk.hh:60
Etype Type
Definition XrdOucNSWalk.hh:58
Etype
Definition XrdOucNSWalk.hh:56
@ isDir
Definition XrdOucNSWalk.hh:56
@ isFile
Definition XrdOucNSWalk.hh:56
@ isBad
Definition XrdOucNSWalk.hh:56
@ isLink
Definition XrdOucNSWalk.hh:56
@ isMisc
Definition XrdOucNSWalk.hh:56
struct stat Stat
Definition XrdOucNSWalk.hh:52
~NSEnt()
Definition XrdOucNSWalk.hh:61
struct NSEnt * Next
Definition XrdOucNSWalk.hh:48
char * Link
Definition XrdOucNSWalk.hh:53
char * Path
Definition XrdOucNSWalk.hh:49
int Lksz
Definition XrdOucNSWalk.hh:54
char * File
Definition XrdOucNSWalk.hh:50
int Plen
Definition XrdOucNSWalk.hh:51