载入中...
搜索中...
未找到
BakedAssetFile.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <atomic>
4#include <filesystem>
5#include <fstream>
6#include <limits>
7#include <random>
8#if defined(__linux__)
9#include <fcntl.h>
10#include <unistd.h>
11#include <cerrno>
12#endif
13namespace eve::physics {
14std::string bakedAssetContentKey(std::string_view source) {
15 std::uint64_t a = 14695981039346656037ull, b = 0x9e3779b97f4a7c15ull;
16 for (unsigned char c : source) {
17 a = (a ^ c) * 1099511628211ull;
18 b = (b ^ c) * 0x100000001b3ull;
19 b ^= b >> 29;
20 }
21 constexpr char digits[] = "0123456789abcdef";
22 std::string key(32, '0');
23 for (unsigned i = 0; i < 16; ++i) {
24 key[i] = digits[(a >> ((15 - i) * 4)) & 15];
25 key[16 + i] = digits[(b >> ((15 - i) * 4)) & 15];
26 }
27 return key;
28}
29
30namespace {
31// The directory is exclusively created before opening its child. Cleanup never touches the destination.
32struct TemporaryAnimation {
33 std::filesystem::path directory;
34 ~TemporaryAnimation() {
35 if (directory.empty()) return;
36 std::error_code ignored;
37 std::filesystem::remove(directory / "clip", ignored);
38 std::filesystem::remove(directory, ignored);
39 }
40};
41#if defined(__linux__)
42// Linux descriptors pin the directory across publication and are never retained by the clip.
43struct PublicationDescriptor {
44 int value = -1;
45 ~PublicationDescriptor() {
46 if (value >= 0) ::close(value);
47 }
48};
49Result<void> syncPublicationDescriptor(int descriptor, std::string_view path, std::string_view publication) {
50 int result;
51 do {
52 result = ::fsync(descriptor);
53 } while (result < 0 && errno == EINTR);
54 if (result < 0)
55 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Failed, "animation durability barrier failed",
56 std::string(path),
57 {{"publication", std::string(publication)},
58 {"durability", "unconfirmed"},
59 {"system_error", std::to_string(errno)}}));
60 return Result<void>::success();
61}
62#endif
63} // namespace
64Result<void> publishBakedAsset(std::string_view path, std::string_view bytes) {
65 if (path.empty() || path.find('\0') != std::string_view::npos)
67 Diagnostic::error(DiagnosticCode::InvalidArgument, "asset path is empty or contains NUL"));
68 const std::filesystem::path target(path);
69 if (target.filename().empty())
71#if defined(__linux__)
72 const auto parent = target.parent_path().empty() ? std::filesystem::path(".") : target.parent_path();
73 PublicationDescriptor directory{::open(parent.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)};
74 if (directory.value < 0)
76 Diagnostic::error(DiagnosticCode::Failed, "cannot open asset publication directory", std::string(path)));
77 // No visible staging name: the kernel reclaims unpublished data even after SIGKILL.
78 PublicationDescriptor data{::openat(directory.value, ".", O_TMPFILE | O_WRONLY | O_CLOEXEC, 0666)};
79 if (data.value < 0) {
80 const int error = errno;
81 const auto code = error == EOPNOTSUPP || error == EINVAL || error == EISDIR ? DiagnosticCode::Unsupported
83 return Result<void>::failure(Diagnostic::error(code, "anonymous asset staging unavailable", std::string(path),
84 {{"system_error", std::to_string(error)}}));
85 }
86 std::size_t offset = 0;
87 while (offset < bytes.size()) {
88 const auto count =
89 std::min(bytes.size() - offset, static_cast<std::size_t>(std::numeric_limits<ssize_t>::max()));
90 const auto written = ::write(data.value, bytes.data() + offset, count);
91 if (written < 0 && errno == EINTR) continue;
92 if (written <= 0)
94 Diagnostic::error(DiagnosticCode::Failed, "asset staging write failed", std::string(path)));
95 offset += static_cast<std::size_t>(written);
96 }
97 auto synced = syncPublicationDescriptor(data.value, path, "not_published");
98 if (!synced) return synced;
99 // procfs descriptor linking works without CAP_DAC_READ_SEARCH (unlike AT_EMPTY_PATH).
100 const auto source = "/proc/self/fd/" + std::to_string(data.value);
101 if (::linkat(AT_FDCWD, source.c_str(), directory.value, target.filename().c_str(), AT_SYMLINK_FOLLOW) < 0) {
102 const int error = errno;
105 "asset publication failed (requires procfs descriptor links and a new path)",
106 std::string(path), {{"system_error", std::to_string(error)}}));
107 }
108 // Never roll back a complete target that readers may already have observed.
109 return syncPublicationDescriptor(directory.value, path, "published");
110#else
111 static std::atomic<std::uint64_t> sequence{0};
112 TemporaryAnimation temporary;
113 // This entropy names temporary storage only; it never enters simulation or content identity.
114 std::random_device entropy;
115 for (unsigned attempt = 0; attempt < 16; ++attempt) {
116 const auto name = ".eve-collapse-" + std::to_string(entropy()) + "-" +
117 std::to_string(sequence.fetch_add(1, std::memory_order_relaxed));
118 auto candidate = target.parent_path() / name;
119 std::error_code error;
120 if (std::filesystem::create_directory(candidate, error)) {
121 temporary.directory = std::move(candidate);
122 break;
123 }
124 if (error && error != std::errc::file_exists)
126 DiagnosticCode::Failed, "cannot create animation temporary directory", std::string(path)));
127 }
128 if (temporary.directory.empty())
130 DiagnosticCode::Conflict, "cannot reserve animation temporary directory", std::string(path)));
131 const auto staging = temporary.directory / "clip";
132 std::ofstream file(staging, std::ios::binary);
133 file.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
134 file.close();
135 if (!file)
137 Diagnostic::error(DiagnosticCode::Failed, "animation temporary write failed", std::string(path)));
138 std::error_code error;
139 std::filesystem::create_hard_link(staging, target, error);
140 if (error)
142 error == std::errc::file_exists ? DiagnosticCode::Conflict : DiagnosticCode::Failed,
143 "animation publication failed (requires same-filesystem hard links and a new path)", std::string(path)));
144 return Result<void>::success();
145#endif
146}
147} // namespace eve::physics
LogicalId target
std::string descriptor
std::filesystem::path directory
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
DiagnosticCode code
std::int32_t c
std::function< void(b3JointId, const Numbers &)> write
const ColourScope * parent
size_t offset
Poco::JSON::Object::Ptr result
std::uint64_t bytes
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::unique_ptr< gpgpu::Sequence > sequence
Definition OnnxGpgpu.cpp:43
std::string error
Definition Package.cpp:174
std::string path
Definition PlayHost.cpp:111
std::string file
Object::Ptr value
std::string_view source
int count
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
std::string bakedAssetContentKey(std::string_view source)
Stable noncryptographic 128-bit content digest, encoded as 32 lowercase hexadecimal bytes....
Result< void > publishBakedAsset(std::string_view path, std::string_view bytes)
Publish complete baked asset bytes atomically without replacing an existing path. @ownership Borrows ...