From f8ee296930f2d758bd4557536053894555596f3f Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 17 Apr 2022 22:41:05 -0500 Subject: [PATCH] Bugfix: Problems in f_file functions exposed by unit tests. Clean up f_file_name_base() and f_file_name_directory() code. Use f_string_ascii_period_s rather than f_string_ascii_plus_s to match a period! Perform this comparison check after allocating the necessary space to ensure a NULL terminated string is returned. The POSIX standard designates that read() returns 0 on EOF. Rather than checking for size_read == file.size_read (and similar), check fo size_read to be 0. When total is 0, return F_data_not for f_file_read_until(). --- level_0/f_file/c/file.c | 30 ++++++++++++++++-------------- level_0/f_file/c/file.h | 5 +++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index b9bf345..3226d92 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -1479,12 +1479,11 @@ extern "C" { // POSIX basename() modifies the path, so protect it (and add a terminating NULL). char path_argument[path.used + 1]; - f_string_t path_to_name; memcpy(path_argument, path.string, sizeof(f_char_t) * path.used); path_argument[path.used] = 0; - path_to_name = (f_string_t) basename(path_argument); + char *path_to_name = basename(path_argument); const f_array_length_t size = strnlen(path_to_name, path.used); @@ -1511,25 +1510,24 @@ extern "C" { // POSIX dirname() modifies the path, so protect it (and add a terminating NULL). char path_argument[path.used + 1]; - f_string_t path_to_name; memcpy(path_argument, path.string, sizeof(f_char_t) * path.used); path_argument[path.used] = 0; - path_to_name = (f_string_t) dirname(path_argument); + char *path_to_name = dirname(path_argument); const f_array_length_t size = strnlen(path_to_name, path.used); - // Do not treat '.' as a directory. - if (size == 1 && f_string_ascii_plus_s.string[0]) { - return F_none; - } - { const f_status_t status = f_string_dynamic_increase_by(size + 1, name_directory); if (F_status_is_error(status)) return status; } + // Do not treat '.' as a directory. + if (size == 1 && path_to_name[0] == f_string_ascii_period_s.string[0]) { + return f_string_dynamic_terminate_after(name_directory); + } + memcpy(name_directory->string + name_directory->used, path_to_name, sizeof(f_char_t) * size); name_directory->used += size; name_directory->string[name_directory->used] = 0; @@ -1625,7 +1623,7 @@ extern "C" { buffer->used += size_read; - } while (size_read == file.size_read); + } while (size_read); return F_none_eof; } @@ -1667,11 +1665,11 @@ extern "C" { return F_status_set_error(F_failure); } - if (size_read < file.size_read) { - return F_none_eof; + if (size_read) { + return F_none; } - return F_none; + return F_none_eof; } #endif // _di_f_file_read_block_ @@ -1686,6 +1684,10 @@ extern "C" { return F_status_set_error(F_file_closed); } + if (!total) { + return F_data_not; + } + f_array_length_t buffer_size = file.size_read; f_array_length_t buffer_count = 0; @@ -1717,7 +1719,7 @@ extern "C" { buffer->used += size_read; - if (size_read < buffer_size) { + if (!size_read) { return F_none_eof; } diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 9d5f872..3db20e9 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -1355,6 +1355,8 @@ extern "C" { /** * Get the directory name of a file path. * + * This does not consider '.' a directory for the purposes of appending the directory. + * * @param path * The path file name. * Need not be NULL terminated. @@ -1372,10 +1374,12 @@ extern "C" { * F_string_too_large (with error bit) if string is too large to store in the buffer. * * Errors (with error bit) from: f_string_dynamic_increase_by(). + * Errors (with error bit) from: f_string_dynamic_terminate_after(). * * @see dirname() * * @see f_string_dynamic_increase_by() + * @see f_string_dynamic_terminate_after() */ #ifndef _di_f_file_name_directory_ extern f_status_t f_file_name_directory(const f_string_static_t path, f_string_dynamic_t * const name_directory); @@ -1573,6 +1577,7 @@ extern "C" { * @return * F_none_eof on success and EOF was reached. * F_none_stop on success and total was reached. + * F_data_not if total is 0. * * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. -- 1.8.3.1