aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-06-21 16:39:59 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-06-21 16:39:59 +0200
commit744eb2a2a571fa21a5524812b60ca8ebf9a65fe8 (patch)
treefde901cff5ecb54c01cfe972bc0ee360548ca1d9 /src
parent548c43810fc07ea220b01c02baa4facff7a1d3ed (diff)
downloaddir_monitor-744eb2a2a571fa21a5524812b60ca8ebf9a65fe8.tar.gz
dir_monitor-744eb2a2a571fa21a5524812b60ca8ebf9a65fe8.tar.bz2
Move data collection to other source
Diffstat (limited to 'src')
-rw-r--r--src/dir_monitor.c92
-rw-r--r--src/list_management.c83
2 files changed, 82 insertions, 93 deletions
diff --git a/src/dir_monitor.c b/src/dir_monitor.c
index 722f6d1..86f2361 100644
--- a/src/dir_monitor.c
+++ b/src/dir_monitor.c
@@ -7,12 +7,7 @@
*
* vim:ts=4:sw=4:expandtab
*/
-#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <dirent.h>
-#include <sys/stat.h>
#include "output.h"
#include "list_management.h"
@@ -20,93 +15,6 @@
-/* === DEFINITIONS === */
-
-struct list_head *get_data_from_directory(char *path);
-
-
-/* === IMPLEMENTATION === */
-
-/* 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;
- size_t l;
- struct dirent *de = NULL;
- struct list_node *tmp = NULL;
- struct list_head *list = NULL;
- struct stat stat_res;
-
- if (path == NULL) {
- LOGERR("ERROR: No path given.\n");
- return NULL;
- }
-
- l = strlen(path);
- /* 258 filename and possibly required PATH_SEP and of course string termination */
- if ((fullpath = calloc(l+258, sizeof(char))) == NULL) {
- LOGERR("ERROR: Failed to allocate memory for list head.\n");
- return NULL;
- }
- sprintf(fullpath, "%s%c", path, ((path[l-1] == PATH_SEP) ? '\0' : PATH_SEP));
- fname_in_path = (fullpath[l] == PATH_SEP) ? &(fullpath[l+1]) : &(fullpath[l]) ;
-
- if ((dir = opendir(path)) == NULL) {
- LOGERR("ERROR: Failed to open directory '%s': %s (errno %d)\n",
- path, strerror(errno), errno);
- return NULL;
- }
-
- /* TBH: In case those few bytes are not available, the process will be killed anyways */
- if ((list = calloc(1, sizeof(struct list_head))) == NULL) {
- LOGERR("ERROR: Failed to allocate memory for list head.\n");
- closedir(dir);
- return NULL;
- }
-
- errno = 0;
- /* 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;
-
- sprintf(fname_in_path, "%s", de->d_name);
- DBGTRC("DEBUG: fullpath: '%s'\n", fullpath);
- if (lstat(fullpath, &stat_res) != 0) {
- LOGERR("ERROR: lstat call on '%s' failed: %s (errno %d)\n",
- fullpath, strerror(errno), errno);
- continue;
- }
- if ((tmp = create_node(de->d_name, &stat_res)) == 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);
- break;
- case SORT_BY_TIME:
- insert_sorted_by_time(list, tmp);
- break;
- };
- }
-
- free(fullpath);
- closedir(dir);
- return list;
-}
-
-
int main(int argc, char **argv) {
struct list_head *list = NULL;
int path_index = 1;
diff --git a/src/list_management.c b/src/list_management.c
index 582eccd..d1a93d3 100644
--- a/src/list_management.c
+++ b/src/list_management.c
@@ -8,6 +8,8 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <dirent.h>
+#include <sys/stat.h>
#include "output.h"
#include "list_management.h"
@@ -119,7 +121,7 @@ inline int insert_sorted_by_time(struct list_head *list, struct list_node *node)
}
-struct list_head *create_list_sort_reversed(struct list_head *list) {
+inline struct list_head *create_list_sort_reversed(struct list_head *list) {
struct list_head *lh = NULL;
struct list_node *cpynode = NULL, *ptr = NULL;
if (list == NULL) {
@@ -148,3 +150,82 @@ struct list_head *create_list_sort_reversed(struct list_head *list) {
return lh;
}
+/* 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;
+ size_t l;
+ struct dirent *de = NULL;
+ struct list_node *tmp = NULL;
+ struct list_head *list = NULL;
+ struct stat stat_res;
+
+ if (path == NULL) {
+ LOGERR("ERROR: No path given.\n");
+ return NULL;
+ }
+
+ l = strlen(path);
+ /* 258 filename and possibly required PATH_SEP and of course string termination */
+ if ((fullpath = calloc(l+258, sizeof(char))) == NULL) {
+ LOGERR("ERROR: Failed to allocate memory for list head.\n");
+ return NULL;
+ }
+ sprintf(fullpath, "%s%c", path, ((path[l-1] == PATH_SEP) ? '\0' : PATH_SEP));
+ fname_in_path = (fullpath[l] == PATH_SEP) ? &(fullpath[l+1]) : &(fullpath[l]) ;
+
+ if ((dir = opendir(path)) == NULL) {
+ LOGERR("ERROR: Failed to open directory '%s': %s (errno %d)\n",
+ path, strerror(errno), errno);
+ return NULL;
+ }
+
+ /* TBH: In case those few bytes are not available, the process will be killed anyways */
+ if ((list = calloc(1, sizeof(struct list_head))) == NULL) {
+ LOGERR("ERROR: Failed to allocate memory for list head.\n");
+ closedir(dir);
+ return NULL;
+ }
+
+ errno = 0;
+ /* 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;
+
+ sprintf(fname_in_path, "%s", de->d_name);
+ DBGTRC("DEBUG: fullpath: '%s'\n", fullpath);
+ if (lstat(fullpath, &stat_res) != 0) {
+ LOGERR("ERROR: lstat call on '%s' failed: %s (errno %d)\n",
+ fullpath, strerror(errno), errno);
+ continue;
+ }
+ if ((tmp = create_node(de->d_name, &stat_res)) == 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);
+ break;
+ case SORT_BY_TIME:
+ insert_sorted_by_time(list, tmp);
+ break;
+ };
+ }
+
+ free(fullpath);
+ closedir(dir);
+ return list;
+}
+