1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* The MIT License
*
* Copyright (C) 2015-2017 Alexander Saprykin <saprykin.spb@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pdir.h"
#include "perror.h"
#include "pfile.h"
#include "pmem.h"
#include "pstring.h"
#include "perror-private.h"
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined (__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 24)
# define P_DIR_NON_REENTRANT 1
# elif defined (P_OS_SOLARIS) || defined (P_OS_QNX6) || defined (P_OS_UNIXWARE) || \
defined (P_OS_SCO) || defined (P_OS_IRIX) || defined (P_OS_HAIKU)
# define P_DIR_NEED_BUF_ALLOC 1
#endif
#ifdef P_DIR_NEED_BUF_ALLOC
# if defined (P_OS_SCO)
# define P_DIR_NEED_SIMPLE_R 1
# endif
#else
# if defined (P_OS_BEOS) || defined (P_OS_AMIGA)
# define P_DIR_NON_REENTRANT 1
# endif
#endif
struct PDir_ {
DIR * dir;
struct dirent *dir_result;
pchar *path;
pchar *orig_path;
};
P_LIB_API PDir *
p_dir_new (const pchar *path,
PError **error)
{
PDir *ret;
DIR *dir;
pchar *pathp;
if (P_UNLIKELY (path == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_INVALID_ARGUMENT,
0,
"Invalid input argument");
return NULL;
}
if (P_UNLIKELY ((dir = opendir (path)) == NULL)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call opendir() to open directory stream");
return NULL;
}
if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PDir))) == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_NO_RESOURCES,
0,
"Failed to allocate memory for directory structure");
closedir (dir);
return NULL;
}
ret->dir = dir;
ret->path = p_strdup (path);
ret->orig_path = p_strdup (path);
pathp = ret->path + strlen (ret->path) - 1;
if (*pathp == '/' || *pathp == '\\')
*pathp = '\0';
return ret;
}
P_LIB_API pboolean
p_dir_create (const pchar *path,
pint mode,
PError **error)
{
if (P_UNLIKELY (path == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_INVALID_ARGUMENT,
0,
"Invalid input argument");
return FALSE;
}
if (p_dir_is_exists (path))
return TRUE;
if (P_UNLIKELY (mkdir (path, (mode_t) mode) != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call mkdir() to create directory");
return FALSE;
} else
return TRUE;
}
P_LIB_API pboolean
p_dir_remove (const pchar *path,
PError **error)
{
if (P_UNLIKELY (path == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_INVALID_ARGUMENT,
0,
"Invalid input argument");
return FALSE;
}
if (!p_dir_is_exists (path)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_NOT_EXISTS,
0,
"Specified directory doesn't exist");
return FALSE;
}
if (P_UNLIKELY (rmdir (path) != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call rmdir() to remove directory");
return FALSE;
} else
return TRUE;
}
P_LIB_API pboolean
p_dir_is_exists (const pchar *path)
{
struct stat sb;
if (P_UNLIKELY (path == NULL))
return FALSE;
return (stat (path, &sb) == 0 && S_ISDIR (sb.st_mode)) ? TRUE : FALSE;
}
P_LIB_API pchar *
p_dir_get_path (const PDir *dir)
{
if (P_UNLIKELY (dir == NULL))
return NULL;
return p_strdup (dir->orig_path);
}
P_LIB_API PDirEntry *
p_dir_get_next_entry (PDir *dir,
PError **error)
{
PDirEntry *ret;
#ifdef P_DIR_NEED_BUF_ALLOC
struct dirent *dirent_st;
#elif !defined (P_DIR_NON_REENTRANT)
struct dirent dirent_st;
#endif
struct stat sb;
pchar *entry_path;
psize path_len;
#ifdef P_DIR_NEED_BUF_ALLOC
pint name_max;
#endif
if (P_UNLIKELY (dir == NULL || dir->dir == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_INVALID_ARGUMENT,
0,
"Invalid input argument");
return NULL;
}
#ifdef P_DIR_NEED_BUF_ALLOC
# if defined (P_OS_SOLARIS)
name_max = (pint) (FILENAME_MAX);
# elif defined (P_OS_SCO) || defined (P_OS_IRIX)
name_max = (pint) pathconf (dir->orig_path, _PC_NAME_MAX);
if (name_max == -1) {
if (p_error_get_last_system () == 0)
name_max = _POSIX_PATH_MAX;
else {
p_error_set_error_p (error,
(pint) P_ERROR_IO_FAILED,
0,
"Failed to get NAME_MAX using pathconf()");
return NULL;
}
}
# elif defined (P_OS_QNX6) || defined (P_OS_UNIXWARE) || defined (P_OS_HAIKU)
name_max = (pint) (NAME_MAX);
# endif
if (P_UNLIKELY ((dirent_st = p_malloc0 (sizeof (struct dirent) + name_max + 1)) == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_NO_RESOURCES,
0,
"Failed to allocate memory for internal directory entry");
return NULL;
}
# ifdef P_DIR_NEED_SIMPLE_R
p_error_set_last_system (0);
if ((dir->dir_result = readdir_r (dir->dir, dirent_st)) == NULL) {
if (P_UNLIKELY (p_error_get_last_system () != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call readdir_r() to read directory stream");
p_free (dirent_st);
return NULL;
}
}
# else
if (P_UNLIKELY (readdir_r (dir->dir, dirent_st, &dir->dir_result) != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call readdir_r() to read directory stream");
p_free (dirent_st);
return NULL;
}
# endif
#else
# ifdef P_DIR_NON_REENTRANT
p_error_set_last_system (0);
if ((dir->dir_result = readdir (dir->dir)) == NULL) {
if (P_UNLIKELY (p_error_get_last_system () != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call readdir() to read directory stream");
return NULL;
}
}
# else
if (P_UNLIKELY (readdir_r (dir->dir, &dirent_st, &dir->dir_result) != 0)) {
p_error_set_error_p (error,
(pint) p_error_get_last_io (),
p_error_get_last_system (),
"Failed to call readdir_r() to read directory stream");
return NULL;
}
# endif
#endif
if (dir->dir_result == NULL) {
#ifdef P_DIR_NEED_BUF_ALLOC
p_free (dirent_st);
#endif
return NULL;
}
if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PDirEntry))) == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_NO_RESOURCES,
0,
"Failed to allocate memory for directory entry");
#ifdef P_DIR_NEED_BUF_ALLOC
p_free (dirent_st);
#endif
return NULL;
}
#ifdef P_DIR_NEED_BUF_ALLOC
ret->name = p_strdup (dirent_st->d_name);
p_free (dirent_st);
#else
# ifdef P_DIR_NON_REENTRANT
ret->name = p_strdup (dir->dir_result->d_name);
# else
ret->name = p_strdup (dirent_st.d_name);
# endif
#endif
path_len = strlen (dir->path);
if (P_UNLIKELY ((entry_path = p_malloc0 (path_len + strlen (ret->name) + 2)) == NULL)) {
P_WARNING ("PDir::p_dir_get_next_entry: failed to allocate memory for stat()");
ret->type = P_DIR_ENTRY_TYPE_OTHER;
return ret;
}
strcat (entry_path, dir->path);
*(entry_path + path_len) = '/';
strcat (entry_path + path_len + 1, ret->name);
if (P_UNLIKELY (stat (entry_path, &sb) != 0)) {
P_WARNING ("PDir::p_dir_get_next_entry: stat() failed");
ret->type = P_DIR_ENTRY_TYPE_OTHER;
p_free (entry_path);
return ret;
}
p_free (entry_path);
if (S_ISDIR (sb.st_mode))
ret->type = P_DIR_ENTRY_TYPE_DIR;
else if (S_ISREG (sb.st_mode))
ret->type = P_DIR_ENTRY_TYPE_FILE;
else
ret->type = P_DIR_ENTRY_TYPE_OTHER;
return ret;
}
P_LIB_API pboolean
p_dir_rewind (PDir *dir,
PError **error)
{
if (P_UNLIKELY (dir == NULL || dir->dir == NULL)) {
p_error_set_error_p (error,
(pint) P_ERROR_IO_INVALID_ARGUMENT,
0,
"Invalid input argument");
return FALSE;
}
rewinddir (dir->dir);
return TRUE;
}
P_LIB_API void
p_dir_free (PDir *dir)
{
if (P_UNLIKELY (dir == NULL))
return;
if (P_LIKELY (dir->dir != NULL)) {
if (P_UNLIKELY (closedir (dir->dir) != 0))
P_ERROR ("PDir::p_dir_free: closedir() failed");
}
p_free (dir->path);
p_free (dir->orig_path);
p_free (dir);
}
|