XRootD
XrdSysDir.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S y s D i r . h h */
4 /* */
5 /* (c) 2006 G. Ganis (CERN) */
6 /* */
7 /* This file is part of the XRootD software suite. */
8 /* */
9 /* XRootD is free software: you can redistribute it and/or modify it under */
10 /* the terms of the GNU Lesser General Public License as published by the */
11 /* Free Software Foundation, either version 3 of the License, or (at your */
12 /* option) any later version. */
13 /* */
14 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
15 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
16 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
17 /* License for more details. */
18 /* */
19 /* You should have received a copy of the GNU Lesser General Public License */
20 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
21 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
22 /* */
23 /* The copyright holder's institutional names and contributor's names may not */
24 /* be used to endorse or promote products derived from this software without */
25 /* specific prior written permission of the institution or contributor. */
26 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */
27 /******************************************************************************/
28 
30 // //
31 // XrdSysDir //
32 // //
33 // Author: G. Ganis, CERN, 2006 //
34 // //
35 // API for handling directories //
36 // //
38 
39 #include "XrdSys/XrdSysDir.hh"
40 
41 #if !defined(WINDOWS)
42 #include <dirent.h>
43 #else
44 #include <windows.h>
45 #endif
46 
47 #include <cerrno>
48 #include <cstring>
49 
50 //______________________________________________________________________________
51 XrdSysDir::XrdSysDir(const char *path)
52 {
53  // Constructor. Initialize a directory handle for 'path'.
54  // Use isValid() to check the result of this operation, and lastError()
55  // to get the last error code, if any.
56 
57  lasterr = 0; dhandle = 0;
58  if (path && strlen(path) > 0) {
59 #if !defined(WINDOWS)
60  dhandle = (void *) opendir(path);
61  if (!dhandle)
62  lasterr = errno;
63 #else
64  WIN32_FIND_DATA filedata;
65  dhandle = (void *) ::FindFirstFile(path, &filedata);
66  if ((HANDLE)dhandle == INVALID_HANDLE_VALUE) {
67  lasterr = EINVAL;
68  dhandle = 0;
69  }
70  else if (!(filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
71  lasterr = ENOTDIR;
72  dhandle = 0;
73  }
74 #endif
75  } else
76  // Invalid argument
77  lasterr = EINVAL;
78 }
79 
80 //______________________________________________________________________________
82 {
83  // Destructor.
84 
85  if (dhandle) {
86 #if !defined(WINDOWS)
87  closedir((DIR *)dhandle);
88 #else
89  ::FindClose((HANDLE)dhandle);
90 #endif
91  }
92 }
93 
94 //______________________________________________________________________________
96 {
97  // Get next entry in directory structure.
98  // Return 0 if no more entries or error. In the latter case
99  // the error code can be retrieved via lastError().
100 
101  char *dent = 0;
102 
103  lasterr = 0;
104  if (!dhandle) {
105  lasterr = EINVAL;
106  return dent;
107  }
108 
109 #if !defined(WINDOWS)
110  struct dirent *ent = readdir((DIR *)dhandle);
111  if (!ent) {
112  if (errno == EBADF)
113  lasterr = errno;
114  } else {
115  dent = (char *) ent->d_name;
116  }
117 #else
118  WIN32_FIND_DATA filedata;
119  if (::FindNextFile((HANDLE)dhandle, &filedata)) {
120  dent = (char *) filedata.cFileName;
121  } else {
122  if (::GetLastError() != ERROR_NO_MORE_FILES)
123  lasterr = EBADF;
124  }
125 #endif
126  // Done
127  return dent;
128 }
129 
struct dirent * readdir(DIR *dirp)
int closedir(DIR *dirp)
DIR * opendir(const char *path)
virtual ~XrdSysDir()
Definition: XrdSysDir.cc:81
XrdSysDir(const char *path)
Definition: XrdSysDir.cc:51
char * nextEntry()
Definition: XrdSysDir.cc:95