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
10
15void n_free_file_info( void *ptr )
16{
17 __n_assert( ptr , return );
18 N_FILE_INFO *file = (N_FILE_INFO *)ptr ;
19 if( file -> name )
20 free( file -> name );
21 free(file);
22 return ;
23}
24
31static int n_comp_file_info( const void *a , const void *b )
32{
33 N_FILE_INFO *f1 , *f2 ;
34
35 f1=(N_FILE_INFO *)a ;
36 f2=(N_FILE_INFO *)b ;
37
38 int ret = -1 ;
39
40 if( !f1 )
41 {
42 return -1 ;
43 }
44 if( !f2 )
45 {
46 return 1 ;
47 }
48
49 if( f1 -> filetime < f2 -> filetime )
50 {
51 ret = -1 ;
52 }
53 else if( f1 -> filetime == f2 -> filetime )
54 {
55 if( f1 -> filetime_nsec < f2 -> filetime_nsec )
56 {
57 ret = -1 ;
58 }
59 else if( f1 -> filetime_nsec > f2 -> filetime_nsec )
60 {
61 ret = 1 ;
62 }
63 }
64 else if( f1 -> filetime > f2 -> filetime )
65 {
66 ret = 1 ;
67 }
68
69 return ret ;
70}
71
82int n_scan_dir( const char *dir, LIST *result , const int recurse )
83{
84 DIR *dp = NULL ;
85 struct dirent *entry = NULL;
86 struct stat statbuf;
87
88 __n_assert( dir , return FALSE );
89 __n_assert( result , return FALSE );
90
91 dp = opendir( dir );
92 int error = errno ;
93 if( dp == NULL )
94 {
95 n_log( LOG_ERR , "cannot open directory: %s, %s" , dir , strerror( error ) );
96 return FALSE ;
97 }
98 if( chdir( dir ) == -1 )
99 {
100 error = errno ;
101 n_log( LOG_ERR , "cannot chdir to directory %s, %s" , dir , strerror( error ) );
102 closedir( dp );
103 return FALSE ;
104 }
105 error = 0 ;
106 while( ( entry = readdir( dp ) ) != NULL )
107 {
108 if( stat( entry -> d_name , &statbuf ) != -1 )
109 {
110 error = errno ;
111 if( S_ISDIR( statbuf . st_mode ) )
112 {
113 if( strcmp( "." , entry -> d_name ) == 0 || strcmp( ".." , entry -> d_name ) == 0 )
114 {
115 continue;
116 }
117 /* Recurse */
118 if( recurse != FALSE )
119 {
120 if( n_scan_dir( entry -> d_name , result, recurse ) == FALSE )
121 {
122 n_log( LOG_ERR , entry -> d_name , "error while recursively scanning %s" , entry -> d_name );
123 }
124 }
125 }
126 else if( S_ISREG( statbuf . st_mode ) )
127 {
128 N_FILE_INFO *file = NULL ;
129 Malloc( file , N_FILE_INFO , 1 );
130 file -> name = strdup( entry -> d_name );
131#ifdef XLINUX
132 file -> filetime = statbuf . st_mtime ;
133 file -> filetime_nsec = statbuf . st_mtimensec ;
134#elif defined SOLARIS
135 file -> filetime = statbuf . st_mtim . tv_sec ;
136 file -> filetime_nsec = statbuf . st_mtim . tv_nsec ;
137#else
138 file -> filetime = statbuf . st_mtime ;
139 file -> filetime_nsec = 0 ;
140#endif
141
143 }
144 else
145 {
146 n_log( LOG_ERR , "Not a S_ISDIR or S_ISREG file: %s" , entry -> d_name );
147 }
148 }
149 else
150 {
151 error = errno ;
152 n_log( LOG_ERR , "unable to stat %s, %s" , entry -> d_name , strerror( error ) );
153 }
154 }
155 if( error != 0 )
156 {
157 n_log( LOG_ERR , "readdir failed: %s" , strerror( error ) );
158 }
159 closedir( dp );
160 return TRUE ;
161}
162
163
164
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:191
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:284
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:285
Structure of a generic LIST container.
Definition n_list.h:45
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:74
#define LOG_ERR
error conditions
Definition n_log.h:58
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:82
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:32
static int n_comp_file_info(const void *a, const void *b)
local comparison function for sorting filenames, case insensitive
Definition n_files.c:31
files configuration header