XRootD
XrdPosixDir.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d P o s i x D i r . c c */
4 /* */
5 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /* Modified by Frank Winklmeier to add the full Posix file system definition. */
30 /******************************************************************************/
31 
32 #include "XrdPosix/XrdPosixDir.hh"
33 #include "XrdPosix/XrdPosixMap.hh"
34 
35 /******************************************************************************/
36 /* G l o b a l s */
37 /******************************************************************************/
38 
39 namespace XrdPosixGlobals
40 {
42 };
43 
44 /******************************************************************************/
45 /* n e x t E n t r y */
46 /******************************************************************************/
47 
48 dirent64 *XrdPosixDir::nextEntry(dirent64 *dp)
49 {
51  const char *d_name;
52  const int dirhdrln = dp->d_name - (char *)dp;
53  size_t d_nlen;
54 
55 // Reread the directory if we need to (rewind forces this)
56 //
57  if (!myDirVec && !Open()) {eNum = errno; return 0;} // Open() sets ecMsg
58 
59 // Check if dir is empty or all entries have been read
60 //
61  if (nxtEnt >= numEnt) {eNum = 0; return 0;}
62 
63 // Get information about the next entry
64 //
65  dirEnt = myDirVec->At(nxtEnt);
66  d_name = dirEnt->GetName().c_str();
67  d_nlen = dirEnt->GetName().length();
68 
69 // Create a directory entry
70 //
71  if (!dp) dp = myDirEnt;
72  if (d_nlen > maxDlen) d_nlen = maxDlen;
73 #ifndef __solaris__
74  dp->d_type = DT_DIR;
75 #endif
76 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
77  dp->d_fileno = nxtEnt;
78  dp->d_namlen = d_nlen;
79 #else
80  dp->d_ino = nxtEnt+1;
81  dp->d_off = nxtEnt;
82 #endif
83  dp->d_reclen = d_nlen + dirhdrln;
84  strncpy(dp->d_name, d_name, d_nlen);
85  dp->d_name[d_nlen] = '\0';
86  nxtEnt++;
87  return dp;
88 }
89 
90 /******************************************************************************/
91 /* O p e n */
92 /******************************************************************************/
93 
95 {
96  static const size_t dEntSize = sizeof(dirent64) + maxDlen + 1;
97  int rc;
98 
99 // Allocate a local dirent. Note that we get additional padding because on
100 // some system the dirent structure does not include the name buffer
101 //
102  if (!myDirEnt && !(myDirEnt = (dirent64 *)malloc(dEntSize)))
103  {ecMsg.SetErrno(ENOMEM);
104  return (DIR*)0;
105  }
106 
107 // Get the directory list
108 //
109  rc = XrdPosixMap::Result(DAdmin.Xrd.DirList(DAdmin.Url.GetPathWithParams(),
111  myDirVec, (uint16_t)0),ecMsg);
112 
113 // If we failed, return a zero pointer ote that Result() set errno for us
114 //
115  if (rc) return (DIR *)0;
116 
117 // Finish up
118 //
119  numEnt = myDirVec->GetSize();
120  return (DIR *)&fdNum;
121 }
const std::string & GetName() const
Get file name.
ListEntry * At(uint32_t index)
Get an entry at given index.
uint32_t GetSize() const
Get the size of the listing.
XRootDStatus DirList(const std::string &path, DirListFlags::Flags flags, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
std::string GetPathWithParams() const
Get the path with params.
Definition: XrdClURL.cc:311
int SetErrno(int ecc, int retval=-1, const char *alt=0)
Definition: XrdOucECMsg.cc:144
XrdCl::FileSystem Xrd
XrdCl::URL Url
DIR * Open()
Definition: XrdPosixDir.cc:94
dirent64 * nextEntry(dirent64 *dp=0)
Definition: XrdPosixDir.cc:48
static const size_t maxDlen
Definition: XrdPosixDir.hh:84
static int Result(const XrdCl::XRootDStatus &Status, XrdOucECMsg &ecMsg, bool retneg1=false)
Definition: XrdPosixMap.cc:150
XrdOucECMsg ecMsg
XrdCl::DirListFlags::Flags dlFlag
Definition: XrdPosixDir.cc:41