Blender V5.3
BLI_filelist.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cerrno>
10#include <cstdio>
11#include <cstdlib>
12#include <sys/types.h>
13
14#ifndef WIN32
15# include <dirent.h>
16#endif
17
18#include <cstring>
19#include <ctime>
20#include <sys/stat.h>
21
22#ifdef WIN32
23# include "BLI_winstuff.hh"
24# include "utfconv.hh"
25# include <direct.h>
26# include <io.h>
27#else
28# include <pwd.h>
29# include <sys/ioctl.h>
30# include <unistd.h>
31#endif
32
33/* lib includes */
34#include "MEM_guardedalloc.h"
35
36#include "DNA_listBase.h"
37
38#include "BLI_fileops.hh"
39#include "BLI_fileops_types.hh"
40#include "BLI_listbase.hh"
41#include "BLI_path_utils.hh"
42#include "BLI_string.hh"
43#include "BLI_string_utils.hh"
44
45namespace blender {
46
47/*
48 * Ordering function for sorting lists of files/directories. Returns -1 if
49 * entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped.
50 */
51static int direntry_cmp(direntry *entry1, direntry *entry2)
52{
53 /* type is equal to stat.st_mode */
54
55 /* directories come before non-directories */
56 if (S_ISDIR(entry1->type)) {
57 if (S_ISDIR(entry2->type) == 0) {
58 return -1;
59 }
60 }
61 else {
62 if (S_ISDIR(entry2->type)) {
63 return 1;
64 }
65 }
66 /* non-regular files come after regular files */
67 if (S_ISREG(entry1->type)) {
68 if (S_ISREG(entry2->type) == 0) {
69 return -1;
70 }
71 }
72 else {
73 if (S_ISREG(entry2->type)) {
74 return 1;
75 }
76 }
77 /* arbitrary, but consistent, ordering of different types of non-regular files */
78 if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) {
79 return -1;
80 }
81 if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) {
82 return 1;
83 }
84
85 /* OK, now we know their S_IFMT fields are the same, go on to a name comparison */
86 /* make sure "." and ".." are always first */
87 if (FILENAME_IS_CURRENT(entry1->relname)) {
88 return -1;
89 }
90 if (FILENAME_IS_CURRENT(entry2->relname)) {
91 return 1;
92 }
93 if (FILENAME_IS_PARENT(entry1->relname)) {
94 return -1;
95 }
96 if (FILENAME_IS_PARENT(entry2->relname)) {
97 return 1;
98 }
99
100 return BLI_strcasecmp_natural(entry1->relname, entry2->relname);
101}
102
104 direntry *files; /* array[files_num] */
106};
107
111static void bli_builddir(BuildDirCtx *dir_ctx, const char *dirname)
112{
114 DIR *dir = opendir(dirname);
115 if (dir == nullptr) [[unlikely]] {
116 fprintf(stderr,
117 "Failed to open dir (%s): %s\n",
118 errno ? strerror(errno) : "unknown error",
119 dirname);
120 return;
121 }
122
123 ListBaseT<dirlink> dirbase = {nullptr, nullptr};
124 int newnum = 0;
125 const dirent *fname;
126 bool has_current = false, has_parent = false;
127
128 char dirname_with_slash[FILE_MAXDIR + 1];
129 size_t dirname_with_slash_len = BLI_strncpy_rlen(
130 dirname_with_slash, dirname, sizeof(dirname_with_slash) - 1);
131
132 if ((dirname_with_slash_len > 0) &&
133 (BLI_path_slash_is_native_compat(dirname[dirname_with_slash_len - 1]) == false))
134 {
135 dirname_with_slash[dirname_with_slash_len++] = SEP;
136 dirname_with_slash[dirname_with_slash_len] = '\0';
137 }
138
139 while ((fname = readdir(dir)) != nullptr) {
140 dirlink *const dlink = static_cast<dirlink *>(malloc(sizeof(dirlink)));
141 if (dlink != nullptr) {
142 dlink->name = BLI_strdup(fname->d_name);
143 if (FILENAME_IS_PARENT(dlink->name)) {
144 has_parent = true;
145 }
146 else if (FILENAME_IS_CURRENT(dlink->name)) {
147 has_current = true;
148 }
149 BLI_addhead(&dirbase, dlink);
150 newnum++;
151 }
152 }
153
154 if (!has_parent) {
155 char pardir[FILE_MAXDIR];
156
157 STRNCPY(pardir, dirname);
158 if (BLI_path_parent_dir(pardir) && (BLI_access(pardir, R_OK) == 0)) {
159 dirlink *const dlink = static_cast<dirlink *>(malloc(sizeof(dirlink)));
160 if (dlink != nullptr) {
162 BLI_addhead(&dirbase, dlink);
163 newnum++;
164 }
165 }
166 }
167 if (!has_current) {
168 dirlink *const dlink = static_cast<dirlink *>(malloc(sizeof(dirlink)));
169 if (dlink != nullptr) {
171 BLI_addhead(&dirbase, dlink);
172 newnum++;
173 }
174 }
175
176 if (newnum) {
177 if (dir_ctx->files) {
178 void *const tmp = MEM_realloc_uninitialized(
179 dir_ctx->files, (dir_ctx->files_num + newnum) * sizeof(direntry));
180 if (tmp) {
181 dir_ctx->files = static_cast<direntry *>(tmp);
182 }
183 else { /* Reallocation may fail. */
184 MEM_delete(dir_ctx->files);
185 dir_ctx->files = nullptr;
186 }
187 }
188
189 if (dir_ctx->files == nullptr) {
190 dir_ctx->files = MEM_new_array_uninitialized<direntry>(size_t(newnum), __func__);
191 }
192
193 if (dir_ctx->files == nullptr) [[unlikely]] {
194 fprintf(stderr, "Couldn't get memory for dir: %s\n", dirname);
195 dir_ctx->files_num = 0;
196 }
197 else {
198 dirlink *dlink = dirbase.first();
199 direntry *file = &dir_ctx->files[dir_ctx->files_num];
200
201 while (dlink) {
202 memset(file, 0, sizeof(direntry));
203 file->relname = dlink->name;
204 file->path = BLI_string_joinN(dirname_with_slash, dlink->name);
205 if (BLI_stat(file->path, &file->s) != -1) {
206 file->type = file->s.st_mode;
207 }
208 else if (FILENAME_IS_CURRPAR(file->relname)) {
209 /* Unfortunately a hack around UNC paths on WIN32,
210 * which does not support `stat` on `\\SERVER\foo\..`. */
211 file->type |= S_IFDIR;
212 }
213 dir_ctx->files_num++;
214 file++;
215 dlink = dlink->next;
216 }
217
218 qsort(dir_ctx->files,
219 dir_ctx->files_num,
220 sizeof(direntry),
221 reinterpret_cast<int (*)(const void *, const void *)>(direntry_cmp));
222 }
223
224 BLI_freelist(&dirbase);
225 }
226
227 closedir(dir);
228}
229
231{
232 BuildDirCtx dir_ctx;
233
234 dir_ctx.files_num = 0;
235 dir_ctx.files = nullptr;
236
237 bli_builddir(&dir_ctx, dirname);
238
239 if (dir_ctx.files) {
240 *r_filelist = dir_ctx.files;
241 }
242 else {
243 /* Keep Blender happy. Blender stores this in a variable
244 * where 0 has special meaning..... */
245 *r_filelist = MEM_new_uninitialized<direntry>(__func__);
246 }
247
248 return dir_ctx.files_num;
249}
250
251void BLI_filelist_entry_size_to_string(const struct stat *st,
252 const uint64_t st_size_fallback,
253 const bool compact,
254 char r_size[FILELIST_DIRENTRY_SIZE_LEN])
255{
256 /*
257 * Seems st_size is signed 32-bit value in *nix and Windows. This
258 * will buy us some time until files get bigger than 4GB or until
259 * everyone starts using __USE_FILE_OFFSET64 or equivalent.
260 */
261 double size = double(st ? st->st_size : st_size_fallback);
262#ifdef WIN32
263 if (compact) {
264 BLI_str_format_byte_unit_compact(r_size, size, false);
265 }
266 else {
267 BLI_str_format_byte_unit(r_size, size, false);
268 }
269#else
270 if (compact) {
271 BLI_str_format_byte_unit_compact(r_size, size, true);
272 }
273 else {
274 BLI_str_format_byte_unit(r_size, size, true);
275 }
276#endif
277}
278
279void BLI_filelist_entry_mode_to_string(const struct stat *st,
280 const bool /*compact*/,
281 char r_mode1[FILELIST_DIRENTRY_MODE_LEN],
282 char r_mode2[FILELIST_DIRENTRY_MODE_LEN],
283 char r_mode3[FILELIST_DIRENTRY_MODE_LEN])
284{
285 const char *types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
286
287#ifdef WIN32
288 UNUSED_VARS(st);
289 BLI_strncpy(r_mode1, types[0], sizeof(*r_mode1) * FILELIST_DIRENTRY_MODE_LEN);
290 BLI_strncpy(r_mode2, types[0], sizeof(*r_mode2) * FILELIST_DIRENTRY_MODE_LEN);
291 BLI_strncpy(r_mode3, types[0], sizeof(*r_mode3) * FILELIST_DIRENTRY_MODE_LEN);
292#else
293 const int mode = st->st_mode;
294
295 BLI_strncpy(r_mode1, types[(mode & 0700) >> 6], sizeof(*r_mode1) * FILELIST_DIRENTRY_MODE_LEN);
296 BLI_strncpy(r_mode2, types[(mode & 0070) >> 3], sizeof(*r_mode2) * FILELIST_DIRENTRY_MODE_LEN);
297 BLI_strncpy(r_mode3, types[(mode & 0007)], sizeof(*r_mode3) * FILELIST_DIRENTRY_MODE_LEN);
298
299 if (((mode & S_ISGID) == S_ISGID) && (r_mode2[2] == '-')) {
300 r_mode2[2] = 'l';
301 }
302
303 if (mode & (S_ISUID | S_ISGID)) {
304 if (r_mode1[2] == 'x') {
305 r_mode1[2] = 's';
306 }
307 else {
308 r_mode1[2] = 'S';
309 }
310
311 if (r_mode2[2] == 'x') {
312 r_mode2[2] = 's';
313 }
314 }
315
316 if (mode & S_ISVTX) {
317 if (r_mode3[2] == 'x') {
318 r_mode3[2] = 't';
319 }
320 else {
321 r_mode3[2] = 'T';
322 }
323 }
324#endif
325}
326
327void BLI_filelist_entry_owner_to_string(const struct stat *st,
328 const bool /*compact*/,
329 char r_owner[FILELIST_DIRENTRY_OWNER_LEN])
330{
331#ifdef WIN32
332 UNUSED_VARS(st);
333 BLI_strncpy(r_owner, "unknown", FILELIST_DIRENTRY_OWNER_LEN);
334#else
335 const passwd *pwuser = getpwuid(st->st_uid);
336
337 if (pwuser) {
338 BLI_strncpy(r_owner, pwuser->pw_name, sizeof(*r_owner) * FILELIST_DIRENTRY_OWNER_LEN);
339 }
340 else {
341 BLI_snprintf(r_owner, sizeof(*r_owner) * FILELIST_DIRENTRY_OWNER_LEN, "%u", st->st_uid);
342 }
343#endif
344}
345
347{
348 *dst = *src;
349 if (dst->relname) {
350 dst->relname = MEM_dupalloc(src->relname);
351 }
352 if (dst->path) {
353 dst->path = MEM_dupalloc(src->path);
354 }
355}
356
357void BLI_filelist_duplicate(direntry **dest_filelist,
358 direntry *const src_filelist,
359 const uint nrentries)
360{
361 uint i;
362
363 *dest_filelist = MEM_new_array_uninitialized<direntry>(size_t(nrentries), __func__);
364 for (i = 0; i < nrentries; i++) {
365 const direntry *src = &src_filelist[i];
366 direntry *dst = &(*dest_filelist)[i];
368 }
369}
370
372{
373 if (entry->relname) {
374 MEM_delete(entry->relname);
375 }
376 if (entry->path) {
377 MEM_delete(entry->path);
378 }
379}
380
381void BLI_filelist_free(direntry *filelist, const uint nrentries)
382{
383 uint i;
384 for (i = 0; i < nrentries; i++) {
385 BLI_filelist_entry_free(&filelist[i]);
386 }
387
388 if (filelist != nullptr) {
389 MEM_delete(filelist);
390 }
391}
392
393} // namespace blender
File and directory operations.
Some types for dealing with directories.
#define FILELIST_DIRENTRY_SIZE_LEN
#define FILELIST_DIRENTRY_MODE_LEN
#define FILELIST_DIRENTRY_OWNER_LEN
#define FILENAME_IS_CURRENT(_n)
#define FILENAME_IS_CURRPAR(_n)
#define FILENAME_CURRENT
#define FILENAME_PARENT
#define FILE_MAXDIR
#define FILENAME_IS_PARENT(_n)
#define BLI_string_joinN(...)
Compatibility-like things for windows.
struct __dirstream DIR
struct dirent * readdir(DIR *dp)
int closedir(DIR *dp)
#define S_ISDIR(x)
const char * dirname(char *path)
DIR * opendir(const char *path)
#define S_ISREG(x)
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define MEM_realloc_uninitialized(vmemh, len)
BMesh Iterators.
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_filelist_duplicate(struct direntry **dest_filelist, struct direntry *const src_filelist, unsigned int nrentries)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
void BLI_filelist_entry_mode_to_string(const struct stat *st, bool compact, char r_mode1[FILELIST_DIRENTRY_MODE_LEN], char r_mode2[FILELIST_DIRENTRY_MODE_LEN], char r_mode3[FILELIST_DIRENTRY_MODE_LEN])
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src)
void BLI_str_format_byte_unit_compact(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_COMPACT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BLI_freelist(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:490
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void bli_builddir(BuildDirCtx *dir_ctx, const char *dirname)
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static int direntry_cmp(direntry *entry1, direntry *entry2)
bool BLI_path_parent_dir(char *path) ATTR_NONNULL(1)
void BLI_filelist_entry_free(struct direntry *entry)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
constexpr char SEP
void BLI_filelist_entry_owner_to_string(const struct stat *st, bool compact, char r_owner[FILELIST_DIRENTRY_OWNER_LEN])
void BLI_filelist_entry_size_to_string(const struct stat *st, uint64_t st_size_fallback, bool compact, char r_size[FILELIST_DIRENTRY_SIZE_LEN])
char * d_name