Cleanup: move filelist out of storage
[blender.git] / source / blender / blenlib / intern / BLI_filelist.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/blenlib/intern/filelist.c
22  *  \ingroup bli
23  */
24
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #ifndef WIN32
30 #  include <dirent.h>
31 #endif
32
33 #include <time.h>
34 #include <sys/stat.h>
35 #include <string.h>  /* strcpy etc.. */
36
37 #ifdef WIN32
38 #  include <io.h>
39 #  include <direct.h>
40 #  include "BLI_winstuff.h"
41 #  include "utfconv.h"
42 #else
43 #  include <sys/ioctl.h>
44 #  include <unistd.h>
45 #  include <pwd.h>
46 #endif
47
48 /* lib includes */
49 #include "MEM_guardedalloc.h"
50
51 #include "DNA_listBase.h"
52
53 #include "BLI_listbase.h"
54 #include "BLI_string.h"
55 #include "BLI_fileops.h"
56 #include "BLI_fileops_types.h"
57 #include "BLI_path_util.h"
58
59 #include "../imbuf/IMB_imbuf.h"
60
61
62 /*
63  * Ordering function for sorting lists of files/directories. Returns -1 if
64  * entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped.
65  */
66 static int bli_compare(struct direntry *entry1, struct direntry *entry2)
67 {
68         /* type is equal to stat.st_mode */
69
70         /* directories come before non-directories */
71         if (S_ISDIR(entry1->type)) {
72                 if (S_ISDIR(entry2->type) == 0) return (-1);
73         }
74         else {
75                 if (S_ISDIR(entry2->type)) return (1);
76         }
77         /* non-regular files come after regular files */
78         if (S_ISREG(entry1->type)) {
79                 if (S_ISREG(entry2->type) == 0) return (-1);
80         }
81         else {
82                 if (S_ISREG(entry2->type)) return (1);
83         }
84         /* arbitrary, but consistent, ordering of different types of non-regular files */
85         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
86         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
87
88         /* OK, now we know their S_IFMT fields are the same, go on to a name comparison */
89         /* make sure "." and ".." are always first */
90         if (FILENAME_IS_CURRENT(entry1->relname)) return (-1);
91         if (FILENAME_IS_CURRENT(entry2->relname)) return (1);
92         if (FILENAME_IS_PARENT(entry1->relname)) return (-1);
93         if (FILENAME_IS_PARENT(entry2->relname)) return (1);
94
95         return (BLI_natstrcmp(entry1->relname, entry2->relname));
96 }
97
98
99 struct BuildDirCtx {
100         struct direntry *files; /* array[nrfiles] */
101         int nrfiles;
102 };
103
104 /**
105  * Scans the directory named *dirname and appends entries for its contents to files.
106  */
107 static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
108 {
109         struct ListBase dirbase = {NULL, NULL};
110         int newnum = 0;
111         DIR *dir;
112
113         if ((dir = opendir(dirname)) != NULL) {
114                 const struct dirent *fname;
115                 while ((fname = readdir(dir)) != NULL) {
116                         struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
117                         if (dlink != NULL) {
118                                 dlink->name = BLI_strdup(fname->d_name);
119                                 BLI_addhead(&dirbase, dlink);
120                                 newnum++;
121                         }
122                 }
123
124                 if (newnum) {
125                         if (dir_ctx->files) {
126                                 void * const tmp = MEM_reallocN(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry));
127                                 if (tmp) {
128                                         dir_ctx->files = (struct direntry *)tmp;
129                                 }
130                                 else { /* realloc fail */
131                                         MEM_freeN(dir_ctx->files);
132                                         dir_ctx->files = NULL;
133                                 }
134                         }
135
136                         if (dir_ctx->files == NULL)
137                                 dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), __func__);
138
139                         if (dir_ctx->files) {
140                                 struct dirlink * dlink = (struct dirlink *) dirbase.first;
141                                 struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles];
142                                 while (dlink) {
143                                         char fullname[PATH_MAX];
144                                         memset(file, 0, sizeof(struct direntry));
145                                         file->relname = dlink->name;
146                                         file->path = BLI_strdupcat(dirname, dlink->name);
147                                         BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name);
148                                         if (BLI_stat(fullname, &file->s) != -1) {
149                                                 file->type = file->s.st_mode;
150                                         }
151                                         file->flags = 0;
152                                         dir_ctx->nrfiles++;
153                                         file++;
154                                         dlink = dlink->next;
155                                 }
156                         }
157                         else {
158                                 printf("Couldn't get memory for dir\n");
159                                 exit(1);
160                         }
161
162                         BLI_freelist(&dirbase);
163                         if (dir_ctx->files) {
164                                 qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare);
165                         }
166                 }
167                 else {
168                         printf("%s empty directory\n", dirname);
169                 }
170
171                 closedir(dir);
172         }
173         else {
174                 printf("%s non-existent directory\n", dirname);
175         }
176 }
177
178 /**
179  * Fills in the "mode[123]", "size" and "string" fields in the elements of the files
180  * array with descriptive details about each item. "string" will have a format similar to "ls -l".
181  */
182 static void bli_adddirstrings(struct BuildDirCtx *dir_ctx)
183 {
184         const char *types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
185         /* symbolic display, indexed by mode field value */
186         int num;
187 #ifdef WIN32
188         __int64 st_size;
189 #else
190         off_t st_size;
191         int mode;
192 #endif
193
194         struct direntry *file;
195         struct tm *tm;
196         time_t zero = 0;
197
198         for (num = 0, file = dir_ctx->files; num < dir_ctx->nrfiles; num++, file++) {
199
200
201                 /* Mode */
202 #ifdef WIN32
203                 BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
204                 BLI_strncpy(file->mode2, types[0], sizeof(file->mode2));
205                 BLI_strncpy(file->mode3, types[0], sizeof(file->mode3));
206 #else
207                 mode = file->s.st_mode;
208
209                 BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1));
210                 BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2));
211                 BLI_strncpy(file->mode3, types[(mode & 0007)],      sizeof(file->mode3));
212
213                 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2] == '-')) file->mode2[2] = 'l';
214
215                 if (mode & (S_ISUID | S_ISGID)) {
216                         if (file->mode1[2] == 'x') file->mode1[2] = 's';
217                         else file->mode1[2] = 'S';
218
219                         if (file->mode2[2] == 'x') file->mode2[2] = 's';
220                 }
221
222                 if (mode & S_ISVTX) {
223                         if (file->mode3[2] == 'x') file->mode3[2] = 't';
224                         else file->mode3[2] = 'T';
225                 }
226 #endif
227
228
229                 /* User */
230 #ifdef WIN32
231                 strcpy(file->owner, "user");
232 #else
233                 {
234                         struct passwd *pwuser;
235                         pwuser = getpwuid(file->s.st_uid);
236                         if (pwuser) {
237                                 BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner));
238                         }
239                         else {
240                                 BLI_snprintf(file->owner, sizeof(file->owner), "%u", file->s.st_uid);
241                         }
242                 }
243 #endif
244
245
246                 /* Time */
247                 tm = localtime(&file->s.st_mtime);
248                 // prevent impossible dates in windows
249                 if (tm == NULL) tm = localtime(&zero);
250                 strftime(file->time, sizeof(file->time), "%H:%M", tm);
251                 strftime(file->date, sizeof(file->date), "%d-%b-%y", tm);
252
253
254                 /* Size */
255                 /*
256                  * Seems st_size is signed 32-bit value in *nix and Windows.  This
257                  * will buy us some time until files get bigger than 4GB or until
258                  * everyone starts using __USE_FILE_OFFSET64 or equivalent.
259                  */
260                 st_size = file->s.st_size;
261
262                 if (st_size > 1024 * 1024 * 1024) {
263                         BLI_snprintf(file->size, sizeof(file->size), "%.2f GiB", ((double)st_size) / (1024 * 1024 * 1024));
264                 }
265                 else if (st_size > 1024 * 1024) {
266                         BLI_snprintf(file->size, sizeof(file->size), "%.1f MiB", ((double)st_size) / (1024 * 1024));
267                 }
268                 else if (st_size > 1024) {
269                         BLI_snprintf(file->size, sizeof(file->size), "%d KiB", (int)(st_size / 1024));
270                 }
271                 else {
272                         BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size);
273                 }
274         }
275 }
276
277 /**
278  * Scans the contents of the directory named *dirname, and allocates and fills in an
279  * array of entries describing them in *filelist.
280  *
281  * \return The length of filelist array.
282  */
283 unsigned int BLI_filelist_dir_contents(const char *dirname,  struct direntry **filelist)
284 {
285         struct BuildDirCtx dir_ctx;
286
287         dir_ctx.nrfiles = 0;
288         dir_ctx.files = NULL;
289
290         bli_builddir(&dir_ctx, dirname);
291         bli_adddirstrings(&dir_ctx);
292
293         if (dir_ctx.files) {
294                 *filelist = dir_ctx.files;
295         }
296         else {
297                 // keep blender happy. Blender stores this in a variable
298                 // where 0 has special meaning.....
299                 *filelist = MEM_mallocN(sizeof(**filelist), __func__);
300         }
301
302         return dir_ctx.nrfiles;
303 }
304
305 /**
306  * Deep-duplicate of an array of direntries, including the array itself.
307  *
308  * \param dup_poin If given, called for each non-NULL direntry->poin. Otherwise, pointer is always simply copied over.
309  */
310 void BLI_filelist_duplicate(
311         struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries,
312         void *(*dup_poin)(void *))
313 {
314         unsigned int i;
315
316         *dest_filelist = MEM_mallocN(sizeof(**dest_filelist) * (size_t)(nrentries), __func__);
317         for (i = 0; i < nrentries; ++i) {
318                 struct direntry * const src = &src_filelist[i];
319                 struct direntry *dest = &(*dest_filelist)[i];
320                 *dest = *src;
321                 if (dest->image) {
322                         dest->image = IMB_dupImBuf(src->image);
323                 }
324                 if (dest->relname) {
325                         dest->relname = MEM_dupallocN(src->relname);
326                 }
327                 if (dest->path) {
328                         dest->path = MEM_dupallocN(src->path);
329                 }
330                 if (dest->poin && dup_poin) {
331                         dest->poin = dup_poin(src->poin);
332                 }
333         }
334 }
335
336 /**
337  * frees storage for an array of direntries, including the array itself.
338  */
339 void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries, void (*free_poin)(void *))
340 {
341         unsigned int i;
342         for (i = 0; i < nrentries; ++i) {
343                 struct direntry *entry = filelist + i;
344                 if (entry->image) {
345                         IMB_freeImBuf(entry->image);
346                 }
347                 if (entry->relname)
348                         MEM_freeN(entry->relname);
349                 if (entry->path)
350                         MEM_freeN(entry->path);
351                 if (entry->poin && free_poin)
352                         free_poin(entry->poin);
353         }
354
355         if (filelist != NULL) {
356                 MEM_freeN(filelist);
357         }
358 }