Blender V5.3
filelist_readjob_common.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "AS_asset_library.hh"
10
11#include "BLI_linklist.hh"
12#include "BLI_listbase.hh"
13#include "BLI_path_utils.hh"
14#include "BLI_stack_c.hh"
15#include "BLI_string.hh"
16#include "BLI_string_utils.hh"
17
18#include "BKE_asset.hh"
19#include "BKE_blendfile.hh"
20#include "BKE_idtype.hh"
21
22#ifdef WIN32
23# include "BKE_appdir.hh"
24# include "BLF_api.hh"
25# include "BLI_winstuff.hh"
26#endif
27
28#include "DNA_space_enums.h"
29
30#include "ED_file_indexer.hh"
31#include "ED_fileselect.hh"
32
33#include "filelist_intern.hh"
34
35#include "filelist_readjob.hh"
36
37namespace blender {
38
39/* helper, could probably go in BKE actually? */
40static int groupname_to_code(const char *group)
41{
42 char buf[BLO_GROUP_MAX];
43 char *lslash;
44
45 BLI_assert(group);
46
47 STRNCPY(buf, group);
48 lslash = const_cast<char *>(BLI_path_slash_rfind(buf));
49 if (lslash) {
50 lslash[0] = '\0';
51 }
52
53 return buf[0] ? BKE_idtype_idcode_from_name(buf) : 0;
54}
55
61char *current_relpath_append(const FileListReadJob *job_params, const char *filename)
62{
63 char relbase[sizeof(job_params->cur_relbase)];
64 STRNCPY(relbase, job_params->cur_relbase);
65
66 /* Early exit, nothing to join. */
67 if (!relbase[0]) {
68 return BLI_strdup(filename);
69 }
70
71 BLI_path_slash_ensure(relbase, sizeof(relbase));
72
73 char relpath[FILE_MAX_LIBEXTRA];
74 /* Using #BLI_path_join works but isn't needed as `rel_subdir` has a trailing slash. */
75 BLI_string_join(relpath,
76 sizeof(relpath),
77 /* + 2 to remove "//" relative path prefix. */
78 BLI_path_is_rel(relbase) ? relbase + 2 : relbase,
79 filename);
80
81 return BLI_strdup(relpath);
82}
83
84/* -------------------------------------------------------------------- */
87
89 char /*dirpath*/[FILE_MAX_LIBEXTRA],
90 const bool /*do_change*/)
91{
92 return true;
93}
94
96{
97 /* Only allow absolute paths as CWD relative doesn't make sense from the UI. */
99 return;
100 }
101
102#ifdef WIN32
104#else
105 ARRAY_SET_ITEMS(dir, '/', '\0');
106#endif
107}
108
109bool filelist_checkdir_dir(const FileList * /*filelist*/,
110 char dirpath[FILE_MAX_LIBEXTRA],
111 const bool do_change)
112{
113 bool is_valid;
114 if (do_change) {
116 is_valid = true;
117 }
118 else {
119 is_valid = BLI_path_is_abs_from_cwd(dirpath) && BLI_is_dir(dirpath);
120 }
121 return is_valid;
122}
123
124bool filelist_checkdir_lib(const FileList * /*filelist*/,
125 char dirpath[FILE_MAX_LIBEXTRA],
126 const bool do_change)
127{
128 char tdir[FILE_MAX_LIBEXTRA];
129 char *name;
130
131 const bool is_valid = (BLI_is_dir(dirpath) ||
132 (BKE_blendfile_library_path_explode(dirpath, tdir, nullptr, &name) &&
133 BLI_is_file(tdir) && !name));
134
135 if (do_change && !is_valid) {
136 /* if not a valid library, we need it to be a valid directory! */
138 return true;
139 }
140 return is_valid;
141}
142
144
145/* -------------------------------------------------------------------- */
148
153 ListBaseT<FileListInternEntry> *from_entries,
154 int from_entries_num)
155{
156 BLI_assert(from_entries->count() == from_entries_num);
157 if (from_entries_num <= 0) {
158 return false;
159 }
160
161 FileList *filelist = job_params->tmp_filelist; /* Use the thread-safe filelist queue. */
162 std::scoped_lock lock(job_params->lock);
163 BLI_movelisttolist(&filelist->filelist.entries, from_entries);
164 filelist->filelist.entries_num += from_entries_num;
165
166 return true;
167}
168
169#ifdef WIN32
170static int filelist_add_userfonts_regpath(HKEY hKeyParent,
171 LPCSTR subkeyName,
172 ListBaseT<FileListInternEntry> *entries)
173{
174 int font_num = 0;
175 HKEY key = 0;
176 /* Try to open the requested key. */
177 if (RegOpenKeyExA(hKeyParent, subkeyName, 0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS) {
178 return 0;
179 }
180
181 DWORD index = 0;
182 /* Value name and data buffers (ANSI). */
183 TCHAR KeyName[255];
184 DWORD KeyNameLen = sizeof(KeyName);
185 TCHAR KeyValue[FILE_MAX];
186 DWORD KeyValueLen = sizeof(KeyValue);
187 DWORD valueType;
188
189 /* Enumerate values. */
190 while (RegEnumValueA(key,
191 index,
192 (LPSTR)&KeyName,
193 &KeyNameLen,
194 NULL,
195 &valueType,
196 (LPBYTE)&KeyValue,
197 &KeyValueLen) == ERROR_SUCCESS)
198 {
199 /* Only consider string values (paths). */
200 if (valueType == REG_SZ || valueType == REG_EXPAND_SZ) {
201 FileListInternEntry *entry = MEM_new<FileListInternEntry>(__func__);
202 /* Find last slash to determine basename/relpath portion. */
203 const char *val_str = (const char *)KeyValue;
204 const char *lslash_str = BLI_path_slash_rfind(val_str);
205 const size_t lslash = lslash_str ? size_t(lslash_str - val_str) + 1 : 0;
206
207 BLI_stat(val_str, &entry->st);
208 entry->relpath = BLI_strdup(val_str + lslash);
209 entry->name = BLF_display_name_from_file(val_str);
210 entry->free_name = true;
211 entry->attributes = FILE_ATTR_READONLY & FILE_ATTR_ALIAS;
212 entry->typeflag = FILE_TYPE_FTFONT;
213 entry->redirection_path = BLI_strdup(val_str);
214 BLI_addtail(entries, entry);
215 font_num++;
216 }
217
218 KeyNameLen = sizeof(KeyName);
219 KeyValueLen = sizeof(KeyValue);
220 index++;
221 }
222
223 /* Enumerate sub-keys and recurse into them. */
224 index = 0;
225 while (RegEnumKeyExA(key, index, (LPSTR)&KeyName, &KeyNameLen, NULL, NULL, NULL, NULL) ==
226 ERROR_SUCCESS)
227 {
228 font_num += filelist_add_userfonts_regpath(key, KeyName, entries);
229 KeyNameLen = sizeof(KeyName);
230 index++;
231 }
232
233 RegCloseKey(key);
234 return font_num;
235}
236
237static int filelist_add_userfonts(ListBaseT<FileListInternEntry> *entries)
238{
239 return filelist_add_userfonts_regpath(
240 HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", entries);
241}
242
243#endif
244
246 const char *root,
247 ListBaseT<FileListInternEntry> *entries,
248 const char *filter_glob,
249 const bool do_lib,
250 const char *main_filepath,
251 const bool skip_currpar)
252{
253 direntry *files;
254 int entries_num = 0;
255 /* Full path of the item. */
256 char full_path[FILE_MAX];
257
258#ifdef WIN32
259 char fonts_path[FILE_MAXDIR] = {0};
260 BKE_appdir_font_folder_default(fonts_path, sizeof(fonts_path));
261 BLI_path_slash_ensure(fonts_path, sizeof(fonts_path));
262 if (STREQ(root, fonts_path)) {
263 entries_num += filelist_add_userfonts(entries);
264 }
265#endif
266
267 const int files_num = BLI_filelist_dir_contents(root, &files);
268 if (files) {
269 int i = files_num;
270 while (i--) {
271 FileListInternEntry *entry;
272
273 if (skip_currpar && FILENAME_IS_CURRPAR(files[i].relname)) {
274 continue;
275 }
276
277 entry = MEM_new<FileListInternEntry>(__func__);
278 entry->relpath = current_relpath_append(job_params, files[i].relname);
279 entry->st = files[i].s;
280
281 BLI_path_join(full_path, FILE_MAX, root, files[i].relname);
282 char *target = full_path;
283
284 /* Set initial file type and attributes. */
285 entry->attributes = BLI_file_attributes(full_path);
286 if (S_ISDIR(files[i].s.st_mode)
287#ifdef __APPLE__
289#endif
290 )
291 {
292 entry->typeflag = FILE_TYPE_DIR;
293 }
294
295 /* Is this a file that points to another file? */
296 if (entry->attributes & FILE_ATTR_ALIAS) {
298 if (BLI_file_alias_target(full_path, entry->redirection_path)) {
299 if (BLI_is_dir(entry->redirection_path)) {
300 entry->typeflag = FILE_TYPE_DIR;
302 }
303 else {
305 }
306 target = entry->redirection_path;
307#ifdef WIN32
308 /* On Windows don't show `.lnk` extension for valid shortcuts. */
310#endif
311 }
312 else {
313 MEM_delete(entry->redirection_path);
314 entry->redirection_path = nullptr;
316 }
317 }
318
319 if (!(entry->typeflag & FILE_TYPE_DIR)) {
320 if (do_lib && BKE_blendfile_extension_check(target)) {
321 /* If we are considering .blend files as libraries, promote them to directory status. */
323 /* prevent current file being used as acceptable dir */
324 if (BLI_path_cmp(main_filepath, target) != 0) {
325 entry->typeflag |= FILE_TYPE_DIR;
326 }
327 }
328 else {
330 }
331 }
332
333 filelist_entry_glob_tag(entry, filter_glob);
334
335#ifndef WIN32
336 /* Set linux-style dot files hidden too. */
339 }
340#endif
341
342 BLI_addtail(entries, entry);
343 entries_num++;
344 }
345 BLI_filelist_free(files, files_num);
346 }
347 return entries_num;
348}
349
355struct TodoDir {
356 int level;
357 char *dir;
358};
359
365
370};
371
374
375 /* Will read both the groups + actual ids from the library. Reduces the amount of times that
376 * a library needs to be opened. */
378
379 /* Will only list assets. */
381
382 /* Add given root as result. */
384};
386
388 const FileListReadJob *job_params, const int idcode, const char *group_name)
389{
390 FileListInternEntry *entry = MEM_new<FileListInternEntry>(__func__);
391 entry->relpath = current_relpath_append(job_params, group_name);
393 entry->blentype = idcode;
394 return entry;
395}
396
398 FileListReadJob *job_params,
399 ListBaseT<FileListInternEntry> *entries,
400 BLODataBlockInfo *datablock_info,
401 const bool prefix_relpath_with_group_name,
402 const int idcode,
403 const char *group_name,
404 const std::optional<asset_system::OnlineAssetInfo> online_asset_info)
405{
406 FileListInternEntry *entry = MEM_new<FileListInternEntry>(__func__);
407 if (prefix_relpath_with_group_name) {
408 std::string datablock_path = StringRef(group_name) + SEP_STR + datablock_info->name;
409 entry->relpath = current_relpath_append(job_params, datablock_path.c_str());
410 }
411 else {
412 entry->relpath = current_relpath_append(job_params, datablock_info->name);
413 }
415 if (datablock_info) {
416 entry->blenderlib_has_no_preview = datablock_info->no_preview_found;
417
418 if (datablock_info->name[0] == '.') {
420 }
421
422 if (datablock_info->asset_data) {
423
424 entry->typeflag |= FILE_TYPE_ASSET;
425 if (online_asset_info) {
427 }
428
429 if (job_params->load_asset_library) {
430 /* We never want to add assets directly to the "All" library, always add to the actually
431 * containing one. */
433
434 /* Take ownership over the asset data (shallow copies into unique_ptr managed memory) to
435 * pass it on to the asset system. */
436 std::unique_ptr metadata = std::make_unique<AssetMetaData>(
437 std::move(*datablock_info->asset_data));
438 MEM_delete(datablock_info->asset_data);
439 /* Give back a non-owning pointer, because the data-block info is still needed (e.g. to
440 * update the asset index). */
441 datablock_info->asset_data = metadata.get();
442 datablock_info->free_asset_data = false;
443
444 entry->asset = online_asset_info ?
446 entry->relpath,
447 datablock_info->name,
448 idcode,
449 std::move(metadata),
450 *online_asset_info) :
452 entry->relpath, datablock_info->name, idcode, std::move(metadata));
453 if (job_params->on_asset_added) {
454 (*job_params->on_asset_added)(*entry->get_asset());
455 }
456 }
457 }
458 }
459 entry->blentype = idcode;
460 BLI_addtail(entries, entry);
461}
462
464 ListBaseT<FileListInternEntry> *entries,
465 LinkNode *datablock_infos,
466 const bool prefix_relpath_with_group_name,
467 const int idcode,
468 const char *group_name)
469{
470 for (LinkNode *ln = datablock_infos; ln; ln = ln->next) {
471 BLODataBlockInfo *datablock_info = static_cast<BLODataBlockInfo *>(ln->link);
473 job_params, entries, datablock_info, prefix_relpath_with_group_name, idcode, group_name);
474 }
475}
476
478 const FileListReadJob *job_params)
479{
480 FileListInternEntry *entry = MEM_new<FileListInternEntry>(__func__);
481 entry->relpath = current_relpath_append(job_params, FILENAME_PARENT);
483 return entry;
484}
485
487 FileListReadJob *job_params,
488 ListBaseT<FileListInternEntry> *entries,
489 const FileIndexerEntries *indexer_entries,
490 const bool prefix_relpath_with_group_name)
491{
492 for (const LinkNode *ln = indexer_entries->entries; ln; ln = ln->next) {
493 FileIndexerEntry *indexer_entry = static_cast<FileIndexerEntry *>(ln->link);
494 const char *group_name = BKE_idtype_idcode_to_name(indexer_entry->idcode);
496 entries,
497 &indexer_entry->datablock_info,
498 prefix_relpath_with_group_name,
499 indexer_entry->idcode,
500 group_name);
501 }
502}
503
505 ListBaseT<FileListInternEntry> *entries,
507 const int read_from_index,
508 const FileIndexerEntries *indexer_entries)
509{
510 int navigate_to_parent_len = 0;
513 job_params);
514 BLI_addtail(entries, entry);
515 navigate_to_parent_len = 1;
516 }
517
518 filelist_readjob_list_lib_add_from_indexer_entries(job_params, entries, indexer_entries, true);
519 return read_from_index + navigate_to_parent_len;
520}
521
526static std::optional<int> filelist_readjob_list_lib(FileListReadJob *job_params,
527 const char *root,
528 ListBaseT<FileListInternEntry> *entries,
530 FileIndexer *indexer_runtime)
531{
532 BLI_assert(indexer_runtime);
533
534 char dir[FILE_MAX_LIBEXTRA], *group;
535
536 BlendHandle *libfiledata = nullptr;
537
538 /* Check if the given root is actually a library. All folders are passed to
539 * `filelist_readjob_list_lib` and based on the number of found entries `filelist_readjob_do`
540 * will do a dir listing only when this function does not return any entries. */
541 /* TODO(jbakker): We should consider introducing its own function to detect if it is a lib and
542 * call it directly from `filelist_readjob_do` to increase readability. */
543 const bool is_lib = BKE_blendfile_library_path_explode(root, dir, &group, nullptr);
544 if (!is_lib) {
545 return std::nullopt;
546 }
547
548 /* The root path contains an ID group (e.g. "Materials" or "Objects"). */
549 const bool has_group = group != nullptr;
550
551 /* Try read from indexer_runtime. */
552 /* Indexing returns all entries in a blend file. We should ignore the index when listing a group
553 * inside a blend file, so the `entries` isn't filled with undesired entries.
554 * This happens when linking or appending data-blocks, where you can navigate into a group (ie
555 * Materials/Objects) where you only want to work with partial indexes.
556 *
557 * Adding support for partial reading/updating indexes would increase the complexity.
558 */
559 const bool use_indexer = !has_group;
560 FileIndexerEntries indexer_entries = {nullptr};
561 if (use_indexer) {
562 int read_from_index = 0;
563 eFileIndexerResult indexer_result = indexer_runtime->callbacks->read_index(
564 dir, &indexer_entries, &read_from_index, indexer_runtime->user_data);
565 if (indexer_result == FILE_INDEXER_ENTRIES_LOADED) {
567 job_params, entries, options, read_from_index, &indexer_entries);
568 ED_file_indexer_entries_clear(&indexer_entries);
569 return entries_read;
570 }
571 }
572
573 /* Open the library file. */
574 BlendFileReadReport bf_reports{};
575 libfiledata = BLO_blendhandle_from_file(dir, &bf_reports);
576 if (libfiledata == nullptr) {
577 return std::nullopt;
578 }
579
580 /* Add current parent when requested. */
581 /* Is the navigate to previous level added to the list of entries. When added the return value
582 * should be increased to match the actual number of entries added. It is introduced to keep
583 * the code clean and readable and not counting in a single variable. */
584 int navigate_to_parent_len = 0;
587 job_params);
588 BLI_addtail(entries, entry);
589 navigate_to_parent_len = 1;
590 }
591
592 int group_len = 0;
593 int datablock_len = 0;
594 /* Read only the datablocks from this group. */
595 if (has_group) {
596 const int idcode = groupname_to_code(group);
598 libfiledata, idcode, options & LIST_LIB_ASSETS_ONLY, &datablock_len);
600 job_params, entries, datablock_infos, false, idcode, group);
601 BLO_datablock_info_linklist_free(datablock_infos);
602 }
603 /* Read all datablocks from all groups. */
604 else {
605 LinkNode *groups = BLO_blendhandle_get_linkable_groups(libfiledata);
606 group_len = BLI_linklist_count(groups);
607
608 for (LinkNode *ln = groups; ln; ln = ln->next) {
609 const char *group_name = static_cast<char *>(ln->link);
610 const int idcode = groupname_to_code(group_name);
612 job_params, idcode, group_name);
613 BLI_addtail(entries, group_entry);
614
616 int group_datablock_len;
617 LinkNode *group_datablock_infos = BLO_blendhandle_get_datablock_info(
618 libfiledata, idcode, options & LIST_LIB_ASSETS_ONLY, &group_datablock_len);
620 job_params, entries, group_datablock_infos, true, idcode, group_name);
621 if (use_indexer) {
623 &indexer_entries, group_datablock_infos, idcode);
624 }
625 BLO_datablock_info_linklist_free(group_datablock_infos);
626 datablock_len += group_datablock_len;
627 }
628 }
629
630 BLI_linklist_freeN(groups);
631 }
632
633 BLO_blendhandle_close(libfiledata);
634
635 /* Update the index. */
636 if (use_indexer) {
637 indexer_runtime->callbacks->update_index(dir, &indexer_entries, indexer_runtime->user_data);
638 ED_file_indexer_entries_clear(&indexer_entries);
639 }
640
641 /* Return the number of items added to entries. */
642 int added_entries_len = group_len + datablock_len + navigate_to_parent_len;
643 return added_entries_len;
644}
645
646static bool filelist_readjob_should_recurse_into_entry(const int max_recursion,
647 const bool is_lib,
648 const int current_recursion_level,
649 FileListInternEntry *entry)
650{
651 if (max_recursion == 0) {
652 /* Recursive loading is disabled. */
653 return false;
654 }
655 if (!is_lib && current_recursion_level > max_recursion) {
656 /* No more levels of recursion left. */
657 return false;
658 }
659 /* Show entries when recursion is set to `Blend file` even when `current_recursion_level`
660 * exceeds `max_recursion`. */
661 if (!is_lib && (current_recursion_level >= max_recursion) &&
663 {
664 return false;
665 }
666 if (entry->typeflag & FILE_TYPE_BLENDERLIB) {
667 /* Libraries are already loaded recursively when recursive loaded is used. No need to add
668 * them another time. This loading is done with the `LIST_LIB_RECURSIVE` option. */
669 return false;
670 }
671 if (!(entry->typeflag & FILE_TYPE_DIR)) {
672 /* Cannot recurse into regular file entries. */
673 return false;
674 }
675 if (FILENAME_IS_CURRPAR(entry->relpath)) {
676 /* Don't schedule go to parent entry, (`..`) */
677 return false;
678 }
679
680 return true;
681}
682
684 FileListReadJob *job_params,
685 const bool *stop,
686 bool *do_update,
687 float *progress)
688{
689 FileList *filelist = job_params->tmp_filelist; /* Use the thread-safe filelist queue. */
690 ListBaseT<FileListInternEntry> entries = {nullptr};
691 BLI_Stack *todo_dirs;
692 TodoDir *td_dir;
693 char dir[FILE_MAX_LIBEXTRA];
694 char filter_glob[FILE_MAXFILE];
695 const char *root = filelist->filelist.root;
696 const int max_recursion = filelist->max_recursion;
697 int dirs_done_count = 0, dirs_todo_count = 1;
698
699 /* The code below assumes the root ends in a slash. It's also not just the code below; weird
700 * things happen when it doesn't end in a slash. Better to just enforce it. */
701 BLI_assert_msg(StringRef(filelist->filelist.root).endswith(SEP_STR), filelist->filelist.root);
702
703 todo_dirs = BLI_stack_new(sizeof(*td_dir), __func__);
704 td_dir = static_cast<TodoDir *>(BLI_stack_push_r(todo_dirs));
705 td_dir->level = 1;
706
707 STRNCPY(dir, filelist->filelist.root);
708 STRNCPY(filter_glob, filelist->filter_glob);
709
710 BLI_path_abs(dir, job_params->main_filepath);
711 BLI_path_normalize_dir(dir, sizeof(dir));
712 td_dir->dir = BLI_strdup(dir);
713
714 /* Init the file indexer. */
715 FileIndexer indexer_runtime{};
716 indexer_runtime.callbacks = filelist->indexer;
717 if (indexer_runtime.callbacks->init_user_data) {
718 indexer_runtime.user_data = indexer_runtime.callbacks->init_user_data(dir, sizeof(dir));
719 }
720
721 while (!BLI_stack_is_empty(todo_dirs) && !(*stop)) {
722 int entries_num = 0;
723
724 char *subdir;
725 char rel_subdir[FILE_MAX_LIBEXTRA];
726 int recursion_level;
727 bool skip_currpar;
728
729 td_dir = static_cast<TodoDir *>(BLI_stack_peek(todo_dirs));
730 subdir = td_dir->dir;
731 recursion_level = td_dir->level;
732 skip_currpar = (recursion_level > 1);
733
734 BLI_stack_discard(todo_dirs);
735
736 /* ARRRG! We have to be very careful *not to use* common `BLI_path_utils.hh` helpers over
737 * entry->relpath itself (nor any path containing it), since it may actually be a datablock
738 * name inside .blend file, which can have slashes and backslashes! See #46827.
739 * Note that in the end, this means we 'cache' valid relative subdir once here,
740 * this is actually better. */
741 STRNCPY(rel_subdir, subdir);
742 BLI_path_abs(rel_subdir, root);
743 BLI_path_normalize_dir(rel_subdir, sizeof(rel_subdir));
744 BLI_path_rel(rel_subdir, root);
745
746 /* Update the current relative base path within the filelist root. */
747 STRNCPY(job_params->cur_relbase, rel_subdir);
748
749 bool is_lib = false;
750 if (do_lib) {
751 ListLibOptions list_lib_options = LIST_LIB_OPTION_NONE;
752 if (!skip_currpar) {
753 list_lib_options |= LIST_LIB_ADD_PARENT;
754 }
755
756 /* Libraries are loaded recursively when max_recursion is set. It doesn't check if there is
757 * still a recursion level over. */
758 if (max_recursion > 0) {
759 list_lib_options |= LIST_LIB_RECURSIVE;
760 }
761 /* Only load assets when browsing an asset library. For normal file browsing we return all
762 * entries. `FLF_ASSETS_ONLY` filter can be enabled/disabled by the user. */
763 if (job_params->load_asset_library) {
764 list_lib_options |= LIST_LIB_ASSETS_ONLY;
765 }
766 std::optional<int> lib_entries_num = filelist_readjob_list_lib(
767 job_params, subdir, &entries, list_lib_options, &indexer_runtime);
768 if (lib_entries_num) {
769 is_lib = true;
770 entries_num += *lib_entries_num;
771 }
772 }
773
774 if (!is_lib && BLI_is_dir(subdir)) {
775 entries_num = filelist_readjob_list_dir(job_params,
776 subdir,
777 &entries,
778 filter_glob,
779 do_lib,
780 job_params->main_filepath,
781 skip_currpar);
782 }
783
784 for (FileListInternEntry &entry : entries) {
785 entry.uid = filelist_uid_generate(filelist);
786 if (!entry.name) {
787 entry.name = fileentry_uiname(root, &entry, dir);
788 }
789 entry.free_name = true;
790
792 max_recursion, is_lib, recursion_level, &entry))
793 {
794 /* We have a directory we want to list, add it to todo list!
795 * Using #BLI_path_join works but isn't needed as `root` has a trailing slash. */
796 BLI_string_join(dir, sizeof(dir), root, entry.relpath);
797 BLI_path_abs(dir, job_params->main_filepath);
798 BLI_path_normalize_dir(dir, sizeof(dir));
799 td_dir = static_cast<TodoDir *>(BLI_stack_push_r(todo_dirs));
800 td_dir->level = recursion_level + 1;
801 td_dir->dir = BLI_strdup(dir);
802 dirs_todo_count++;
803 }
804 }
805
806 if (filelist_readjob_append_entries(job_params, &entries, entries_num)) {
807 *do_update = true;
808 }
809
810 dirs_done_count++;
811 *progress = float(dirs_done_count) / float(dirs_todo_count);
812 MEM_delete(subdir);
813 }
814
815 /* Finalize and free indexer. */
816 if (indexer_runtime.callbacks->filelist_finished && BLI_stack_is_empty(todo_dirs)) {
817 indexer_runtime.callbacks->filelist_finished(indexer_runtime.user_data);
818 }
819 if (indexer_runtime.callbacks->free_user_data && indexer_runtime.user_data) {
820 indexer_runtime.callbacks->free_user_data(indexer_runtime.user_data);
821 indexer_runtime.user_data = nullptr;
822 }
823
824 /* If we were interrupted by stop, stack may not be empty and we need to free
825 * pending dir paths. */
826 while (!BLI_stack_is_empty(todo_dirs)) {
827 td_dir = static_cast<TodoDir *>(BLI_stack_peek(todo_dirs));
828 MEM_delete(td_dir->dir);
829 BLI_stack_discard(todo_dirs);
830 }
831 BLI_stack_free(todo_dirs);
832}
833
835 FileListReadJob *job_params,
836 const bool *stop,
837 bool *do_update,
838 float *progress)
839{
840 FileList *filelist = job_params->tmp_filelist; /* Use the thread-safe filelist queue. */
841
842 // BLI_assert(filelist->filtered == nullptr);
843 BLI_assert(filelist->filelist.entries.is_empty() &&
845
846 /* A valid, but empty directory from now. */
847 filelist->filelist.entries_num = 0;
848
849 filelist_readjob_recursive_dir_add_items(do_lib, job_params, stop, do_update, progress);
850}
851
853
854/* -------------------------------------------------------------------- */
857
862{
864
865 FileList *filelist = job_params->tmp_filelist; /* Use the thread-safe filelist queue. */
866 *do_update = false;
867
868 /* See if loading is necessary (and then load). */
869 const bool is_force_reload = job_params->reload_asset_library;
870 if (!filelist->asset_library || is_force_reload) {
871 filelist->asset_library = AS_asset_library_load(job_params->current_main,
872 *job_params->filelist->asset_library_ref);
873 job_params->reload_asset_library = false;
874 *do_update = true;
875 }
876
877 /* Not really necessary for this function to do, but otherwise it's up to the caller, and can be
878 * forgotten. */
879 job_params->load_asset_library = filelist->asset_library;
880}
881
883
884} // namespace blender
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define FILENAME_IS_CURRPAR(_n)
#define FILENAME_PARENT
#define FILE_MAXDIR
#define BLI_path_cmp
#define BLI_string_join(...)
Compatibility-like things for windows.
#define S_ISDIR(x)
#define BLO_GROUP_MAX
#define FILE_MAX_LIBEXTRA
volatile int lock
#define ARRAY_SET_ITEMS(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
constexpr StringRef()
eAssetLibraryType library_type() const
std::weak_ptr< AssetRepresentation > add_external_on_disk_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata)
std::weak_ptr< AssetRepresentation > add_external_online_asset(StringRef relative_asset_path, StringRef name, int id_type, std::unique_ptr< AssetMetaData > metadata, OnlineAssetInfo online_info)
CCL_NAMESPACE_BEGIN struct Options options
#define FILEDIR_NBR_ENTRIES_UNSET
ENUM_OPERATORS(Layer::Flags)
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
LinkNode * BLO_blendhandle_get_datablock_info(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_info_items)
void BLO_datablock_info_linklist_free(LinkNode *datablock_infos)
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:253
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:99
int ED_path_extension_type(const char *path)
Definition filelist.cc:1852
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:94
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
int BLI_path_normalize_dir(char *dir, size_t dir_maxncpy) ATTR_NONNULL(1)
static std::optional< int > filelist_readjob_list_lib(FileListReadJob *job_params, const char *root, ListBaseT< FileListInternEntry > *entries, const ListLibOptions options, FileIndexer *indexer_runtime)
void ED_file_indexer_entries_clear(FileIndexerEntries *indexer_entries)
bool filelist_checkdir_dir(const FileList *, char dirpath[FILE_MAX_LIBEXTRA], const bool do_change)
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
void BLI_stack_discard(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:179
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:239
void * BLI_stack_push_r(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:106
char * current_relpath_append(const FileListReadJob *job_params, const char *filename)
static void parent_dir_until_exists_or_default_root(char *dir)
@ FILE_ATTR_HIDDEN
@ FILE_ATTR_READONLY
@ FILE_ATTR_ALIAS
bool filelist_checkdir_return_always_valid(const FileList *, char[1024+258], const bool)
void BLI_linklist_freeN(LinkNode *list)
void filelist_entry_glob_tag(FileListInternEntry *entry, const char *filter_glob)
Definition filelist.cc:1158
static void filelist_readjob_list_lib_add_datablocks(FileListReadJob *job_params, ListBaseT< FileListInternEntry > *entries, LinkNode *datablock_infos, const bool prefix_relpath_with_group_name, const int idcode, const char *group_name)
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void ED_file_indexer_entries_extend_from_datablock_infos(FileIndexerEntries *indexer_entries, LinkNode *datablock_infos, int idcode)
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool filelist_checkdir_lib(const FileList *, char dirpath[FILE_MAX_LIBEXTRA], const bool do_change)
void filelist_readjob_load_asset_library_data(FileListReadJob *job_params, bool *do_update)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
const char * fileentry_uiname(const char *root, FileListInternEntry *entry, char *buff)
Definition filelist.cc:1073
asset_system::AssetLibrary * AS_asset_library_load(const Main *bmain, const AssetLibraryReference &library_reference)
static bool filelist_readjob_should_recurse_into_entry(const int max_recursion, const bool is_lib, const int current_recursion_level, FileListInternEntry *entry)
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:172
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
void filelist_readjob_list_lib_add_datablock(FileListReadJob *job_params, ListBaseT< FileListInternEntry > *entries, BLODataBlockInfo *datablock_info, const bool prefix_relpath_with_group_name, const int idcode, const char *group_name, const std::optional< asset_system::OnlineAssetInfo > online_asset_info=std::nullopt)
static FileListInternEntry * filelist_readjob_list_lib_navigate_to_parent_entry_create(const FileListReadJob *job_params)
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
static int filelist_readjob_list_dir(FileListReadJob *job_params, const char *root, ListBaseT< FileListInternEntry > *entries, const char *filter_glob, const bool do_lib, const char *main_filepath, const bool skip_currpar)
static void filelist_readjob_list_lib_add_from_indexer_entries(FileListReadJob *job_params, ListBaseT< FileListInternEntry > *entries, const FileIndexerEntries *indexer_entries, const bool prefix_relpath_with_group_name)
static int groupname_to_code(const char *group)
@ FILE_INDEXER_ENTRIES_LOADED
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
FileUID filelist_uid_generate(FileList *filelist)
Definition filelist.cc:1422
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
char * BLF_display_name_from_file(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition blf.cc:1115
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
LinkNode * BLO_blendhandle_get_linkable_groups(BlendHandle *bh)
bool BKE_appdir_font_folder_default(char *dir, size_t dir_maxncpy)
Definition appdir.cc:234
@ FILE_TYPE_ASSET_ONLINE
@ FILE_TYPE_BLENDER_BACKUP
bool BKE_blendfile_extension_check(const char *str)
Definition blendfile.cc:88
void filelist_readjob_recursive_dir_add_items(const bool do_lib, FileListReadJob *job_params, const bool *stop, bool *do_update, float *progress)
bool BLI_path_has_hidden_component(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition idtype.cc:282
int BLI_linklist_count(const LinkNode *list) ATTR_WARN_UNUSED_RESULT
bool BLI_file_alias_target(const char *filepath, char r_targetpath[768]) ATTR_WARN_UNUSED_RESULT
void filelist_readjob_directories_and_libraries(const bool do_lib, FileListReadJob *job_params, const bool *stop, bool *do_update, float *progress)
bool BLI_path_parent_dir_until_exists(char *path) ATTR_NONNULL(1)
BLI_Stack * BLI_stack_new(size_t elem_size, const char *description) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:85
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:469
const char * BLI_path_slash_rfind(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool filelist_readjob_append_entries(FileListReadJob *job_params, ListBaseT< FileListInternEntry > *from_entries, int from_entries_num)
static int filelist_readjob_list_lib_populate_from_index(FileListReadJob *job_params, ListBaseT< FileListInternEntry > *entries, const ListLibOptions options, const int read_from_index, const FileIndexerEntries *indexer_entries)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static FileListInternEntry * filelist_readjob_list_lib_group_create(const FileListReadJob *job_params, const int idcode, const char *group_name)
void BLI_windows_get_default_root_dir(char root_dir[4])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< struct FileListInternEntry > entries
BLODataBlockInfo datablock_info
FileIndexerReadIndexFunc read_index
FileIndexerFinishedFunc filelist_finished
FileIndexerUpdateIndexFunc update_index
FileIndexerFreeUserDataFunc free_user_data
FileIndexerInitUserDataFunc init_user_data
const FileIndexerType * callbacks
std::weak_ptr< asset_system::AssetRepresentation > asset
asset_system::AssetRepresentation * get_asset() const
std::atomic< bool > reload_asset_library
asset_system::AssetLibrary * load_asset_library
std::optional< std::function< void(asset_system::AssetRepresentation &)> > on_asset_added
AssetLibraryReference * asset_library_ref
FileDirEntryArr filelist
const FileIndexerType * indexer
char filter_glob[FILE_MAXFILE]
asset_system::AssetLibrary * asset_library
struct LinkNode * next
#define SEP_STR
Definition unit.cc:41