From 05243a420676ff83835d461eb3fc1eb07e9cd38f Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 24 Jul 2023 14:48:27 +0200 Subject: [PATCH] BKE_file: Add util to check if a given blend file is readable. `BKE_blendfile_is_readable` attempts to open the given file path, and return `true` on success, `false` on failure. Part of #109151 (PR !110109). --- source/blender/blenkernel/BKE_blendfile.h | 7 +++++++ source/blender/blenkernel/intern/blendfile.cc | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/source/blender/blenkernel/BKE_blendfile.h b/source/blender/blenkernel/BKE_blendfile.h index 827b03d46a9..2a9ea2127ed 100644 --- a/source/blender/blenkernel/BKE_blendfile.h +++ b/source/blender/blenkernel/BKE_blendfile.h @@ -48,6 +48,13 @@ bool BKE_blendfile_library_path_explode(const char *path, char **r_group, char **r_name); +/** + * Check whether a given path is actually a Blender-readable, valid .blend file. + * + * \note Currently does attempt to open and read (part of) the given file. + */ +bool BKE_blendfile_is_readable(const char *path, struct ReportList *reports); + /** * Shared setup function that makes the data from `bfd` into the current blend file, * replacing the contents of #G.main. diff --git a/source/blender/blenkernel/intern/blendfile.cc b/source/blender/blenkernel/intern/blendfile.cc index 1eb3c5ff425..148cb3ae78a 100644 --- a/source/blender/blenkernel/intern/blendfile.cc +++ b/source/blender/blenkernel/intern/blendfile.cc @@ -142,6 +142,18 @@ bool BKE_blendfile_library_path_explode(const char *path, return true; } +bool BKE_blendfile_is_readable(const char *path, ReportList *reports) +{ + BlendFileReadReport readfile_reports; + readfile_reports.reports = reports; + BlendHandle *bh = BLO_blendhandle_from_file(path, &readfile_reports); + if (bh != nullptr) { + BLO_blendhandle_close(bh); + return true; + } + return false; +} + /** \} */ /* -------------------------------------------------------------------- */