From ee9e80110235a20d29b490e883fb717422e80ab3 Mon Sep 17 00:00:00 2001 From: Joseph <119084558+DerjenigeUberMensch@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:31:27 +0000 Subject: [PATCH] Fixed incorrect file handling (#470) --- tools/file_util.c | 39 +++++++++++++++++++++++++++++++-------- tools/file_util.h | 9 ++++++--- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/tools/file_util.c b/tools/file_util.c index 1166741..7286386 100644 --- a/tools/file_util.c +++ b/tools/file_util.c @@ -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 @@ -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) @@ -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 @@ -206,7 +229,7 @@ FFIsFileEmpty( { int ret = 0; - if(FFPathExists(FILE_NAME)) + if(FFFileExists(FILE_NAME)) { FILE *fr = fopen(FILE_NAME, "r"); if(fr) diff --git a/tools/file_util.h b/tools/file_util.h index 88a14d7..b06becf 100644 --- a/tools/file_util.h +++ b/tools/file_util.h @@ -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. */ @@ -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. */ @@ -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. */