Skip to content

Commit

Permalink
Fixed incorrect file handling (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerjenigeUberMensch authored Nov 7, 2024
1 parent 95d4098 commit ee9e801
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
39 changes: 31 additions & 8 deletions tools/file_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ FFDirExists(

statstatus = stat(DIR_NAME, &st);

const int EXISTS = statstatus != NOT_FOUND && S_ISDIR(st.st_mode);

const int FOUND = statstatus != NOT_FOUND;
const int IS_DIRECTORY = S_ISDIR(st.st_mode);

return EXISTS;
return FOUND && IS_DIRECTORY;
}


int
FFCreateDir(
char *const DIR_NAME
Expand Down Expand Up @@ -165,7 +164,7 @@ FFCreatePath(
{ FULL_PATH[file_index] = old_char;
}

if(!FFPathExists(FULL_PATH))
if(!FFFileExists(FULL_PATH))
{
FILE *f = fopen(FULL_PATH, "ab+");
if(!f)
Expand All @@ -182,14 +181,38 @@ FFPathExists(
char *const FULL_PATH
)
{
return FFDirExists(FULL_PATH);
if(!FULL_PATH)
{ return EXIT_FAILURE;
}
const int NOT_FOUND = -1;
int statstatus = 0;
struct stat st = {0};

statstatus = stat(FULL_PATH, &st);

const int FOUND = statstatus != NOT_FOUND;

return FOUND;
}

int
FFFileExists(
char *const FILE_NAME
)
{ return FFPathExists(FILE_NAME);
{
if(!FILE_NAME)
{ return EXIT_FAILURE;
}
const int NOT_FOUND = -1;
int statstatus = 0;
struct stat st = {0};

statstatus = stat(FILE_NAME, &st);

const int FOUND = statstatus != NOT_FOUND;
const int IS_FILE = S_ISREG(st.st_mode);

return FOUND && IS_FILE;
}

int
Expand All @@ -206,7 +229,7 @@ FFIsFileEmpty(
{
int ret = 0;

if(FFPathExists(FILE_NAME))
if(FFFileExists(FILE_NAME))
{
FILE *fr = fopen(FILE_NAME, "r");
if(fr)
Expand Down
9 changes: 6 additions & 3 deletions tools/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ FFGetSysConfigPath(
unsigned int *len_return
);

/*
/* Directory checking checks if the specified directory exists.
*
* RETURN: EXIT_SUCCESS on Success.
* RETURN: EXIT_FAILURE on Failure.
*/
Expand Down Expand Up @@ -99,7 +100,8 @@ FFCreatePath(
char *const FULL_PATH
);

/*
/* Path checking simply checks if the specified path exists, this does not care if its a directory or regular file.
*
* RETURN: 1 on Path Exists.
* RETURN: 0 on Path Doesnt Exists.
*/
Expand All @@ -108,7 +110,8 @@ FFPathExists(
char *const FULL_PATH
);

/*
/* File checking checks if it exists via the specified path.
*
* RETURN: 1 on File Exists.
* RETURN: 0 on File Doesnt Exists.
*/
Expand Down

0 comments on commit ee9e801

Please sign in to comment.