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