MCPcopy Create free account
hub / github.com/Niko1221/Strata / write_impl

Function write_impl

engine/src/core/conversation_file.cpp:1102–1176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1100
1101namespace {
1102bool write_impl(const std::string& path, const SavedConversation& image, const std::vector<SessionKvSource>* sources,
1103 const SessionFileIdentity& id, size_t& bytes, std::string& error, const SessionWriteOptions& opt,
1104 SessionStatus& st) {
1105 st = {};
1106 st.error = SessionError::invalid; // refusals before any disk access
1107 if (has_stage_parts(image)) { error = "session file: layer-split state cannot be saved"; return false; }
1108 if (sources && !image.kv.empty()) { error = "session file: K/V given both in the image and as sources"; return false; }
1109 Out sizing;
1110 put_payload(sizing, image, sources);
1111 if (sizing.overflow || sizing.n > UINT64_MAX - kHeader - kTrailer ||
1112 sizing.n + kHeader + kTrailer > (uint64_t) std::numeric_limits<size_t>::max()) {
1113 error = "session file: the state is too large for this build";
1114 return false;
1115 }
1116 const uint64_t payload = sizing.n, total = kHeader + payload + kTrailer;
1117 st.error = SessionError::io;
1118 if (opt.min_free_bytes) {
1119 uint64_t avail = 0;
1120 if (!free_space(path, avail)) { error = "session file: cannot read the free disk space for " + path; return false; }
1121 if (avail < total || avail - total < opt.min_free_bytes) {
1122 st.error = SessionError::storage;
1123 error = "session file: not enough disk space (" + std::to_string(total >> 20) + " MiB plus a reserve of " +
1124 std::to_string(opt.min_free_bytes >> 20) + " MiB needed, " + std::to_string(avail >> 20) +
1125 " MiB free)";
1126 return false;
1127 }
1128 }
1129 Publisher pub;
1130 if (!pub.open(path, error)) { st.error = pub.kind(); return false; }
1131 RawFile raw;
1132 if (!pub.create_temp(raw, error)) {
1133 st.error = raw.kind();
1134 error = "session file: cannot create a temporary file beside " + path + ": " + error;
1135 return false;
1136 }
1137 FileSink f(raw);
1138 if (!f.ok()) { st.error = SessionError::memory; error = "session file: " + f.error(); return false; }
1139 if (opt.progress) f.progress.emplace(opt.progress);
1140 f.phase = opt.phase;
1141 f.expected = total;
1142 if (f.progress) (*f.progress)(0, total); // the temporary file exists: the transfer starts
1143 const std::function<int(const char*)>* fault = opt.fault ? &opt.fault : nullptr;
1144 f.write_fault = injected(fault, "write");
1145 f.flush_fault = injected(fault, "file_flush");
1146 uint8_t h[kHeader];
1147 header_bytes(h, id, payload);
1148 bool ok = f.write(h, kHeader);
1149 SessionHasher hash(0);
1150 Out out;
1151 out.f = &f;
1152 out.hash = &hash;
1153 if (ok) put_payload(out, image, sources);
1154 ok = ok && out.ok && out.n == payload;
1155 if (ok) {
1156 const uint64_t ph = hash.digest();
1157 ok = f.write(&ph, 8) && f.write(kEnd, 8);
1158 }
1159 ok = f.finish(opt.durable) && ok;

Callers 1

write_guardedFunction · 0.85

Calls 15

has_stage_partsFunction · 0.85
put_payloadFunction · 0.85
free_spaceFunction · 0.85
injectedFunction · 0.85
header_bytesFunction · 0.85
create_tempMethod · 0.80
maxFunction · 0.50
to_stringFunction · 0.50
stringFunction · 0.50
emptyMethod · 0.45
openMethod · 0.45
kindMethod · 0.45

Tested by

no test coverage detected