From 597f1ca5041c630b7defcd7e19df4775cb125221 Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 19 Nov 2023 15:54:57 -0500 Subject: [PATCH] cwFileSys.cpp : isDir(),isFile(),isLink() now automatically expands the path prior to evaluating it. --- cwFileSys.cpp | 97 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 35 deletions(-) diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 788e1cb..32264cc 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -58,70 +58,97 @@ namespace cw } } -bool cw::filesys::isDir( const char* dirStr ) +bool cw::filesys::isDir( const char* dir0 ) { + char* dir; struct stat s; - + bool result = false; + errno = 0; - if( dirStr == nullptr ) + if( dir0 == nullptr ) return false; - if( stat(dirStr,&s) != 0 ) + dir = expandPath(dir0); + + if( stat(dir,&s) != 0 ) { // if the dir does not exist if( errno == ENOENT ) return false; - cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'",dirStr); - return false; + cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'",cwStringNullGuard(dir)); + goto errLabel; } - return S_ISDIR(s.st_mode); -} + result = S_ISDIR(s.st_mode); -bool cw::filesys::isFile( const char* fnStr ) -{ - struct stat s; - errno = 0; - - if( fnStr == nullptr ) - return false; + errLabel: + mem::release(dir); - if( stat(fnStr,&s) != 0 ) - { - - // if the file does not exist - if( errno == ENOENT ) - return false; - - cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",fnStr); - return false; - } - - return S_ISREG(s.st_mode); + return result; } - -bool cw::filesys::isLink( const char* fnStr ) +bool cw::filesys::isFile( const char* fn0 ) { + char* fn = nullptr; + bool result = false; struct stat s; errno = 0; - if( fnStr == nullptr ) - return false; + if( fn0 == nullptr ) + goto errLabel; - if( lstat(fnStr,&s) != 0 ) + fn = expandPath(fn0); + + if( stat(fn,&s) != 0 ) + { + + // if the file does not exist + if( errno == ENOENT ) + return false; + + cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",cwStringNullGuard(fn)); + goto errLabel; + } + + result = S_ISREG(s.st_mode); + + errLabel: + mem::release(fn); + + return result; +} + + +bool cw::filesys::isLink( const char* fn0 ) +{ + bool result = false; + char* fn = nullptr; + struct stat s; + errno = 0; + + if( fn0 == nullptr ) + goto errLabel; + + fn = expandPath(fn0); + + if( lstat(fn,&s) != 0 ) { // if the file does not exist if( errno == ENOENT ) return false; - cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",fnStr); - return false; + cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",cwStringNullGuard(fn)); + goto errLabel; } - return S_ISLNK(s.st_mode); + result = S_ISLNK(s.st_mode); + + errLabel: + mem::release(fn); + + return result; }