Skip to content

Commit

Permalink
Fixed stat function status check while parsing file path
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Jan 31, 2024
1 parent 2f489b0 commit 7596a04
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
31 changes: 15 additions & 16 deletions src/sys/xfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ int xclose(int nFD)
#endif
}

int xstat(const char *pPath, struct stat *pStat)
int xstat(const char *pPath, xstat_t *pStat)
{
memset(pStat, 0, sizeof(struct stat));
memset(pStat, 0, sizeof(xstat_t));
#ifdef _WIN32
if (stat(pPath, pStat) < 0) return XSTDERR;
#else
Expand Down Expand Up @@ -257,7 +257,7 @@ int XFile_GetStats(xfile_t *pFile)
{
XASSERT(XFile_IsOpen(pFile), XSTDERR);

struct stat fileStat;
xstat_t fileStat;
if (fstat(pFile->nFD, &fileStat) < 0) return XSTDERR;

#ifdef _WIN32
Expand Down Expand Up @@ -376,8 +376,8 @@ int XFile_ReadLine(xfile_t *pFile, char* pLine, size_t nSize, size_t nLineNum)

xbool_t XPath_Exists(const char *pPath)
{
struct stat st;
memset(&st, 0, sizeof(struct stat));
xstat_t st;
memset(&st, 0, sizeof(xstat_t));
if (stat(pPath, &st) < 0) return XFALSE;
return XTRUE;
}
Expand Down Expand Up @@ -476,10 +476,9 @@ int XPath_Parse(xpath_t *pPath, const char *pPathStr, xbool_t bStat)

if (bStat)
{
struct stat st;
XSTATUS nStat = xstat(pPathStr, &st);
XASSERT((nStat >= 0), XSTDERR);
bIsDir = S_ISDIR(st.st_mode);
xstat_t st;
if (xstat(pPathStr, &st) >= 0)
bIsDir = S_ISDIR(st.st_mode);
}

if (bIsDir || pPathStr[nLength - 1] == '/')
Expand Down Expand Up @@ -600,7 +599,7 @@ int XPath_SetPerm(const char *pPath, const char *pPerm)

int XPath_GetPerm(char *pOutput, size_t nSize, const char *pPath)
{
struct stat statbuf;
xstat_t statbuf;
xstat(pPath, &statbuf);
return XPath_ModeToPerm(pOutput, nSize, statbuf.st_mode);
}
Expand Down Expand Up @@ -796,7 +795,7 @@ int XDir_Read(xdir_t *pDir, char *pFile, size_t nSize)

int XDir_Valid(const char *pPath)
{
struct stat statbuf = {0};
xstat_t statbuf = {0};
int nStatus = stat(pPath, &statbuf);
if (nStatus < 0) return nStatus;

Expand Down Expand Up @@ -842,7 +841,7 @@ int XDir_Create(const char *pDir, xmode_t nMode)

int XDir_Unlink(const char *pPath)
{
struct stat statbuf;
xstat_t statbuf;
if (!stat(pPath, &statbuf))
{
return (S_ISDIR(statbuf.st_mode)) ?
Expand Down Expand Up @@ -901,7 +900,7 @@ void XFile_InitEntry(xfile_entry_t *pEntry)
pEntry->nUID = 0;
}

void XFile_CreateEntry(xfile_entry_t *pEntry, const char *pName, const char *pPath, struct stat *pStat)
void XFile_CreateEntry(xfile_entry_t *pEntry, const char *pName, const char *pPath, xstat_t *pStat)
{
XFile_InitEntry(pEntry);

Expand Down Expand Up @@ -940,7 +939,7 @@ xfile_entry_t* XFile_AllocEntry()
return pEntry;
}

xfile_entry_t* XFile_NewEntry(const char *pName, const char *pPath, struct stat *pStat)
xfile_entry_t* XFile_NewEntry(const char *pName, const char *pPath, xstat_t *pStat)
{
xfile_entry_t *pEntry = (xfile_entry_t*)malloc(sizeof(xfile_entry_t));
if (pEntry == NULL) return NULL;
Expand Down Expand Up @@ -1065,7 +1064,7 @@ static xbool_t XFile_SearchName(xfile_search_t *pSearch, const char *pFileName)
return bFound;
}

static xbool_t XFile_CheckCriteria(xfile_search_t *pSearch, const char *pPath, const char *pName, struct stat *pStat)
static xbool_t XFile_CheckCriteria(xfile_search_t *pSearch, const char *pPath, const char *pName, xstat_t *pStat)
{
if (pSearch->nLinkCount >= 0 && pSearch->nLinkCount != pStat->st_nlink) return XFALSE;
if (pSearch->nFileSize >= 0 && pSearch->nFileSize != pStat->st_size) return XFALSE;
Expand Down Expand Up @@ -1220,7 +1219,7 @@ int XFile_Search(xfile_search_t *pSearch, const char *pDirectory)
{
char sFullPath[XPATH_MAX];
char sDirPath[XPATH_MAX];
struct stat statbuf;
xstat_t statbuf;

/* Don't add slash twice if directory already contains slash character at the end */
const char *pSlash = (pDirectory[nDirLen - 1] != '/') ? "/" : XSTR_EMPTY;
Expand Down
10 changes: 6 additions & 4 deletions src/sys/xfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ typedef struct XPath {
char sFile[XNAME_MAX];
} xpath_t;

#define XFILE_CHECK_FL(c, f) (((c) & (f)) == (f))
typedef struct stat xstat_t;

#define xfprintf(xf, ...) \
XFile_Print(xf, __VA_ARGS__)

int xstat(const char *pPath, struct stat *pStat);
#define XFILE_CHECK_FL(c, f) (((c) & (f)) == (f))

int xstat(const char *pPath, xstat_t *pStat);
int xmkdir(const char* pPath, xmode_t nMode);
int xunlink(const char* pPath);
int xrmdir(const char* pPath);
Expand Down Expand Up @@ -144,8 +146,8 @@ typedef struct XFileEntry {
char *pRealPath;
} xfile_entry_t;

void XFile_CreateEntry(xfile_entry_t *pEntry, const char *pName, const char *pPath, struct stat *pStat);
xfile_entry_t* XFile_NewEntry(const char *pName, const char *pPath, struct stat *pStat);
void XFile_CreateEntry(xfile_entry_t *pEntry, const char *pName, const char *pPath, xstat_t *pStat);
xfile_entry_t* XFile_NewEntry(const char *pName, const char *pPath, xstat_t *pStat);
xfile_entry_t* XFile_AllocEntry();

void XFile_InitEntry(xfile_entry_t *pEntry);
Expand Down
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 5
#define XUTILS_BUILD_NUMBER 49
#define XUTILS_BUILD_NUMBER 50

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion tools/xsrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ static void XSearch_ColorizeSymlink(char *pSimlink, size_t nSize, xfile_entry_t
{
if (pEntry->pRealPath != NULL)
{
struct stat statbuf;
xstat_t statbuf;
xfile_entry_t linkEntry;
xstat(pEntry->pRealPath, &statbuf);

Expand Down

0 comments on commit 7596a04

Please sign in to comment.