aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt21
-rw-r--r--src/dir_monitor.c36
2 files changed, 35 insertions, 22 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0fdbe0c..0e9238a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,27 +4,24 @@ project(dir_monitor
LANGUAGES C
)
-set(SOURCE_DM src/dir_monitor.c)
+# Although C11 would be sufficient, be safe to set this minimum in order to
+# prevent errors from unbeknown features, used to and assuming to be there longer...
+set (CMAKE_C_STANDARD 17)
-add_executable(dir_monitor ${SOURCE_DM})
+add_compile_options(-Wall)
+
+set(SOURCE_DM src/dir_monitor.c)
add_executable(dir_monitor_debug ${SOURCE_DM})
-target_compile_options(dir_monitor_debug PUBLIC -g -DDEBUGBUILD)
+target_compile_options(dir_monitor_debug PUBLIC -g -DDEBUGBUILD -Werror)
add_executable(dir_monitor_debug_asan ${SOURCE_DM})
-target_compile_options(dir_monitor_debug_asan PUBLIC -g -DDEBUGBUILD -fsanitize=address)
+target_compile_options(dir_monitor_debug_asan PUBLIC -g -DDEBUGBUILD -fsanitize=address -Werror)
target_link_libraries(dir_monitor_debug_asan asan)
add_executable(dir_monitor_asan ${SOURCE_DM})
target_compile_options(dir_monitor_asan PUBLIC -fsanitize=address)
target_link_libraries(dir_monitor_asan asan)
+add_executable(dir_monitor ${SOURCE_DM})
-# bin/dir_monitor: bin $(source)
-# gcc -o $@ $(source) -O2
-# bin/dir_monitor_debug: bin $(source)
-# gcc -o $@ $(source) -g -DDEBUGBUILD
-# bin/dir_monitor_debug_asan: bin $(source)
-# gcc -o $@ $(source) -g -DDEBUGBUILD -fsanitize=address
-# bin/dir_monitor_asan: bin $(source)
-# gcc -o $@ $(source) -g -fsanitize=address
diff --git a/src/dir_monitor.c b/src/dir_monitor.c
index 8d70262..5323ce8 100644
--- a/src/dir_monitor.c
+++ b/src/dir_monitor.c
@@ -14,6 +14,7 @@
#include <ctype.h>
#include <getopt.h>
#include <stdint.h>
+#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
@@ -35,7 +36,7 @@
/* === DEFINITIONS === */
#define PATH_SEP '/'
-enum esort_type { SORT_BY_SIZE, SORT_BY_TIME, SORT_UNDEFINED };
+enum esort_type { SORT_BY_SIZE, SORT_BY_TIME };
struct list_node {
struct list_node *next;
size_t fsize;
@@ -143,7 +144,7 @@ inline int fputc_width_x(char c, size_t x, FILE *fdout) {
return 1;
}
-
+/* dirty... */
#define INSERT_BY_NUMERIC_FIELD(list, node, field) \
{ struct list_node *ptr = NULL; \
if (node->field > list->first->field) { \
@@ -154,7 +155,9 @@ inline int fputc_width_x(char c, size_t x, FILE *fdout) {
if (node->field > ptr->next->field) { \
node->next = ptr->next; ptr->next = node; \
return 0; } \
- ptr = ptr->next; } }
+ ptr = ptr->next; \
+ } \
+}
/* TODO: later when, other parameters are actively used move the action to a template */
@@ -170,7 +173,7 @@ inline int insert_sorted_by_size(struct list_head *list, struct list_node *node)
return -3;
}
-int insert_sorted_by_time(struct list_head *list, struct list_node *node) {
+inline int insert_sorted_by_time(struct list_head *list, struct list_node *node) {
if (list == NULL) { LOGERR("ERROR: No list given.\n"); return -1; }
if (node == NULL) { LOGERR("ERROR: No node given.\n"); return -2; }
@@ -179,6 +182,7 @@ int insert_sorted_by_time(struct list_head *list, struct list_node *node) {
return 0;
}
INSERT_BY_NUMERIC_FIELD(list, node, ftime);
+ return -3;
}
#undef INSERT_BY_NUMERIC_FIELD
@@ -217,7 +221,7 @@ void print_list(struct list_head *list) {
ptr = ptr->next;
}
fputc_all_cols('=', stdout);
- printf("\nTotal size: %llu %s\n", ((total_size>1024) ? total_size/1024 : total_size),
+ printf("\nTotal size: %lu %s\n", ((total_size>1024) ? total_size/1024 : total_size),
((total_size >= 1024) ? "kB" : ""));
if (lh != list) destroy_list(lh);
@@ -255,7 +259,16 @@ struct list_head *create_list_sort_reversed(struct list_head *list) {
}
-/* TODO: When sorting via different parameters is possible, the call needs adjustments */
+/* This function contains the only time critical code. The loop over
+ * the directory content.
+ *
+ * Note for future:
+ * I myself use the dir_monitor in combination with watch on a directory
+ * containing temporary data, it may happen that a run into an error occurs,
+ * but it's not tragic. To reduce the chance of the error the first step
+ * would be to optimize the loop with putting the data into a list in stack
+ * form and moving the nodes into a sorted list afterwards.
+ */
struct list_head *get_data_from_directory(char *path) {
char *fullpath = NULL, *fname_in_path = NULL;
DIR *dir = NULL;
@@ -293,7 +306,7 @@ struct list_head *get_data_from_directory(char *path) {
}
errno = 0;
- /* Show hidden entries code is UNIXoid specific, needs to be adjusted for portability */
+ /* Currently the code is specific UNIXoid, needs to be adjusted in case someone ports it. */
while ((de = readdir(dir)) != NULL) {
if (de->d_name[0] == '.' && option_show_hidden_entries == false)
continue;
@@ -305,8 +318,11 @@ struct list_head *get_data_from_directory(char *path) {
fullpath, strerror(errno), errno);
continue;
}
- /* currently ignoring return values */
- tmp = create_node(de->d_name, stat_res.st_size, stat_res.st_mtime);
+ if ((tmp = create_node(de->d_name, stat_res.st_size, stat_res.st_mtime)) == NULL) {
+ LOGERR("ERROR: Skipping entry %s\n", de->d_name);
+ continue;
+ }
+ /* SORT_BY_SIZE is the default value, always check it first. */
switch (option_sort_type) {
case SORT_BY_SIZE:
insert_sorted_by_size(list, tmp);
@@ -393,7 +409,7 @@ int parse_arguments(int argc, char **argv) {
case '?':
break;
default:
- LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c);
+ LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c);
break;
}
}