cwFileSys.cpp : isDir(),isFile(),isLink() now automatically expands the path prior to evaluating it.

This commit is contained in:
kevin 2023-11-19 15:54:57 -05:00
parent d4d325c413
commit 597f1ca504

View File

@ -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; struct stat s;
bool result = false;
errno = 0; errno = 0;
if( dirStr == nullptr ) if( dir0 == nullptr )
return false; return false;
if( stat(dirStr,&s) != 0 ) dir = expandPath(dir0);
if( stat(dir,&s) != 0 )
{ {
// if the dir does not exist // if the dir does not exist
if( errno == ENOENT ) if( errno == ENOENT )
return false; return false;
cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'",dirStr); cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'",cwStringNullGuard(dir));
return false; goto errLabel;
} }
return S_ISDIR(s.st_mode); result = S_ISDIR(s.st_mode);
errLabel:
mem::release(dir);
return result;
} }
bool cw::filesys::isFile( const char* fnStr ) bool cw::filesys::isFile( const char* fn0 )
{ {
char* fn = nullptr;
bool result = false;
struct stat s; struct stat s;
errno = 0; errno = 0;
if( fnStr == nullptr ) if( fn0 == nullptr )
return false; goto errLabel;
if( stat(fnStr,&s) != 0 ) fn = expandPath(fn0);
if( stat(fn,&s) != 0 )
{ {
// if the file does not exist // if the file does not exist
if( errno == ENOENT ) if( errno == ENOENT )
return false; return false;
cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",fnStr); cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",cwStringNullGuard(fn));
return false; goto errLabel;
} }
return S_ISREG(s.st_mode); result = S_ISREG(s.st_mode);
errLabel:
mem::release(fn);
return result;
} }
bool cw::filesys::isLink( const char* fnStr ) bool cw::filesys::isLink( const char* fn0 )
{ {
bool result = false;
char* fn = nullptr;
struct stat s; struct stat s;
errno = 0; errno = 0;
if( fnStr == nullptr ) if( fn0 == nullptr )
return false; goto errLabel;
if( lstat(fnStr,&s) != 0 ) fn = expandPath(fn0);
if( lstat(fn,&s) != 0 )
{ {
// if the file does not exist // if the file does not exist
if( errno == ENOENT ) if( errno == ENOENT )
return false; return false;
cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",fnStr); cwLogSysError( kOpFailRC, errno, "'stat' failed on '%s'.",cwStringNullGuard(fn));
return false; goto errLabel;
} }
return S_ISLNK(s.st_mode); result = S_ISLNK(s.st_mode);
errLabel:
mem::release(fn);
return result;
} }