From 825a359af2d6a2362bbe1a7c4a2a9d35c396b938 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 15 Feb 2013 16:01:22 -0800 Subject: [PATCH] cmFileSys.h/c: Symbolic link fixes. Added kRecurseLInksFsFl and kLinkFsFl. Changed stat() to lstat() in _cmFileSysIsLink(). Added _cmFileSysIsSocket() --- cmFileSys.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++------ cmFileSys.h | 18 +++++++++-------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/cmFileSys.c b/cmFileSys.c index ebca145..800812c 100644 --- a/cmFileSys.c +++ b/cmFileSys.c @@ -273,7 +273,7 @@ bool _cmFileSysIsLink( cmFs_t* p, const cmChar_t* fnStr ) struct stat s; errno = 0; - if( stat(fnStr,&s) != 0 ) + if( lstat(fnStr,&s) != 0 ) { // if the file does not exist @@ -294,6 +294,26 @@ bool cmFileSysIsLink( cmFileSysH_t h, const cmChar_t* fnStr ) return _cmFileSysIsLink(p,fnStr); } +bool _cmFileSysIsSocket( cmFs_t* p, const cmChar_t* fnStr ) +{ + struct stat s; + errno = 0; + + if( stat(fnStr,&s) != 0 ) + { + + // if the file does not exist + if( errno == ENOENT ) + return false; + + _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr); + return false; + } + + return S_ISSOCK(s.st_mode); +} + + bool _cmFileSysConcat( cmChar_t* rp, unsigned rn, char sepChar, const cmChar_t* suffixStr ) { unsigned m = strlen(rp); @@ -777,8 +797,8 @@ typedef struct cmFs_t* p; unsigned filterFlags; cmFileSysDirEntry_t* rp; - cmChar_t* dataPtr; - cmChar_t* endPtr; + cmChar_t* dataPtr; + cmChar_t* endPtr; unsigned entryCnt; unsigned entryIdx; unsigned dataByteCnt; @@ -798,6 +818,9 @@ cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirSt if( dirStr == NULL || strlen(dirStr) == 0 ) dirStr = curDirPtr; + if( _cmFileSysIsDir(drp->p,dirStr) == false ) + return rc; + unsigned fnCharCnt= strlen(dirStr) + PATH_MAX; char fn[ fnCharCnt + 1 ]; @@ -821,10 +844,10 @@ cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirSt if((dirp = opendir(dirStr)) == NULL) { rc = _cmFileSysError(drp->p,kOpenDirFailFsRC,errno,"Unable to open the directory:'%s'.",dirStr); + goto errLabel; } - // get the next directory entry while((dp = readdir(dirp)) != NULL ) { @@ -873,6 +896,7 @@ cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirSt goto errLabel; } + // is the entry a file if( _cmFileSysIsFile(drp->p,fn) ) { @@ -895,9 +919,27 @@ cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirSt if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC ) goto errLabel; } + else + { + if( _cmFileSysIsLink(drp->p,fn) ) + { + if( cmIsFlag(drp->filterFlags,kLinkFsFl) == false ) + continue; + + flags |= kLinkFsFl; + + if( cmIsFlag(drp->filterFlags,kRecurseLinksFsFl) ) + if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC ) + goto errLabel; + } + else + { + continue; + } + } } - assert(flags != 0); + //assert(flags != 0); if( drp->passIdx == 0 ) { @@ -941,6 +983,9 @@ cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirSt } errLabel: + if( dirp != NULL ) + closedir(dirp); + return rc; } @@ -962,7 +1007,7 @@ cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirSt if((rc = _cmFileSysDirGetEntries( &r, dirStr )) != kOkFsRC ) goto errLabel; - if( r.passIdx == 0 ) + if( r.passIdx == 0 && r.dataByteCnt>0 ) { // allocate memory to hold the return values if(( r.rp = (cmFileSysDirEntry_t *)cmLHeapAllocZ( r.p->heapH, r.dataByteCnt )) == NULL ) diff --git a/cmFileSys.h b/cmFileSys.h index 0dddca3..71488c7 100644 --- a/cmFileSys.h +++ b/cmFileSys.h @@ -146,16 +146,18 @@ extern "C" { // Flags used by cmFileSysDirEntries 'includeFlags' parameter. enum { - kFileFsFl = 0x01, //< include all visible files - kDirFsFl = 0x02, //< include all visible directory - kInvisibleFsFl = 0x04, //< include file/dir name beginning with a '.' - kCurDirFsFl = 0x08, //< include '.' directory - kParentDirFsFl = 0x10, //< include '..' directory + kFileFsFl = 0x001, //< include all visible files + kDirFsFl = 0x002, //< include all visible directory + kLinkFsFl = 0x004, //< include all symbolic links + kInvisibleFsFl = 0x008, //< include file/dir name beginning with a '.' + kCurDirFsFl = 0x010, //< include '.' directory + kParentDirFsFl = 0x020, //< include '..' directory - kAllFsFl = 0x1f, //< all type flags + kAllFsFl = 0x02f, //< all type flags - kFullPathFsFl = 0x40, //< return the full path in the 'name' field of cmFileSysDirEntry_t; - kRecurseFsFl = 0x80 //< recurse into directories + kFullPathFsFl = 0x040, //< return the full path in the 'name' field of cmFileSysDirEntry_t; + kRecurseFsFl = 0x080, //< recurse into directories + kRecurseLinksFsFl = 0x100 //< recurse into symbol link directories };