00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 const char *XrdSysDirCVSID = "$Id: XrdSysDir.cc 30949 2009-11-02 16:37:58Z ganis $";
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "XrdSys/XrdSysDir.hh"
00023
00024 #if !defined(WINDOWS)
00025 #include <dirent.h>
00026 #else
00027 #include <windows.h>
00028 #endif
00029
00030 #include <errno.h>
00031 #include <string.h>
00032
00033
00034 XrdSysDir::XrdSysDir(const char *path)
00035 {
00036
00037
00038
00039
00040 lasterr = 0;
00041 if (path && strlen(path) > 0) {
00042 #if !defined(WINDOWS)
00043 dhandle = (void *) opendir(path);
00044 if (!dhandle)
00045 lasterr = errno;
00046 #else
00047 WIN32_FIND_DATA filedata;
00048 dhandle = (void *) ::FindFirstFile(path, &filedata);
00049 if ((HANDLE)dhandle == INVALID_HANDLE_VALUE) {
00050 lasterr = EINVAL;
00051 dhandle = 0;
00052 }
00053 else if (!(filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
00054 lasterr = ENOTDIR;
00055 dhandle = 0;
00056 }
00057 #endif
00058 } else
00059
00060 lasterr = EINVAL;
00061 }
00062
00063
00064 XrdSysDir::~XrdSysDir()
00065 {
00066
00067
00068 if (dhandle) {
00069 #if !defined(WINDOWS)
00070 closedir((DIR *)dhandle);
00071 #else
00072 ::FindClose((HANDLE)dhandle);
00073 #endif
00074 }
00075 }
00076
00077
00078 char *XrdSysDir::nextEntry()
00079 {
00080
00081
00082
00083
00084 char *dent = 0;
00085
00086 lasterr = 0;
00087 if (!dhandle) {
00088 lasterr = EINVAL;
00089 return dent;
00090 }
00091
00092 #if !defined(WINDOWS)
00093 struct dirent *ent = readdir((DIR *)dhandle);
00094 if (!ent) {
00095 if (errno == EBADF)
00096 lasterr = errno;
00097 } else {
00098 dent = (char *) ent->d_name;
00099 }
00100 #else
00101 WIN32_FIND_DATA filedata;
00102 if (::FindNextFile((HANDLE)dhandle, &filedata)) {
00103 dent = (char *) filedata.cFileName;
00104 } else {
00105 if (::GetLastError() != ERROR_NO_MORE_FILES)
00106 lasterr = EBADF;
00107 }
00108 #endif
00109
00110 return dent;
00111 }
00112