From 4f6d8ea12e9f1bf5f66af7a05699e53527dcd304 Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Sun, 15 Jun 2025 02:36:20 +0200 Subject: Improved CMakelists.txt and minor fixes in dir_monitor.c --- src/dir_monitor.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'src/dir_monitor.c') 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 #include #include +#include #include #include #include @@ -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; } } -- cgit v1.2.3-70-g09d2