]> Kevux Git Server - fll/commitdiff
Feature: Add f_file_is_stat() to perform f_file_is() using a pre-populated file stati...
authorKevin Day <thekevinday@gmail.com>
Tue, 3 Jan 2023 20:54:06 +0000 (14:54 -0600)
committerKevin Day <thekevinday@gmail.com>
Tue, 3 Jan 2023 20:54:06 +0000 (14:54 -0600)
level_0/f_file/c/file.c
level_0/f_file/c/file.h
level_0/f_file/data/build/settings-tests
level_0/f_file/tests/unit/c/test-file-is_stat.c [new file with mode: 0644]
level_0/f_file/tests/unit/c/test-file-is_stat.h [new file with mode: 0644]
level_0/f_file/tests/unit/c/test-file.c
level_0/f_file/tests/unit/c/test-file.h

index ec8a5568e66410e3ba531a4612d2ab2fdf2eadb7..53bf5cbbb5663c240e2696b9097aa9894bdc6543 100644 (file)
@@ -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) {
 
index 63a46e50a1dc582d4d3fbdb0091465f116e1a48e..40442fab227c4bed1d4033c8042a5dfc2c568e69 100644 (file)
@@ -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.
index 24d8ea55e0d4c3810beadf2d64c45abb486e912b..5d8d7331ea6664a533d5a0efdce4d93baa902866 100644 (file)
@@ -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 (file)
index 0000000..0cac49b
--- /dev/null
@@ -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 (file)
index 0000000..6aa8eed
--- /dev/null
@@ -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
index d540b9307c384bef974b48b78bb7acb949afac1f..a5b22ec4fee2eed334465f0bad35a27ca8c89fdb 100644 (file)
@@ -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.
index 13497ebeae2fefc3f9ea871a8a66fb8cbb2bdfea..99f4062d8f52cf1665b79fcd5e6b35fba553ddd1 100644 (file)
@@ -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"