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
379
380
381
382
383
384
385
386
387
388
389
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/blenlib/intern/BLI_filelist.c
* \ingroup bli
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
# include <dirent.h>
#endif
#include <time.h>
#include <sys/stat.h>
#include <string.h> /* strcpy etc.. */
#ifdef WIN32
# include <io.h>
# include <direct.h>
# include "BLI_winstuff.h"
# include "utfconv.h"
#else
# include <sys/ioctl.h>
# include <unistd.h>
# include <pwd.h>
#endif
/* lib includes */
#include "MEM_guardedalloc.h"
#include "DNA_listBase.h"
#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_fileops.h"
#include "BLI_fileops_types.h"
#include "BLI_path_util.h"
#include "../imbuf/IMB_imbuf.h"
/*
* Ordering function for sorting lists of files/directories. Returns -1 if
* entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped.
*/
static int bli_compare(struct direntry *entry1, struct direntry *entry2)
{
/* type is equal to stat.st_mode */
/* directories come before non-directories */
if (S_ISDIR(entry1->type)) {
if (S_ISDIR(entry2->type) == 0) return (-1);
}
else {
if (S_ISDIR(entry2->type)) return (1);
}
/* non-regular files come after regular files */
if (S_ISREG(entry1->type)) {
if (S_ISREG(entry2->type) == 0) return (-1);
}
else {
if (S_ISREG(entry2->type)) return (1);
}
/* arbitrary, but consistent, ordering of different types of non-regular files */
if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
/* OK, now we know their S_IFMT fields are the same, go on to a name comparison */
/* make sure "." and ".." are always first */
if (FILENAME_IS_CURRENT(entry1->relname)) return (-1);
if (FILENAME_IS_CURRENT(entry2->relname)) return (1);
if (FILENAME_IS_PARENT(entry1->relname)) return (-1);
if (FILENAME_IS_PARENT(entry2->relname)) return (1);
return (BLI_natstrcmp(entry1->relname, entry2->relname));
}
struct BuildDirCtx {
struct direntry *files; /* array[nrfiles] */
int nrfiles;
};
/**
* Scans the directory named *dirname and appends entries for its contents to files.
*/
static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
{
struct ListBase dirbase = {NULL, NULL};
int newnum = 0;
DIR *dir;
if ((dir = opendir(dirname)) != NULL) {
const struct dirent *fname;
bool has_current = false, has_parent = false;
while ((fname = readdir(dir)) != NULL) {
struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink != NULL) {
dlink->name = BLI_strdup(fname->d_name);
if (FILENAME_IS_PARENT(dlink->name)) {
has_parent = true;
}
else if (FILENAME_IS_CURRENT(dlink->name)) {
has_current = true;
}
BLI_addhead(&dirbase, dlink);
newnum++;
}
}
if (!has_parent) {
char pardir[FILE_MAXDIR];
BLI_strncpy(pardir, dirname, sizeof(pardir));
if (BLI_parent_dir(pardir) && (BLI_access(pardir, R_OK) == 0)) {
struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink != NULL) {
dlink->name = BLI_strdup(FILENAME_PARENT);
BLI_addhead(&dirbase, dlink);
newnum++;
}
}
}
if (!has_current) {
struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink != NULL) {
dlink->name = BLI_strdup(FILENAME_CURRENT);
BLI_addhead(&dirbase, dlink);
newnum++;
}
}
if (newnum) {
if (dir_ctx->files) {
void * const tmp = MEM_reallocN(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry));
if (tmp) {
dir_ctx->files = (struct direntry *)tmp;
}
else { /* realloc fail */
MEM_freeN(dir_ctx->files);
dir_ctx->files = NULL;
}
}
if (dir_ctx->files == NULL)
dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), __func__);
if (dir_ctx->files) {
struct dirlink * dlink = (struct dirlink *) dirbase.first;
struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles];
while (dlink) {
char fullname[PATH_MAX];
memset(file, 0, sizeof(struct direntry));
file->relname = dlink->name;
file->path = BLI_strdupcat(dirname, dlink->name);
BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name);
if (BLI_stat(fullname, &file->s) != -1) {
file->type = file->s.st_mode;
}
else if (FILENAME_IS_CURRPAR(file->relname)) {
/* Hack around for UNC paths on windows - does not support stat on '\\SERVER\foo\..', sigh... */
file->type |= S_IFDIR;
}
file->flags = 0;
dir_ctx->nrfiles++;
file++;
dlink = dlink->next;
}
}
else {
printf("Couldn't get memory for dir\n");
exit(1);
}
BLI_freelist(&dirbase);
if (dir_ctx->files) {
qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare);
}
}
else {
printf("%s empty directory\n", dirname);
}
closedir(dir);
}
else {
printf("%s non-existent directory\n", dirname);
}
}
/**
* Fills in the "mode[123]", "size" and "string" fields in the elements of the files
* array with descriptive details about each item. "string" will have a format similar to "ls -l".
*/
static void bli_adddirstrings(struct BuildDirCtx *dir_ctx)
{
const char *types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
/* symbolic display, indexed by mode field value */
int num;
off_t st_size;
struct direntry *file;
struct tm *tm;
time_t zero = 0;
#ifndef WIN32
int mode;
#endif
for (num = 0, file = dir_ctx->files; num < dir_ctx->nrfiles; num++, file++) {
/* Mode */
#ifdef WIN32
BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
BLI_strncpy(file->mode2, types[0], sizeof(file->mode2));
BLI_strncpy(file->mode3, types[0], sizeof(file->mode3));
#else
mode = file->s.st_mode;
BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1));
BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2));
BLI_strncpy(file->mode3, types[(mode & 0007)], sizeof(file->mode3));
if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2] == '-')) file->mode2[2] = 'l';
if (mode & (S_ISUID | S_ISGID)) {
if (file->mode1[2] == 'x') file->mode1[2] = 's';
else file->mode1[2] = 'S';
if (file->mode2[2] == 'x') file->mode2[2] = 's';
}
if (mode & S_ISVTX) {
if (file->mode3[2] == 'x') file->mode3[2] = 't';
else file->mode3[2] = 'T';
}
#endif
/* User */
#ifdef WIN32
strcpy(file->owner, "user");
#else
{
struct passwd *pwuser;
pwuser = getpwuid(file->s.st_uid);
if (pwuser) {
BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner));
}
else {
BLI_snprintf(file->owner, sizeof(file->owner), "%u", file->s.st_uid);
}
}
#endif
/* Time */
tm = localtime(&file->s.st_mtime);
// prevent impossible dates in windows
if (tm == NULL) tm = localtime(&zero);
strftime(file->time, sizeof(file->time), "%H:%M", tm);
strftime(file->date, sizeof(file->date), "%d-%b-%y", tm);
/* Size */
/*
* Seems st_size is signed 32-bit value in *nix and Windows. This
* will buy us some time until files get bigger than 4GB or until
* everyone starts using __USE_FILE_OFFSET64 or equivalent.
*/
st_size = file->s.st_size;
if (st_size > 1024 * 1024 * 1024) {
BLI_snprintf(file->size, sizeof(file->size), "%.2f GiB", ((double)st_size) / (1024 * 1024 * 1024));
}
else if (st_size > 1024 * 1024) {
BLI_snprintf(file->size, sizeof(file->size), "%.1f MiB", ((double)st_size) / (1024 * 1024));
}
else if (st_size > 1024) {
BLI_snprintf(file->size, sizeof(file->size), "%d KiB", (int)(st_size / 1024));
}
else {
BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size);
}
}
}
/**
* Scans the contents of the directory named *dirname, and allocates and fills in an
* array of entries describing them in *filelist.
*
* \return The length of filelist array.
*/
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **filelist)
{
struct BuildDirCtx dir_ctx;
dir_ctx.nrfiles = 0;
dir_ctx.files = NULL;
bli_builddir(&dir_ctx, dirname);
bli_adddirstrings(&dir_ctx);
if (dir_ctx.files) {
*filelist = dir_ctx.files;
}
else {
// keep blender happy. Blender stores this in a variable
// where 0 has special meaning.....
*filelist = MEM_mallocN(sizeof(**filelist), __func__);
}
return dir_ctx.nrfiles;
}
/**
* Deep-duplicate of an array of direntries, including the array itself.
*
* \param dup_poin If given, called for each non-NULL direntry->poin. Otherwise, pointer is always simply copied over.
*/
void BLI_filelist_duplicate(
struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries,
void *(*dup_poin)(void *))
{
unsigned int i;
*dest_filelist = MEM_mallocN(sizeof(**dest_filelist) * (size_t)(nrentries), __func__);
for (i = 0; i < nrentries; ++i) {
struct direntry * const src = &src_filelist[i];
struct direntry *dest = &(*dest_filelist)[i];
*dest = *src;
if (dest->image) {
dest->image = IMB_dupImBuf(src->image);
}
if (dest->relname) {
dest->relname = MEM_dupallocN(src->relname);
}
if (dest->path) {
dest->path = MEM_dupallocN(src->path);
}
if (dest->poin && dup_poin) {
dest->poin = dup_poin(src->poin);
}
}
}
/**
* frees storage for an array of direntries, including the array itself.
*/
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries, void (*free_poin)(void *))
{
unsigned int i;
for (i = 0; i < nrentries; ++i) {
struct direntry *entry = filelist + i;
if (entry->image) {
IMB_freeImBuf(entry->image);
}
if (entry->relname)
MEM_freeN(entry->relname);
if (entry->path)
MEM_freeN(entry->path);
if (entry->poin && free_poin)
free_poin(entry->poin);
}
if (filelist != NULL) {
MEM_freeN(filelist);
}
}
|