XrdSysDir.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                     X r d S y s D i r . h h                                */
00004 /*                                                                            */
00005 /* (c) 2006 G. Ganis (CERN)                                                   */
00006 /*     All Rights Reserved. See XrdInfo.cc for complete License Terms         */
00007 /******************************************************************************/
00008 // $Id: XrdSysDir.cc 30949 2009-11-02 16:37:58Z ganis $
00009 
00010 const char *XrdSysDirCVSID = "$Id: XrdSysDir.cc 30949 2009-11-02 16:37:58Z ganis $";
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // XrdSysDir                                                            //
00015 //                                                                      //
00016 // Author: G. Ganis, CERN, 2006                                         //
00017 //                                                                      //
00018 // API for handling directories                                         //
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    // Constructor. Initialize a directory handle for 'path'.
00037    // Use isValid() to check the result of this operation, and lastError()
00038    // to get the last error code, if any.
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       // Invalid argument
00060       lasterr = EINVAL;
00061 }
00062 
00063 //______________________________________________________________________________
00064 XrdSysDir::~XrdSysDir()
00065 {
00066    // Destructor.
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    // Get next entry in directory structure.
00081    // Return 0 if no more entries or error. In the latter case
00082    // the error code can be retrieved via lastError().
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    // Done
00110    return dent;
00111 }
00112 

Generated on Tue Jul 5 14:47:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1