Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_files.c
Go to the documentation of this file.
1
8#include "nilorea/n_files.h"
9
14void n_free_file_info(void* ptr) {
15 __n_assert(ptr, return);
16 N_FILE_INFO* file = (N_FILE_INFO*)ptr;
17 if (file->name)
18 free(file->name);
19 free(file);
20 return;
21}
22
29static int n_comp_file_info(const void* a, const void* b) {
30 N_FILE_INFO *f1, *f2;
31
32 f1 = (N_FILE_INFO*)a;
33 f2 = (N_FILE_INFO*)b;
34
35 int ret = -1;
36
37 if (!f1) {
38 return -1;
39 }
40 if (!f2) {
41 return 1;
42 }
43
44 if (f1->filetime < f2->filetime) {
45 ret = -1;
46 } else if (f1->filetime == f2->filetime) {
47 if (f1->filetime_nsec < f2->filetime_nsec) {
48 ret = -1;
49 } else if (f1->filetime_nsec > f2->filetime_nsec) {
50 ret = 1;
51 }
52 } else if (f1->filetime > f2->filetime) {
53 ret = 1;
54 }
55
56 return ret;
57}
58
69int n_scan_dir(const char* dir, LIST* result, const int recurse) {
70 DIR* dp = NULL;
71 struct dirent* entry = NULL;
72 struct stat statbuf;
73
74 __n_assert(dir, return FALSE);
75 __n_assert(result, return FALSE);
76
77 dp = opendir(dir);
78 int error = errno;
79 if (dp == NULL) {
80 n_log(LOG_ERR, "cannot open directory: %s, %s", dir, strerror(error));
81 return FALSE;
82 }
83 if (chdir(dir) == -1) {
84 error = errno;
85 n_log(LOG_ERR, "cannot chdir to directory %s, %s", dir, strerror(error));
86 closedir(dp);
87 return FALSE;
88 }
89 error = 0;
90 while ((entry = readdir(dp)) != NULL) {
91 if (stat(entry->d_name, &statbuf) != -1) {
92 error = errno;
93 if (S_ISDIR(statbuf.st_mode)) {
94 if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) {
95 continue;
96 }
97 /* Recurse */
98 if (recurse != FALSE) {
99 if (n_scan_dir(entry->d_name, result, recurse) == FALSE) {
100 n_log(LOG_ERR, entry->d_name, "error while recursively scanning %s", entry->d_name);
101 }
102 }
103 } else if (S_ISREG(statbuf.st_mode)) {
104 N_FILE_INFO* file = NULL;
105 Malloc(file, N_FILE_INFO, 1);
106 file->name = strdup(entry->d_name);
107#ifdef XLINUX
108 file->filetime = statbuf.st_mtime;
109 file->filetime_nsec = statbuf.st_mtimensec;
110#elif defined SOLARIS
111 file->filetime = statbuf.st_mtim.tv_sec;
112 file->filetime_nsec = statbuf.st_mtim.tv_nsec;
113#else
114 file->filetime = statbuf.st_mtime;
115 file->filetime_nsec = 0;
116#endif
117
119 } else {
120 n_log(LOG_ERR, "Not a S_ISDIR or S_ISREG file: %s", entry->d_name);
121 }
122 } else {
123 error = errno;
124 n_log(LOG_ERR, "unable to stat %s, %s", entry->d_name, strerror(error));
125 }
126 }
127 if (error != 0) {
128 n_log(LOG_ERR, "readdir failed: %s", strerror(error));
129 }
130 if (chdir("..") == -1) {
131 error = errno;
132 n_log(LOG_ERR, "cannot chdir back to parent from %s, %s", dir, strerror(error));
133 }
134 closedir(dp);
135 return TRUE;
136}
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
int list_push_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
Add a pointer sorted in the list , starting by the end of the list.
Definition n_list.c:233
Structure of a generic LIST container.
Definition n_list.h:39
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_ERR
error conditions
Definition n_log.h:56
time_t filetime
file creation time
Definition n_files.h:30
char * name
file name
Definition n_files.h:28
int n_scan_dir(const char *dir, LIST *result, const int recurse)
Scan given directory and return a LIST of char.
Definition n_files.c:69
void n_free_file_info(void *ptr)
free a N_FILE_INFO struct
Definition n_files.c:14
common file information
Definition n_files.h:26
static int n_comp_file_info(const void *a, const void *b)
local comparison function for sorting filenames, case insensitive
Definition n_files.c:29
files configuration header