Blender V5.0
path_util.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#include "IO_path_util.hh"
5
6#include "BLI_fileops.h"
7#include "BLI_path_utils.hh"
8#include "BLI_string.h"
9
10#include "CLG_log.h"
11static CLG_LogRef LOG = {"io.common"};
12
13namespace blender::io {
14
15std::string path_reference(StringRefNull filepath,
16 StringRefNull base_src,
17 StringRefNull base_dst,
19 Set<std::pair<std::string, std::string>> *copy_set)
20{
21 const bool is_relative = BLI_path_is_rel(filepath.c_str());
22 char filepath_abs[PATH_MAX];
23 STRNCPY(filepath_abs, filepath.c_str());
24 BLI_path_abs(filepath_abs, base_src.c_str());
25 BLI_path_normalize(filepath_abs);
26
27 /* Figure out final mode to be used. */
28 if (mode == PATH_REFERENCE_MATCH) {
30 }
31 else if (mode == PATH_REFERENCE_AUTO) {
32 mode = BLI_path_contains(base_dst.c_str(), filepath_abs) ? PATH_REFERENCE_RELATIVE :
34 }
35 else if (mode == PATH_REFERENCE_COPY) {
36 char filepath_cpy[PATH_MAX];
37 BLI_path_join(filepath_cpy, PATH_MAX, base_dst.c_str(), BLI_path_basename(filepath_abs));
38 copy_set->add(std::make_pair(filepath_abs, filepath_cpy));
39 STRNCPY(filepath_abs, filepath_cpy);
41 }
42
43 /* Now we know the final path mode. */
44 if (mode == PATH_REFERENCE_ABSOLUTE) {
45 return filepath_abs;
46 }
47 if (mode == PATH_REFERENCE_RELATIVE) {
48 char rel_path[PATH_MAX];
49 STRNCPY(rel_path, filepath_abs);
50 BLI_path_rel(rel_path, base_dst.c_str());
51 /* Can't always find relative path (e.g. between different drives). */
52 if (!BLI_path_is_rel(rel_path)) {
53 return filepath_abs;
54 }
55 return rel_path + 2; /* Skip blender's internal "//" prefix. */
56 }
57 if (mode == PATH_REFERENCE_STRIP) {
58 return BLI_path_basename(filepath_abs);
59 }
60 BLI_assert_msg(false, "Invalid path reference mode");
61 return filepath_abs;
62}
63
64void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_set)
65{
66 for (const auto &copy : copy_set) {
67 const char *src = copy.first.c_str();
68 const char *dst = copy.second.c_str();
69 if (!BLI_exists(src)) {
70 CLOG_WARN(&LOG, "Missing source file '%s', not copying", src);
71 continue;
72 }
73 if (0 == BLI_path_cmp_normalized(src, dst)) {
74 continue; /* Source and destination are the same. */
75 }
77 CLOG_WARN(&LOG, "Can't make directory for '%s', not copying", dst);
78 continue;
79 }
80 if (BLI_copy(src, dst) != 0) {
81 CLOG_WARN(&LOG, "Can't copy '%s' to '%s'", src, dst);
82 continue;
83 }
84 }
85}
86
87} // namespace blender::io
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:360
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
#define PATH_MAX
Definition BLI_fileops.h:26
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:452
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
#define BLI_path_join(...)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ePathReferenceMode
@ PATH_REFERENCE_AUTO
@ PATH_REFERENCE_RELATIVE
@ PATH_REFERENCE_COPY
@ PATH_REFERENCE_MATCH
@ PATH_REFERENCE_ABSOLUTE
@ PATH_REFERENCE_STRIP
constexpr const char * c_str() const
#define LOG(level)
Definition log.h:97
std::string path_reference(StringRefNull filepath, StringRefNull base_src, StringRefNull base_dst, ePathReferenceMode mode, Set< std::pair< std::string, std::string > > *copy_set)
Definition path_util.cc:15
void path_reference_copy(const Set< std::pair< std::string, std::string > > &copy_set)
Definition path_util.cc:64
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)