From: Kevin Day Date: Sun, 17 Apr 2022 23:18:04 +0000 (-0500) Subject: Update: Redesign f_file functions to not need struct stat passed and other improvements. X-Git-Tag: 0.5.10~200 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=aa5838d6d3a1b1c15630cd4e4688aa16a9acf57c;p=fll Update: Redesign f_file functions to not need struct stat passed and other improvements. While working on unit tests I noticed that passing struct stat seems inconsistent. While there is likely a performance gain of not having to call the stat() functions, the inconsistency is undesirable. Optimizations can be done under another API series. Add missing path.used check. --- diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 24cf181..b9bf345 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -188,7 +188,7 @@ extern "C" { if (macro_f_file_type_is_link(source_stat.st_mode)) { f_string_dynamic_t target = f_string_dynamic_t_initialize; - status = private_f_file_link_read(source, source_stat, &target); + status = private_f_file_link_read(source, source_stat.st_size, &target); if (F_status_is_error(status)) { f_string_dynamic_resize(0, &target); @@ -609,9 +609,8 @@ extern "C" { #endif // _di_f_file_link_hard_at_ #ifndef _di_f_file_link_read_ - f_status_t f_file_link_read(const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) { + f_status_t f_file_link_read(const f_string_static_t path, const bool dereference, f_string_dynamic_t * const target) { #ifndef _di_level_0_parameter_checking_ - if (!link_stat.st_size) return F_status_set_error(F_parameter); if (!target) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ @@ -619,14 +618,22 @@ extern "C" { return F_data_not; } - return private_f_file_link_read(path, link_stat, target); + struct stat stat_file; + + memset(&stat_file, 0, sizeof(struct stat)); + + { + const f_status_t status = private_f_file_stat(path, dereference, &stat_file); + if (F_status_is_error(status)) return status; + } + + return private_f_file_link_read(path, stat_file.st_size, target); } #endif // _di_f_file_link_read_ #ifndef _di_f_file_link_read_at_ - f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) { + f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const int flag, f_string_dynamic_t * const target) { #ifndef _di_level_0_parameter_checking_ - if (!link_stat.st_size) return F_status_set_error(F_parameter); if (!target) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ @@ -634,7 +641,16 @@ extern "C" { return F_data_not; } - return private_f_file_link_read_at(at_id, path, link_stat, target); + struct stat stat_file; + + memset(&stat_file, 0, sizeof(struct stat)); + + { + const f_status_t status = private_f_file_stat_at(at_id, path, flag, &stat_file); + if (F_status_is_error(status)) return status; + } + + return private_f_file_link_read_at(at_id, path, stat_file.st_size, target); } #endif // _di_f_file_link_read_at_ @@ -1343,17 +1359,21 @@ extern "C" { #endif // _di_f_file_mode_read_ #ifndef _di_f_file_mode_read_at_ - f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, mode_t * const mode) { + f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, const int flag, mode_t * const mode) { #ifndef _di_level_0_parameter_checking_ if (!mode) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ + if (!path.used) { + return F_data_not; + } + struct stat stat_file; memset(&stat_file, 0, sizeof(struct stat)); { - const f_status_t status = private_f_file_stat(path, F_true, &stat_file); + const f_status_t status = private_f_file_stat_at(at_id, path, F_true, &stat_file); if (F_status_is_error(status)) return status; } diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index d6a876b..9d5f872 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -957,8 +957,9 @@ extern "C" { * * @param path * The path file name. - * @param link_stat - * The link file statistics. + * @param dereference + * Set to TRUE to dereference symlinks. + * Set to FALSE to operate on the symlink itself. * @param target * Will be replaced with the path in which the link points to. * Will be NULL terminated with the NULL at target.string[target.used]; @@ -982,13 +983,15 @@ extern "C" { * Errors (with error bit) from: f_string_dynamic_increase_by() * Errors (with error bit) from: f_string_dynamic_terminate_after() * + * @see lstat() * @see readlink() + * @see stat() * * @see f_string_dynamic_increase_by() * @see f_string_dynamic_terminate_after() */ #ifndef _di_f_file_link_read_ - extern f_status_t f_file_link_read(const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target); + extern f_status_t f_file_link_read(const f_string_static_t path, const bool dereference, f_string_dynamic_t * const target); #endif // _di_f_file_link_read_ /** @@ -1001,8 +1004,8 @@ extern "C" { * The parent directory, as an open directory file descriptor, in which path is relative to. * @param path * The path file name. - * @param link_stat - * The link file statistics. + * @param flag + * Any valid flag, such as F_file_at_path_empty_d, F_file_at_automount_no_d, or F_file_at_symlink_follow_no_d. * @param target * Will be replaced with the path in which the link points to. * Will be NULL terminated with the NULL at target.string[target.used]; @@ -1027,13 +1030,14 @@ extern "C" { * Errors (with error bit) from: f_string_dynamic_increase_by() * Errors (with error bit) from: f_string_dynamic_terminate_after() * + * @see fstatat() * @see readlinkat() * * @see f_string_dynamic_increase_by() * @see f_string_dynamic_terminate_after() */ #ifndef _di_f_file_link_read_at_ - extern f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target); + extern f_status_t f_file_link_read_at(const int at_id, const f_string_static_t path, const int flag, f_string_dynamic_t * const target); #endif // _di_f_file_link_read_at_ /** @@ -1204,6 +1208,8 @@ extern "C" { * The parent directory, as an open directory file descriptor, in which path is relative to. * @param path * The path file name. + * @param flag + * Any valid flag, such as F_file_at_path_empty_d, F_file_at_automount_no_d, or F_file_at_symlink_follow_no_d. * @param mode * The read file mode. * @@ -1224,7 +1230,7 @@ extern "C" { * @see fstatat() */ #ifndef _di_f_file_mode_read_at_ - extern f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, mode_t * const mode); + extern f_status_t f_file_mode_read_at(const int at_id, const f_string_static_t path, const int flag, mode_t * const mode); #endif // _di_f_file_mode_read_at_ /** diff --git a/level_0/f_file/c/private-file.c b/level_0/f_file/c/private-file.c index 1850fc6..fa2ee95 100644 --- a/level_0/f_file/c/private-file.c +++ b/level_0/f_file/c/private-file.c @@ -402,28 +402,30 @@ extern "C" { #endif // !defined(_di_f_file_link_at_) || !defined(_di_f_file_copy_at_) #if !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) - f_status_t private_f_file_link_read(const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) { + f_status_t private_f_file_link_read(const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) { target->used = 0; - f_status_t status = f_string_dynamic_increase_by(link_stat.st_size + 1, target); + f_status_t status = f_string_dynamic_increase_by(size + 1, target); if (F_status_is_error(status)) return status; - if (readlink(path.string, target->string, link_stat.st_size) < 0) { - if (errno == EACCES) return F_status_set_error(F_access_denied); - if (errno == EFAULT) return F_status_set_error(F_buffer); - if (errno == EINVAL) return F_status_set_error(F_parameter); - if (errno == EIO) return F_status_set_error(F_input_output); - if (errno == ELOOP) return F_status_set_error(F_loop); - if (errno == ENAMETOOLONG) return F_status_set_error(F_name); - if (errno == ENOENT) return F_status_set_error(F_file_found_not); - if (errno == ENOMEM) return F_status_set_error(F_memory_not); - if (errno == ENOTDIR) return F_status_set_error(F_directory_not); + if (size) { + if (readlink(path.string, target->string, size) < 0) { + if (errno == EACCES) return F_status_set_error(F_access_denied); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == ELOOP) return F_status_set_error(F_loop); + if (errno == ENAMETOOLONG) return F_status_set_error(F_name); + if (errno == ENOENT) return F_status_set_error(F_file_found_not); + if (errno == ENOMEM) return F_status_set_error(F_memory_not); + if (errno == ENOTDIR) return F_status_set_error(F_directory_not); - return F_status_set_error(F_failure); + return F_status_set_error(F_failure); + } } - target->used = link_stat.st_size; + target->used = size; status = f_string_dynamic_terminate_after(target); if (F_status_is_error(status)) return status; @@ -433,29 +435,31 @@ extern "C" { #endif // !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) #if !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) - f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) { + f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) { target->used = 0; - f_status_t status = f_string_dynamic_increase_by(link_stat.st_size + 1, target); + f_status_t status = f_string_dynamic_increase_by(size + 1, target); if (F_status_is_error(status)) return status; - if (readlinkat(at_id, path.string, target->string, link_stat.st_size) < 0) { - if (errno == EACCES) return F_status_set_error(F_access_denied); - if (errno == EBADF) return F_status_set_error(F_directory_descriptor); - if (errno == EFAULT) return F_status_set_error(F_buffer); - if (errno == EINVAL) return F_status_set_error(F_parameter); - if (errno == EIO) return F_status_set_error(F_input_output); - if (errno == ELOOP) return F_status_set_error(F_loop); - if (errno == ENAMETOOLONG) return F_status_set_error(F_name); - if (errno == ENOENT) return F_status_set_error(F_file_found_not); - if (errno == ENOMEM) return F_status_set_error(F_memory_not); - if (errno == ENOTDIR) return F_status_set_error(F_directory_not); + if (size) { + if (readlinkat(at_id, path.string, target->string, size) < 0) { + if (errno == EACCES) return F_status_set_error(F_access_denied); + if (errno == EBADF) return F_status_set_error(F_directory_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == ELOOP) return F_status_set_error(F_loop); + if (errno == ENAMETOOLONG) return F_status_set_error(F_name); + if (errno == ENOENT) return F_status_set_error(F_file_found_not); + if (errno == ENOMEM) return F_status_set_error(F_memory_not); + if (errno == ENOTDIR) return F_status_set_error(F_directory_not); - return F_status_set_error(F_failure); + return F_status_set_error(F_failure); + } } - target->used = link_stat.st_size; + target->used = size; status = f_string_dynamic_terminate_after(target); if (F_status_is_error(status)) return status; diff --git a/level_0/f_file/c/private-file.h b/level_0/f_file/c/private-file.h index 7e2b248..2d798cd 100644 --- a/level_0/f_file/c/private-file.h +++ b/level_0/f_file/c/private-file.h @@ -520,8 +520,8 @@ extern "C" { * * @param path * The path file name. - * @param link_stat - * The link file statistics. + * @param size + * The file size in bytes, such as (struct stat).st_size. * @param target * Will be replaced with the path in which the link points to. * Will be NULL terminated with the NULL at target.string[target.used]; @@ -547,7 +547,7 @@ extern "C" { * @see f_string_dynamic_terminate_after() */ #if !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) - extern f_status_t private_f_file_link_read(const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_link_read(const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_link_read_) || !defined(_di_f_file_copy_) /** @@ -559,8 +559,8 @@ extern "C" { * The parent directory, as an open directory file descriptor, in which path is relative to. * @param path * The path file name. - * @param link_stat - * The link file statistics. + * @param size + * The file size in bytes, such as (struct stat).st_size. * @param target * Will be replaced with the path in which the link points to. * Will be NULL terminated with the NULL at target.string[target.used]; @@ -587,7 +587,7 @@ extern "C" { * @see f_string_dynamic_terminate_after() */ #if !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) - extern f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const struct stat link_stat, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_link_read_at(const int at_id, const f_string_static_t path, const off_t size, f_string_dynamic_t * const target) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_link_read_at_) || !defined(_di_f_file_copy_at_) /** diff --git a/level_0/f_file/tests/unit/c/test-file-copy.c b/level_0/f_file/tests/unit/c/test-file-copy.c index d468617..f8ec4ef 100644 --- a/level_0/f_file/tests/unit/c/test-file-copy.c +++ b/level_0/f_file/tests/unit/c/test-file-copy.c @@ -640,6 +640,7 @@ void test__f_file_copy__fails_for_link(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_link_d; + statistics.st_size = 1; for (int i = 0; i < 10; ++i) { @@ -704,6 +705,7 @@ void test__f_file_copy__fails_for_link(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_link_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -829,6 +831,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 21; ++i) { @@ -869,6 +872,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 6; ++i) { @@ -912,6 +916,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 6; ++i) { @@ -968,6 +973,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 11; ++i) { @@ -1047,6 +1053,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 11; ++i) { @@ -1129,6 +1136,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; for (int i = 0; i < 11; ++i) { @@ -1197,6 +1205,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -1288,6 +1297,7 @@ void test__f_file_copy__fails_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -1391,6 +1401,7 @@ void test__f_file_copy__fails_for_socket(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_socket_d; + statistics.st_size = 1; for (int i = 0; i < 15; ++i) { @@ -1451,6 +1462,7 @@ void test__f_file_copy__fails_for_socket(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_socket_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -1515,6 +1527,7 @@ void test__f_file_copy__works_for_block(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_block_d; + statistics.st_size = 1; will_return(__wrap_stat, false); will_return(__wrap_stat, &statistics); @@ -1543,6 +1556,7 @@ void test__f_file_copy__works_for_character(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_character_d; + statistics.st_size = 1; will_return(__wrap_stat, false); will_return(__wrap_stat, &statistics); @@ -1571,6 +1585,7 @@ void test__f_file_copy__works_for_directory(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_directory_d; + statistics.st_size = 1; will_return(__wrap_stat, false); will_return(__wrap_stat, &statistics); @@ -1599,6 +1614,7 @@ void test__f_file_copy__works_for_fifo(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_fifo_d; + statistics.st_size = 1; will_return(__wrap_stat, false); will_return(__wrap_stat, &statistics); @@ -1627,6 +1643,7 @@ void test__f_file_copy__works_for_link(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_link_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -1660,6 +1677,7 @@ void test__f_file_copy__works_for_regular(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_regular_d; + statistics.st_size = 1; f_char_t buffer[1]; @@ -1727,6 +1745,7 @@ void test__f_file_copy__works_for_socket(void **state) { memset(&statistics, 0, sizeof(struct stat)); statistics.st_mode = 1 | F_file_type_socket_d; + statistics.st_size = 1; will_return(__wrap_stat, false); will_return(__wrap_stat, &statistics); diff --git a/level_0/f_file/tests/unit/c/test-file-link_read.c b/level_0/f_file/tests/unit/c/test-file-link_read.c index 8519a9a..2f4398f 100644 --- a/level_0/f_file/tests/unit/c/test-file-link_read.c +++ b/level_0/f_file/tests/unit/c/test-file-link_read.c @@ -47,10 +47,14 @@ void test__f_file_link_read__fails(void **state) { buffer.used = 0; + will_return(__wrap_stat, false); + will_return(__wrap_stat, &statistics); + will_return(__wrap_stat, 0); + will_return(__wrap_readlink, true); will_return(__wrap_readlink, errnos[i]); - const f_status_t status = f_file_link_read(path, statistics, &buffer); + const f_status_t status = f_file_link_read(path, F_true, &buffer); assert_int_equal(F_status_set_fine(status), statuss[i]); } // for @@ -68,7 +72,7 @@ void test__f_file_link_read__fails(void **state) { f_string_dynamic_t buffer = f_string_dynamic_t_initialize; { - const f_status_t status = f_file_link_read(f_string_empty_s, statistics, 0); + const f_status_t status = f_file_link_read(f_string_empty_s, F_true, 0); assert_int_equal(F_status_set_fine(status), F_parameter); } @@ -90,7 +94,7 @@ void test__f_file_link_read__returns_data_not(void **state) { f_string_dynamic_t buffer = f_string_dynamic_t_initialize; { - const f_status_t status = f_file_link_read(f_string_empty_s, statistics, &buffer); + const f_status_t status = f_file_link_read(f_string_empty_s, F_true, &buffer); assert_int_equal(status, F_data_not); } @@ -113,11 +117,15 @@ void test__f_file_link_read__works(void **state) { char source[2] = { 'x', 0 }; { + will_return(__wrap_stat, false); + will_return(__wrap_stat, &statistics); + will_return(__wrap_stat, 0); + will_return(__wrap_readlink, false); will_return(__wrap_readlink, source); will_return(__wrap_readlink, 0); - const f_status_t status = f_file_link_read(path, statistics, &buffer); + const f_status_t status = f_file_link_read(path, F_true, &buffer); assert_int_equal(status, F_none); } diff --git a/level_0/f_file/tests/unit/c/test-file-link_read_at.c b/level_0/f_file/tests/unit/c/test-file-link_read_at.c index 253ab4c..7e0a064 100644 --- a/level_0/f_file/tests/unit/c/test-file-link_read_at.c +++ b/level_0/f_file/tests/unit/c/test-file-link_read_at.c @@ -47,10 +47,14 @@ void test__f_file_link_read_at__fails(void **state) { for (int i = 0; i < 11; ++i) { + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); + will_return(__wrap_readlinkat, true); will_return(__wrap_readlinkat, errnos[i]); - const f_status_t status = f_file_link_read_at(0, path, statistics, &buffer); + const f_status_t status = f_file_link_read_at(0, path, 0, &buffer); assert_int_equal(F_status_set_fine(status), statuss[i]); } // for @@ -61,14 +65,10 @@ void test__f_file_link_read_at__fails(void **state) { #ifndef _di_level_0_parameter_checking_ void test__f_file_link_read_at__parameter_checking(void **state) { - struct stat statistics; - - memset(&statistics, 0, sizeof(struct stat)); - f_string_dynamic_t buffer = f_string_dynamic_t_initialize; { - const f_status_t status = f_file_link_read_at(0, f_string_empty_s, statistics, 0); + const f_status_t status = f_file_link_read_at(0, f_string_empty_s, 0, 0); assert_int_equal(F_status_set_fine(status), F_parameter); } @@ -81,16 +81,10 @@ void test__f_file_link_read_at__returns_data_not(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); - struct stat statistics; - - memset(&statistics, 0, sizeof(struct stat)); - - statistics.st_size = 1; - f_string_dynamic_t buffer = f_string_dynamic_t_initialize; { - const f_status_t status = f_file_link_read_at(0, f_string_empty_s, statistics, &buffer); + const f_status_t status = f_file_link_read_at(0, f_string_empty_s, 0, &buffer); assert_int_equal(status, F_data_not); } @@ -113,11 +107,15 @@ void test__f_file_link_read_at__works(void **state) { char source[2] = { 'x', 0 }; { + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); + will_return(__wrap_readlinkat, false); will_return(__wrap_readlinkat, source); will_return(__wrap_readlinkat, 0); - const f_status_t status = f_file_link_read_at(0, path, statistics, &buffer); + const f_status_t status = f_file_link_read_at(0, path, 0, &buffer); assert_int_equal(status, F_none); } diff --git a/level_0/f_file/tests/unit/c/test-file-mode_read.c b/level_0/f_file/tests/unit/c/test-file-mode_read.c index 3c87189..4cbc416 100644 --- a/level_0/f_file/tests/unit/c/test-file-mode_read.c +++ b/level_0/f_file/tests/unit/c/test-file-mode_read.c @@ -7,22 +7,42 @@ extern "C" { void test__f_file_mode_read__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + int errnos[] = { + EACCES, + EFAULT, + ELOOP, + ENAMETOOLONG, + ENOENT, + ENOMEM, + ENOTDIR, + EOVERFLOW, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_access_denied, + F_buffer, + F_loop, + F_name, + F_file_found_not, + F_memory_not, + F_directory_not, + F_number_overflow, + F_file_stat, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 9; ++i) { + + mode_t mode = 0; - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_lstat, true); + will_return(__wrap_lstat, errnos[i]); - //const f_status_t status = f_file_mode_read(path, F_false, &id); + const f_status_t status = f_file_mode_read(path, F_false, &mode); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -50,14 +70,40 @@ void test__f_file_mode_read__returns_data_not(void **state) { void test__f_file_mode_read__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + + struct stat statistics; + + memset(&statistics, 0, sizeof(struct stat)); + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + mode_t mode = 0; + + statistics.st_mode = 1 | F_file_type_link_d; + + will_return(__wrap_lstat, false); + will_return(__wrap_lstat, &statistics); + will_return(__wrap_lstat, 0); + + const f_status_t status = f_file_mode_read(path, F_false, &mode); + + assert_int_equal(status, F_none); + assert_int_equal(mode, statistics.st_mode); + } + + { + mode_t mode = 0; + + statistics.st_mode = 1 | F_file_type_regular_d; + + will_return(__wrap_stat, false); + will_return(__wrap_stat, &statistics); + will_return(__wrap_stat, 0); - //const f_status_t status = f_file_mode_read(); + const f_status_t status = f_file_mode_read(path, F_true, &mode); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none); + assert_int_equal(mode, statistics.st_mode); } } diff --git a/level_0/f_file/tests/unit/c/test-file-mode_read_at.c b/level_0/f_file/tests/unit/c/test-file-mode_read_at.c index 1038248..06a5530 100644 --- a/level_0/f_file/tests/unit/c/test-file-mode_read_at.c +++ b/level_0/f_file/tests/unit/c/test-file-mode_read_at.c @@ -1,5 +1,5 @@ #include "test-file.h" -#include "test-file-mode_read_at.h" +#include "test-file-exists_at.h" #ifdef __cplusplus extern "C" { @@ -7,22 +7,44 @@ extern "C" { void test__f_file_mode_read_at__fails(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + int errnos[] = { + EACCES, + EBADF, + EFAULT, + ELOOP, + ENAMETOOLONG, + ENOENT, + ENOMEM, + ENOTDIR, + EOVERFLOW, mock_errno_generic, }; f_status_t statuss[] = { - F_failure, + F_access_denied, + F_directory_descriptor, + F_buffer, + F_loop, + F_name, + F_file_found_not, + F_memory_not, + F_directory_not, + F_number_overflow, + F_file_stat, }; - for (int i = 0; i < 1; ++i) { + for (int i = 0; i < 10; ++i) { + + mode_t mode = 0; - //will_return(__wrap_open, true); - //will_return(__wrap_open, errnos[i]); + will_return(__wrap_fstatat, true); + will_return(__wrap_fstatat, errnos[i]); - //const f_status_t status = f_file_mode_read_at(path, F_false, &id); + const f_status_t status = f_file_mode_read_at(0, path, 0, &mode); - //assert_int_equal(F_status_set_fine(status), statuss[i]); + assert_int_equal(F_status_set_fine(status), statuss[i]); } // for } @@ -30,7 +52,7 @@ void test__f_file_mode_read_at__fails(void **state) { void test__f_file_mode_read_at__parameter_checking(void **state) { { - const f_status_t status = f_file_mode_read_at(0, f_string_empty_s, 0); + const f_status_t status = f_file_mode_read_at(0, f_string_empty_s, 0, 0); assert_int_equal(F_status_set_fine(status), F_parameter); } @@ -42,7 +64,7 @@ void test__f_file_mode_read_at__returns_data_not(void **state) { { mode_t mode = 0; - const f_status_t status = f_file_mode_read_at(0, f_string_empty_s, &mode); + const f_status_t status = f_file_mode_read_at(0, f_string_empty_s, 0, &mode); assert_int_equal(status, F_data_not); } @@ -50,14 +72,25 @@ void test__f_file_mode_read_at__returns_data_not(void **state) { void test__f_file_mode_read_at__works(void **state) { + const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); + + struct stat statistics; + + memset(&statistics, 0, sizeof(struct stat)); + + statistics.st_mode = 1 | F_file_type_regular_d; + { - //will_return(__wrap_open, false); - //will_return(__wrap_open, 5); + mode_t mode = 0; + + will_return(__wrap_fstatat, false); + will_return(__wrap_fstatat, &statistics); + will_return(__wrap_fstatat, 0); - //const f_status_t status = f_file_mode_read_at(); + const f_status_t status = f_file_mode_read_at(0, path, 0, &mode); - //assert_int_equal(status, F_none); - //assert_int_equal(id, 5); + assert_int_equal(status, F_none); + assert_int_equal(mode, statistics.st_mode); } } diff --git a/level_0/f_file/tests/unit/c/test-file.c b/level_0/f_file/tests/unit/c/test-file.c index d3b4961..2e15f6b 100644 --- a/level_0/f_file/tests/unit/c/test-file.c +++ b/level_0/f_file/tests/unit/c/test-file.c @@ -156,9 +156,13 @@ int main(void) { cmocka_unit_test(test__f_file_mode_from_string__works_basic_alphabet), cmocka_unit_test(test__f_file_mode_from_string__works_basic_digit), - // f_file_mode_read + cmocka_unit_test(test__f_file_mode_read__fails), + cmocka_unit_test(test__f_file_mode_read__returns_data_not), + cmocka_unit_test(test__f_file_mode_read__works), - // f_file_mode_read_at + cmocka_unit_test(test__f_file_mode_read_at__fails), + cmocka_unit_test(test__f_file_mode_read_at__returns_data_not), + cmocka_unit_test(test__f_file_mode_read_at__works), // f_file_mode_set