aboutsummaryrefslogtreecommitdiff
path: root/src/list_management.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/list_management.c')
-rw-r--r--src/list_management.c83
1 files changed, 82 insertions, 1 deletions
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;
+}
+