From d2537e69790b93a535edee152ec9626321e555c5 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 3 Jan 2023 14:54:06 -0600 Subject: [PATCH] Feature: Add f_file_is_stat() to perform f_file_is() using a pre-populated file statistics structure. --- level_0/f_file/c/file.c | 9 +++ level_0/f_file/c/file.h | 19 +++++++ level_0/f_file/data/build/settings-tests | 2 +- level_0/f_file/tests/unit/c/test-file-is_stat.c | 76 +++++++++++++++++++++++++ level_0/f_file/tests/unit/c/test-file-is_stat.h | 34 +++++++++++ level_0/f_file/tests/unit/c/test-file.c | 5 ++ level_0/f_file/tests/unit/c/test-file.h | 1 + 7 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 level_0/f_file/tests/unit/c/test-file-is_stat.c create mode 100644 level_0/f_file/tests/unit/c/test-file-is_stat.h diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index ec8a556..53bf5cb 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -480,6 +480,15 @@ extern "C" { } #endif // _di_f_file_is_at_ +#ifndef _di_f_file_is_stat_ + f_status_t f_file_is_stat(const struct stat stat_file, const int type) { + + if (macro_f_file_type_get(stat_file.st_mode) == type) return F_true; + + return F_false; + } +#endif // _di_f_file_is_stat_ + #ifndef _di_f_file_link_ f_status_t f_file_link(const f_string_static_t target, const f_string_static_t point) { diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 63a46e5..40442fa 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -772,6 +772,25 @@ extern "C" { #endif // _di_f_file_is_at_ /** + * Identify whether or not a file exists at the given path and if that file is a specific type using the statistics file data. + * + * This does not require access on the file itself. + * This only requires a populated statistics file structure. + * + * @param stat_file + * The statistics read. + * @param type + * The type of the file. + * + * @return + * F_true if path was found and path is type. + * F_false if path was found and path is not type. + */ +#ifndef _di_f_file_is_stat_ + extern f_status_t f_file_is_stat(const struct stat stat_file, const int type); +#endif // _di_f_file_is_stat_ + +/** * Create a symbolic link to a file. * * This will not replace existing files/links. diff --git a/level_0/f_file/data/build/settings-tests b/level_0/f_file/data/build/settings-tests index 24d8ea5..5d8d733 100644 --- a/level_0/f_file/data/build/settings-tests +++ b/level_0/f_file/data/build/settings-tests @@ -25,7 +25,7 @@ build_language c build_libraries -lc -lcmocka build_libraries-individual -lf_memory -lf_string -lf_file -build_sources_program test-file-access.c test-file-access_at.c test-file-clone.c test-file-close.c test-file-close_flush.c test-file-copy.c test-file-create.c test-file-create_at.c test-file-create_device.c test-file-create_device_at.c test-file-create_fifo.c test-file-create_fifo_at.c test-file-create_node.c test-file-create_node_at.c test-file-descriptor.c test-file-exists.c test-file-exists_at.c test-file-flush.c test-file-group_read.c test-file-is.c test-file-is_at.c test-file-link.c test-file-link_at.c test-file-link_hard.c test-file-link_hard_at.c test-file-link_read.c test-file-link_read_at.c test-file-mode_determine.c test-file-mode_from_string.c test-file-mode_read.c test-file-mode_read_at.c test-file-mode_set.c test-file-mode_set_at.c test-file-mode_to_mode.c test-file-name_base.c test-file-name_directory.c test-file-open.c test-file-open_at.c test-file-owner_read.c test-file-read.c test-file-read_block.c test-file-read_until.c test-file-remove.c test-file-remove_at.c test-file-rename.c test-file-rename_at.c test-file-role_change.c test-file-role_change_at.c test-file-seek.c test-file-size.c test-file-size_at.c test-file-size_by_id.c test-file-stat.c test-file-stat_at.c test-file-stat_by_id.c test-file-stream_close.c test-file-stream_open_descriptor.c test-file-stream_open.c test-file-stream_read.c test-file-stream_read_block.c test-file-stream_read_until.c test-file-stream_reopen.c test-file-stream_write.c test-file-stream_write_block.c test-file-stream_write_until.c test-file-stream_write_range.c test-file-touch.c test-file-touch_at.c test-file-type.c test-file-type_at.c test-file-umask_get.c test-file-umask_set.c test-file-write.c test-file-write_block.c test-file-write_until.c test-file-write_range.c +build_sources_program test-file-access.c test-file-access_at.c test-file-clone.c test-file-close.c test-file-close_flush.c test-file-copy.c test-file-create.c test-file-create_at.c test-file-create_device.c test-file-create_device_at.c test-file-create_fifo.c test-file-create_fifo_at.c test-file-create_node.c test-file-create_node_at.c test-file-descriptor.c test-file-exists.c test-file-exists_at.c test-file-flush.c test-file-group_read.c test-file-is.c test-file-is_at.c test-file-is_stat.c test-file-link.c test-file-link_at.c test-file-link_hard.c test-file-link_hard_at.c test-file-link_read.c test-file-link_read_at.c test-file-mode_determine.c test-file-mode_from_string.c test-file-mode_read.c test-file-mode_read_at.c test-file-mode_set.c test-file-mode_set_at.c test-file-mode_to_mode.c test-file-name_base.c test-file-name_directory.c test-file-open.c test-file-open_at.c test-file-owner_read.c test-file-read.c test-file-read_block.c test-file-read_until.c test-file-remove.c test-file-remove_at.c test-file-rename.c test-file-rename_at.c test-file-role_change.c test-file-role_change_at.c test-file-seek.c test-file-size.c test-file-size_at.c test-file-size_by_id.c test-file-stat.c test-file-stat_at.c test-file-stat_by_id.c test-file-stream_close.c test-file-stream_open_descriptor.c test-file-stream_open.c test-file-stream_read.c test-file-stream_read_block.c test-file-stream_read_until.c test-file-stream_reopen.c test-file-stream_write.c test-file-stream_write_block.c test-file-stream_write_until.c test-file-stream_write_range.c test-file-touch.c test-file-touch_at.c test-file-type.c test-file-type_at.c test-file-umask_get.c test-file-umask_set.c test-file-write.c test-file-write_block.c test-file-write_until.c test-file-write_range.c build_sources_program test-file.c build_script no diff --git a/level_0/f_file/tests/unit/c/test-file-is_stat.c b/level_0/f_file/tests/unit/c/test-file-is_stat.c new file mode 100644 index 0000000..0cac49b --- /dev/null +++ b/level_0/f_file/tests/unit/c/test-file-is_stat.c @@ -0,0 +1,76 @@ +#include "test-file.h" +#include "test-file-is.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_file_is_stat__returns_false(void **state) { + + f_status_t types[] = { + F_file_type_block_d, + F_file_type_character_d, + F_file_type_directory_d, + F_file_type_fifo_d, + F_file_type_link_d, + F_file_type_mask_d, + F_file_type_regular_d, + F_file_type_socket_d, + }; + + for (int j = 0; j < 8; ++j) { + + struct stat statistics; + + memset(&statistics, 0, sizeof(struct stat)); + + statistics.st_mode = 1 | types[j]; + + for (int i = 0; i < 8; ++i) { + + // Skip what would return true. + if (j == i) continue; + + const f_status_t status = f_file_is_stat(statistics, types[i]); + + assert_int_equal(status, F_false); + } // for + } // for +} + +void test__f_file_is_stat__returns_true(void **state) { + + f_status_t types[] = { + F_file_type_block_d, + F_file_type_character_d, + F_file_type_directory_d, + F_file_type_fifo_d, + F_file_type_link_d, + F_file_type_mask_d, + F_file_type_regular_d, + F_file_type_socket_d, + }; + + for (int j = 0; j < 8; ++j) { + + struct stat statistics; + + memset(&statistics, 0, sizeof(struct stat)); + + statistics.st_mode = 1 | types[j]; + + for (int i = 0; i < 8; ++i) { + + // Skip what would return false. + if (j != i) continue; + + const f_status_t status = f_file_is_stat(statistics, types[i]); + + assert_int_equal(status, F_true); + } // for + } // for +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_file/tests/unit/c/test-file-is_stat.h b/level_0/f_file/tests/unit/c/test-file-is_stat.h new file mode 100644 index 0000000..6aa8eed --- /dev/null +++ b/level_0/f_file/tests/unit/c/test-file-is_stat.h @@ -0,0 +1,34 @@ +/** + * FLL - Level 0 + * + * Project: File + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Test the file project. + */ +#ifndef _TEST__F_file_is_stat_h +#define _TEST__F_file_is_stat_h + +/** + * Test that function fails. + * + * @see f_file_is_stat() + */ +extern void test__f_file_is_stat__fails(void **state); + +/** + * Test that function works and returns F_false. + * + * @see f_file_is_stat() + */ +extern void test__f_file_is_stat__returns_false(void **state); + +/** + * Test that function works and returns F_true. + * + * @see f_file_is_stat() + */ +extern void test__f_file_is_stat__returns_true(void **state); + +#endif // _TEST__F_file_is_stat_h 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 d540b93..a5b22ec 100644 --- a/level_0/f_file/tests/unit/c/test-file.c +++ b/level_0/f_file/tests/unit/c/test-file.c @@ -124,6 +124,10 @@ int main(void) { cmocka_unit_test(test__f_file_is_at__returns_false), cmocka_unit_test(test__f_file_is_at__returns_true), + cmocka_unit_test(test__f_file_is_stat__fails), + cmocka_unit_test(test__f_file_is_stat__returns_false), + cmocka_unit_test(test__f_file_is_stat__returns_true), + cmocka_unit_test(test__f_file_link__fails), cmocka_unit_test(test__f_file_link__returns_data_not), cmocka_unit_test(test__f_file_link__works), @@ -369,6 +373,7 @@ int main(void) { cmocka_unit_test(test__f_file_group_read__parameter_checking), // f_file_is() doesn't use parameter checking. // f_file_is_at() doesn't use parameter checking. + // f_file_is_stat() doesn't use parameter checking. // f_file_link() doesn't use parameter checking. // f_file_link_at() doesn't use parameter checking. // f_file_link_hard() doesn't use parameter checking. diff --git a/level_0/f_file/tests/unit/c/test-file.h b/level_0/f_file/tests/unit/c/test-file.h index 13497eb..99f4062 100644 --- a/level_0/f_file/tests/unit/c/test-file.h +++ b/level_0/f_file/tests/unit/c/test-file.h @@ -47,6 +47,7 @@ #include "test-file-group_read.h" #include "test-file-is.h" #include "test-file-is_at.h" +#include "test-file-is_stat.h" #include "test-file-link.h" #include "test-file-link_at.h" #include "test-file-link_hard.h" -- 1.8.3.1