Blender V5.3
disk_file_hash_service.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
12
13#include "BKE_idprop.hh"
14
15#ifdef WITH_PYTHON
16# include "BPY_extern_run.hh"
17#endif
18
19#include "CLG_log.h"
20
21static CLG_LogRef LOG = {"assets.disk_file_hash_service"};
22
23namespace blender::asset_system {
24
25std::unique_ptr<DiskFileHashService> disk_file_hash_service_get(const StringRef storage_path)
26{
27 return std::make_unique<DiskFileHashService>(storage_path);
28}
29
31 : storage_path_(storage_path)
32{
33}
34
36{
37 release_python();
38}
39
40void DiskFileHashService::release_python()
41{
42#ifdef WITH_PYTHON
43 constexpr const char *SCRIPT = R"(
44import _bpy_internal.disk_file_hash_service as dfhs
45from pathlib import Path
46
47dfhs.release_service(Path(storage_path))
48)";
49 std::unique_ptr locals = bke::idprop::create_group("locals");
50 IDP_AddToGroup(locals.get(), IDP_NewString(this->storage_path_, "storage_path"));
51
52 BPY_run_string_exec_with_locals(nullptr, SCRIPT, *locals);
53#endif
54}
55
56std::string DiskFileHashService::get_hash(const StringRef filepath, const StringRef hash_algorithm)
57{
58#ifdef WITH_PYTHON
59 /* NOTE: this is a somewhat inefficient implementation for frequently-repeated calls, as each
60 * call repeats the calls to `dfhs.get_service(Path(...))`. However, this does mean that the C++
61 * wrapper does not have to retain any references to Python objects itself, avoiding reference
62 * counting bugs. If the performance starts to matter, do the lookup of the service itself once,
63 * and cache the result. */
64 constexpr const char *SCRIPT = R"(
65import _bpy_internal.disk_file_hash_service as dfhs
66from pathlib import Path
67
68service = dfhs.get_service(Path(storage_path))
69_result = service.get_hash(Path(filepath), hash_algorithm)
70)";
71
72 /* Local variables for the script. */
73 std::unique_ptr locals = bke::idprop::create_group("locals");
74 IDP_AddToGroup(locals.get(), IDP_NewString(this->storage_path_, "storage_path"));
75 IDP_AddToGroup(locals.get(), IDP_NewString(filepath, "filepath"));
76 IDP_AddToGroup(locals.get(), IDP_NewString(hash_algorithm, "hash_algorithm"));
77
78 /* Run the script. */
79 std::optional<IDProperty *> idprop_optptr = BPY_run_string_exec_with_locals_return_idprop(
80 nullptr, SCRIPT, *locals, "_result");
81 if (!idprop_optptr.has_value()) {
82 const std::string filepath_str = filepath;
83 CLOG_ERROR(&LOG, "Failed to run hash script for file [%s].", filepath_str.c_str());
84 return "";
85 }
86 IDProperty *hash_idprop = *idprop_optptr;
87
88 /* Check the returned value. */
89 if (hash_idprop == nullptr || hash_idprop->type != IDP_STRING) {
90 IDP_FreeProperty(hash_idprop);
91 const std::string filepath_str = filepath;
93 "Hash for file [%s] was not returned as string. Please report this as a bug.",
94 filepath_str.c_str());
95 return "";
96 }
97
98 const std::string hash_value(IDP_string_get(hash_idprop));
99 IDP_FreeProperty(hash_idprop);
100
101 return hash_value;
102#else
103 UNUSED_VARS(C, hash_algorithm);
104 const std::string filepath_str = filepath;
106 "Blender was built without Python support, cannot compute hash for file [%s]",
107 filepath_str.c_str());
108 return "";
109#endif
110}
111
113 const StringRef hash_algorithm,
114 const StringRef hexhash,
115 const int64_t size_in_bytes)
116{
117#ifdef WITH_PYTHON
118 /* NOTE: this is a somewhat inefficient implementation for frequently-repeated calls, as each
119 * call repeats the calls to `dfhs.get_service(Path(...))`. However, this does mean that the C++
120 * wrapper does not have to retain any references to Python objects itself, avoiding reference
121 * counting bugs. If the performance starts to matter, do the lookup of the service itself once,
122 * and cache the result. */
123 constexpr const char *SCRIPT = R"(
124import _bpy_internal.disk_file_hash_service as dfhs
125from pathlib import Path
126
127# The '& 0xFFFFFFFF' makes Python interpret the values as unsigned ints.
128size_in_bytes = ((size_in_bytes_high & 0xFFFFFFFF) << 32) | (size_in_bytes_low & 0xFFFFFFFF)
129
130service = dfhs.get_service(Path(storage_path))
131_result = service.file_matches(Path(filepath), hash_algorithm, hexhash, size_in_bytes);
132)";
133
134 /* Since IDProperties don't support 64-bit integers, split it up into two 32-bit integers, and do
135 * bit shifting in Python to get the value back. */
136 BLI_assert(size_in_bytes >= 0);
137 const int size_in_bytes_high = int((size_in_bytes >> 32) & 0xFFFFFFFF);
138 const int size_in_bytes_low = int(size_in_bytes & 0xFFFFFFFF);
139
140 std::unique_ptr locals = bke::idprop::create_group("locals");
141 IDP_AddToGroup(locals.get(), IDP_NewString(this->storage_path_, "storage_path"));
142 IDP_AddToGroup(locals.get(), IDP_NewString(filepath, "filepath"));
143 IDP_AddToGroup(locals.get(), IDP_NewString(hash_algorithm, "hash_algorithm"));
144 IDP_AddToGroup(locals.get(), IDP_NewString(hexhash, "hexhash"));
145 IDP_AddToGroup(locals.get(), IDP_NewInt(size_in_bytes_high, "size_in_bytes_high"));
146 IDP_AddToGroup(locals.get(), IDP_NewInt(size_in_bytes_low, "size_in_bytes_low"));
147
148 /* Run the script. */
149 std::optional<IDProperty *> idprop_optptr = BPY_run_string_exec_with_locals_return_idprop(
150 nullptr, SCRIPT, *locals, "_result");
151 if (!idprop_optptr.has_value()) {
152 const std::string filepath_str = filepath;
153 CLOG_ERROR(&LOG, "Failed to run hash match script for file [%s].", filepath_str.c_str());
154 return false;
155 }
156 IDProperty *is_match_idprop = *idprop_optptr;
157
158 /* Check the returned value. */
159 if (is_match_idprop == nullptr || is_match_idprop->type != IDP_BOOLEAN) {
160 IDP_FreeProperty(is_match_idprop);
161 const std::string filepath_str = filepath;
163 &LOG,
164 "Hash match check for file [%s] did not return a boolean. Please report this as a bug.",
165 filepath_str.c_str());
166 return false;
167 }
168
169 const bool is_match(IDP_bool_get(is_match_idprop));
170 IDP_FreeProperty(is_match_idprop);
171
172 return is_match;
173#else
174 UNUSED_VARS(C, hash_algorithm, hexhash, size_in_bytes);
175 const std::string filepath_str = filepath;
177 "Blender was built without Python support, cannot check hash for file [%s]",
178 filepath_str.c_str());
179 return false;
180#endif
181}
182
183} // namespace blender::asset_system
#define IDP_string_get(prop)
#define IDP_bool_get(prop)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS(...)
bool file_matches(StringRef filepath, StringRef hash_algorithm, StringRef hexhash, int64_t size_in_bytes)
std::string get_hash(StringRef filepath, StringRef hash_algorithm)
#define LOG(level)
Definition log.h:97
std::unique_ptr< DiskFileHashService > disk_file_hash_service_get(StringRef storage_path)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
bool BPY_run_string_exec_with_locals(bContext *C, StringRefNull script, IDProperty &locals)
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_NewString(const char *st, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:443
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
std::optional< IDProperty * > BPY_run_string_exec_with_locals_return_idprop(bContext *C, StringRefNull script, IDProperty &locals, StringRefNull result_var_name)
IDProperty * IDP_NewInt(int value, blender::StringRef name, eIDPropertyFlag flags={})
Definition idprop.cc:1240
eIDPropertyType type
Definition DNA_ID.h:145