[Bf-blender-cvs] [c71e856b4d4] asset-system-filelist: Initial framework and basic reading of file-lists

Severin noreply at git.blender.org
Mon May 10 19:04:59 CEST 2021


Commit: c71e856b4d4a45033aa35b05f4f2bcf1ea14db7b
Author: Severin
Date:   Mon May 10 18:43:11 2021 +0200
Branches: asset-system-filelist
https://developer.blender.org/rBc71e856b4d4a45033aa35b05f4f2bcf1ea14db7b

Initial framework and basic reading of file-lists

Initial steps for T88184.

Adds a `filelist/` directory to `editors/`, containing C++ classes for
(recursively) reading directories and files into a file-list. The logic is
based on the File-Browser file-list, so that the actually executed logic is
basically the same (avoiding regressions or old issues re-appearing). But it
is entirely re-designed to work for both the asset system and the file browser.

For more info about the design, see T88184.

Also contains "peeking" the file list to count the files to be read. This is to
improve progress reporting later. However, it has some issues and I'm not sure
actually it's something we'll even need in the end, I see other ways to improve
things here.

===================================================================

M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/intern/BLI_filelist.c
M	source/blender/editors/CMakeLists.txt
M	source/blender/editors/asset/CMakeLists.txt
M	source/blender/editors/asset/asset_list.cc
A	source/blender/editors/filelist/CMakeLists.txt
A	source/blender/editors/filelist/file_list.cc
A	source/blender/editors/filelist/file_list.hh
A	source/blender/editors/filelist/file_list_entry.cc
A	source/blender/editors/filelist/file_list_entry.hh
A	source/blender/editors/filelist/file_list_reader.cc
A	source/blender/editors/filelist/file_list_reader.hh

===================================================================

diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index df80e720363..51edea39d05 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -94,7 +94,10 @@ typedef enum eFileAttributes {
   FILE_ATTR_JUNCTION_POINT = 1 << 13, /* Folder Symbolic-link. */
   FILE_ATTR_MOUNT_POINT = 1 << 14,    /* Volume mounted as a folder. */
   FILE_ATTR_HARDLINK = 1 << 15,       /* Duplicated directory entry. */
+
+  FILE_ATTR_LAST
 } eFileAttributes;
+ENUM_OPERATORS(eFileAttributes, FILE_ATTR_LAST);
 
 #define FILE_ATTR_ANY_LINK \
   (FILE_ATTR_ALIAS | FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYMLINK | FILE_ATTR_JUNCTION_POINT | \
@@ -114,7 +117,15 @@ eFileAttributes BLI_file_attributes(const char *path);
 
 /* Filelist */
 
+typedef bool (*FileListPeekFn)(const char *filepath,
+                               const char *filename,
+                               const BLI_stat_t *stat,
+                               void *customdata);
+
 unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist);
+void BLI_filelist_dir_contents_iterate_peek(const char *dirname,
+                                            FileListPeekFn peek_fn,
+                                            void *customdata);
 void BLI_filelist_entry_duplicate(struct direntry *dst, const struct direntry *src);
 void BLI_filelist_duplicate(struct direntry **dest_filelist,
                             struct direntry *const src_filelist,
diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c
index cc01693422c..174381d71ec 100644
--- a/source/blender/blenlib/intern/BLI_filelist.c
+++ b/source/blender/blenlib/intern/BLI_filelist.c
@@ -256,6 +256,45 @@ unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_
   return dir_ctx.nrfiles;
 }
 
+/**
+ * Iterate over files in a directory, doing minimal work. The callback \a
+ * peek_fn gets invoked for each file and gets basic file information.
+ * \note The file name string passed to the call back will be destroyed right after the callback
+ *       returns.
+ */
+void BLI_filelist_dir_contents_iterate_peek(const char *dirname,
+                                            FileListPeekFn peek_fn,
+                                            void *customdata)
+{
+  DIR *dir;
+  if ((dir = opendir(dirname)) != NULL) {
+    const struct dirent *fname;
+
+    while ((fname = readdir(dir)) != NULL) {
+      BLI_stat_t stat = {0};
+      char fullname[PATH_MAX];
+      BLI_join_dirfile(fullname, sizeof(fullname), dirname, fname->d_name);
+
+      if (BLI_stat(fullname, &stat) != -1) {
+        /* Pass. */
+      }
+      else if (FILENAME_IS_CURRPAR(fname->d_name)) {
+        /* Hack around for UNC paths on windows:
+         * does not support stat on '\\SERVER\foo\..', sigh... */
+        stat.st_mode |= S_IFDIR;
+      }
+
+      if (!peek_fn(fullname, fname->d_name, &stat, customdata)) {
+        break;
+      }
+    }
+    closedir(dir);
+  }
+  else {
+    printf("failed to open %s\n", dirname);
+  }
+}
+
 /**
  * Convert given entry's size into human-readable strings.
  */
diff --git a/source/blender/editors/CMakeLists.txt b/source/blender/editors/CMakeLists.txt
index 092198cea86..b1fb4763d80 100644
--- a/source/blender/editors/CMakeLists.txt
+++ b/source/blender/editors/CMakeLists.txt
@@ -24,6 +24,7 @@ if(WITH_BLENDER)
   add_subdirectory(armature)
   add_subdirectory(asset)
   add_subdirectory(curve)
+  add_subdirectory(filelist)
   add_subdirectory(geometry)
   add_subdirectory(gizmo_library)
   add_subdirectory(gpencil)
diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/asset/CMakeLists.txt
index d87879bea2a..cd2714ecaeb 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/asset/CMakeLists.txt
@@ -17,6 +17,7 @@
 
 set(INC
     ../include
+    ../filelist
     ../../blenkernel
     ../../blenlib
     ../../blenloader
@@ -38,6 +39,9 @@ set(SRC
 set(LIB
   bf_blenloader
   bf_blenkernel
+
+  bf_editor_filelist
 )
 
 blender_add_lib(bf_editor_asset "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
+
diff --git a/source/blender/editors/asset/asset_list.cc b/source/blender/editors/asset/asset_list.cc
index 378d12442b3..4b6583ae323 100644
--- a/source/blender/editors/asset/asset_list.cc
+++ b/source/blender/editors/asset/asset_list.cc
@@ -23,6 +23,7 @@
  * contains most necessary logic already and there's not much time for a more long-term solution.
  */
 
+#include <memory>
 #include <optional>
 #include <string>
 
@@ -48,6 +49,9 @@
 #include "WM_api.h"
 #include "WM_types.h"
 
+#include "../filelist/file_list.hh"
+#include "../filelist/file_list_entry.hh"
+
 /* XXX uses private header of file-space. */
 #include "../space_file/filelist.h"
 
@@ -158,6 +162,8 @@ class AssetList : NonCopyable {
   AssetLibraryReference library_ref_;
   PreviewTimer previews_timer_;
 
+  std::unique_ptr<ed::filelist::AbstractFileList> cpp_filelist_ = nullptr;
+
  public:
   AssetList() = delete;
   AssetList(eFileSelectType filesel_type, const AssetLibraryReference &asset_library_ref);
@@ -230,12 +236,19 @@ void AssetList::setup(const AssetFilterSettings *filter_settings)
   else {
     filelist_setdir(files, path);
   }
+
+  /* -------------------------------------------------------------------- */
+
+  ed::filelist::FileListReadParams read_params(path);
+  cpp_filelist_ = std::make_unique<ed::filelist::FileList>(read_params);
 }
 
 void AssetList::fetch(const bContext &C)
 {
   FileList *files = filelist_;
 
+  cpp_filelist_->fetch();
+
   if (filelist_needs_force_reset(files)) {
     filelist_readjob_stop(files, CTX_wm_manager(&C));
     filelist_clear(files);
diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/filelist/CMakeLists.txt
similarity index 77%
copy from source/blender/editors/asset/CMakeLists.txt
copy to source/blender/editors/filelist/CMakeLists.txt
index d87879bea2a..51976e06840 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/filelist/CMakeLists.txt
@@ -15,14 +15,11 @@
 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 # ***** END GPL LICENSE BLOCK *****
 
+
 set(INC
     ../include
-    ../../blenkernel
     ../../blenlib
-    ../../blenloader
     ../../makesdna
-    ../../makesrna
-    ../../windowmanager
     ../../../../intern/guardedalloc
 )
 
@@ -30,14 +27,17 @@ set(INC_SYS
 )
 
 set(SRC
-  asset_edit.cc
-  asset_list.cc
-  asset_ops.cc
+  file_list.cc
+  file_list_entry.cc
+  file_list_reader.cc
+
+  file_list.hh
+  file_list_entry.hh
+  file_list_reader.hh
 )
 
 set(LIB
-  bf_blenloader
-  bf_blenkernel
 )
 
-blender_add_lib(bf_editor_asset "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
+blender_add_lib(bf_editor_filelist "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
+set_target_properties(bf_editor_filelist PROPERTIES LINKER_LANGUAGE CXX)
diff --git a/source/blender/editors/filelist/file_list.cc b/source/blender/editors/filelist/file_list.cc
new file mode 100644
index 00000000000..ffc5d4483ae
--- /dev/null
+++ b/source/blender/editors/filelist/file_list.cc
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+#include <filesystem>
+#include <iostream>
+#include <string>
+
+#include "BLI_fileops.h"
+#include "BLI_fileops_types.h"
+#include "BLI_path_util.h"
+#include "BLI_string_ref.hh"
+
+#include "file_list.hh"
+#include "file_list_entry.hh"
+#include "file_list_reader.hh"
+
+namespace blender::ed::filelist {
+
+FileList::FileList(const FileListReadParams &read_params) : read_params_(read_params)
+{
+}
+
+void print_dir(FileTree &file_list)
+{
+  for (const auto &file : file_list) {
+    std::cout << file->relative_file_path() << std::endl;
+    if (DirectoryEntry *dir = dynamic_cast<DirectoryEntry *>(file.get())) {
+      print_dir(dir->children());
+    }
+  }
+  std::cout << std::endl;
+}
+
+void FileList::fetch()
+{
+  std::cout << "fetch() " << read_params_.path_ << std::endl;
+
+  RecursiveFileListReader reader(read_params_);
+
+  int64_t tot_files = reader.peekAndCountFiles();
+  std::cout << tot_files << std::endl;
+
+  reader.read(file_tree_);
+
+  print_dir(file_tree_);
+}
+
+}  // namespace blender::ed::filelist
diff --git a/source/blender/editors/filelist/file_list.hh b/source/blender/editors/filelist/file_list.hh
new file mode 100644
index 00000000000..68d6e274444
--- /dev/null
+++ b/source/blender/editors/filelist/file_list.hh
@@ -0,0 +1,70 @@
+/*
+ * 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.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * \brief High-level API for reading and storing different types of files.
+ */
+
+/* TODO define public and internal API. */
+
+#pragma once
+
+#include <optional>
+
+#include "BLI_vector.hh"
+
+namespace blender::ed::filelist {
+
+class Abst

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list