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_UNKNOWN;
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 
87  // Note we fail if the stat info is needed but not available
88  if (myBuf)
89  {int rc;
90  {
91  std::unique_lock lock(ecMutex);
92  rc = XrdPosixMap::Entry2Buf(*dirEnt, *myBuf, ecMsg);
93  }
94  if (rc)
95  {eNum = rc;
96  dp = nullptr;
97  }
98  }
99  nxtEnt++;
100  return dp;
101 }
102 
103 /******************************************************************************/
104 /* S t a t R e t */
105 /******************************************************************************/
106 int XrdPosixDir::StatRet(struct stat *buf)
107 {
108  myBuf = buf;
109  return 0;
110 }
111 
112 /******************************************************************************/
113 /* O p e n */
114 /******************************************************************************/
115 
117 {
118  static const size_t dEntSize = sizeof(dirent64) + maxDlen + 1;
119  int rc;
120 
121 // Allocate a local dirent. Note that we get additional padding because on
122 // some system the dirent structure does not include the name buffer
123 //
124  if (!myDirEnt && !(myDirEnt = (dirent64 *)malloc(dEntSize)))
125  {std::unique_lock lock(ecMutex);
126  ecMsg.SetErrno(ENOMEM);
127  return (DIR*)0;
128  }
129 
130 // Get the directory list
131 //
132  auto st = DAdmin.Xrd.DirList(DAdmin.Url.GetPathWithParams(),
134  myDirVec, (uint16_t)0);
135  {
136  std::unique_lock lock(ecMutex);
137  rc = XrdPosixMap::Result(st, ecMsg);
138  }
139 
140 // If we failed, return a zero pointer ote that Result() set errno for us
141 //
142  if (rc) return (DIR *)0;
143 
144 // Finish up
145 //
146  numEnt = myDirVec->GetSize();
147  return (DIR *)&fdNum;
148 }
int stat(const char *path, struct stat *buf)
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:318
int SetErrno(int ecc, int retval=-1, const char *alt=0)
Definition: XrdOucECMsg.cc:144
XrdCl::FileSystem Xrd
XrdCl::URL Url
int StatRet(struct stat *buf)
Definition: XrdPosixDir.cc:106
DIR * Open()
Definition: XrdPosixDir.cc:116
dirent64 * nextEntry(dirent64 *dp=0)
Definition: XrdPosixDir.cc:48
static const size_t maxDlen
Definition: XrdPosixDir.hh:92
static int Entry2Buf(const XrdCl::DirectoryList::ListEntry &dirEnt, struct stat &buf, XrdOucECMsg &ecMsg)
Definition: XrdPosixMap.cc:88
static int Result(const XrdCl::XRootDStatus &Status, XrdOucECMsg &ecMsg, bool retneg1=false)
Definition: XrdPosixMap.cc:188
std::mutex ecMutex
XrdOucECMsg ecMsg
XrdCl::DirListFlags::Flags dlFlag
Definition: XrdPosixDir.cc:41