aboutsummaryrefslogtreecommitdiff
path: root/src/dir_monitor.c
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-06-15 02:36:20 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-06-15 02:36:20 +0200
commit4f6d8ea12e9f1bf5f66af7a05699e53527dcd304 (patch)
tree2268b3483eb435c79438b2f39f26161147c17c6a /src/dir_monitor.c
parenta8d2c8044dbdd7cffa475d894843e32747a30eee (diff)
downloaddir_monitor-4f6d8ea12e9f1bf5f66af7a05699e53527dcd304.tar.gz
dir_monitor-4f6d8ea12e9f1bf5f66af7a05699e53527dcd304.tar.bz2
Improved CMakelists.txt and minor fixes in dir_monitor.c
Diffstat (limited to 'src/dir_monitor.c')
-rw-r--r--src/dir_monitor.c36
1 files changed, 26 insertions, 10 deletions
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;
}
}