Blender  V3.3
path_util.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #include "IO_path_util.hh"
3 
4 #include "BLI_fileops.h"
5 #include "BLI_path_util.h"
6 
7 namespace blender::io {
8 
9 std::string path_reference(StringRefNull filepath,
10  StringRefNull base_src,
11  StringRefNull base_dst,
12  ePathReferenceMode mode,
13  Set<std::pair<std::string, std::string>> *copy_set)
14 {
15  const bool is_relative = BLI_path_is_rel(filepath.c_str());
16  char filepath_abs[PATH_MAX];
17  BLI_strncpy(filepath_abs, filepath.c_str(), PATH_MAX);
18  BLI_path_abs(filepath_abs, base_src.c_str());
19  BLI_path_normalize(nullptr, filepath_abs);
20 
21  /* Figure out final mode to be used. */
22  if (mode == PATH_REFERENCE_MATCH) {
23  mode = is_relative ? PATH_REFERENCE_RELATIVE : PATH_REFERENCE_ABSOLUTE;
24  }
25  else if (mode == PATH_REFERENCE_AUTO) {
26  mode = BLI_path_contains(base_dst.c_str(), filepath_abs) ? PATH_REFERENCE_RELATIVE :
28  }
29  else if (mode == PATH_REFERENCE_COPY) {
30  char filepath_cpy[PATH_MAX];
32  filepath_cpy, PATH_MAX, base_dst.c_str(), BLI_path_basename(filepath_abs), nullptr);
33  copy_set->add(std::make_pair(filepath_abs, filepath_cpy));
34  BLI_strncpy(filepath_abs, filepath_cpy, PATH_MAX);
36  }
37 
38  /* Now we know the final path mode. */
39  if (mode == PATH_REFERENCE_ABSOLUTE) {
40  return filepath_abs;
41  }
42  if (mode == PATH_REFERENCE_RELATIVE) {
43  char rel_path[PATH_MAX];
44  BLI_strncpy(rel_path, filepath_abs, PATH_MAX);
45  BLI_path_rel(rel_path, base_dst.c_str());
46  /* Can't always find relative path (e.g. between different drives). */
47  if (!BLI_path_is_rel(rel_path)) {
48  return filepath_abs;
49  }
50  return rel_path + 2; /* Skip blender's internal "//" prefix. */
51  }
52  if (mode == PATH_REFERENCE_STRIP) {
53  return BLI_path_basename(filepath_abs);
54  }
55  BLI_assert_msg(false, "Invalid path reference mode");
56  return filepath_abs;
57 }
58 
59 void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_set)
60 {
61  for (const auto &copy : copy_set) {
62  const char *src = copy.first.c_str();
63  const char *dst = copy.second.c_str();
64  if (!BLI_exists(src)) {
65  fprintf(stderr, "Missing source file '%s', not copying\n", src);
66  continue;
67  }
68  if (0 == BLI_path_cmp_normalized(src, dst)) {
69  continue; /* Source and dest are the same. */
70  }
71  if (!BLI_make_existing_file(dst)) {
72  fprintf(stderr, "Can't make directory for '%s', not copying\n", dst);
73  continue;
74  }
75  if (!BLI_copy(src, dst)) {
76  fprintf(stderr, "Can't copy '%s' to '%s'\n", src, dst);
77  continue;
78  }
79  }
80 }
81 
82 } // 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.c:314
int BLI_copy(const char *file, const char *to) ATTR_NONNULL()
Definition: fileops.c:1198
#define PATH_MAX
Definition: BLI_fileops.h:29
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1653
bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:347
bool BLI_make_existing_file(const char *name)
Definition: path_util.c:1197
void BLI_path_normalize(const char *relabase, char *path) ATTR_NONNULL(2)
Definition: path_util.c:131
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1717
size_t BLI_path_join(char *__restrict dst, size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL()
Definition: path_util.c:450
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:897
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
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
SyclQueue void void * src
void path_reference_copy(const Set< std::pair< std::string, std::string >> &copy_set)
Definition: path_util.cc:59
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:9
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)