Skip to content

Commit 5fa6dec

Browse files
committed
fixup! src: allow blobs instead of FILE*s in embedder snapshot API
1 parent e16e66e commit 5fa6dec

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

src/api/embed_helpers.cc

+2-9
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,15 @@ EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromBlob(
304304
}
305305

306306
EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromFile(FILE* in) {
307-
SnapshotData* snapshot_data = new SnapshotData();
308-
CHECK_EQ(snapshot_data->data_ownership, SnapshotData::DataOwnership::kOwned);
309-
EmbedderSnapshotData::Pointer result{
310-
new EmbedderSnapshotData(snapshot_data, true)};
311-
if (!SnapshotData::FromBlob(snapshot_data, in)) {
312-
return {};
313-
}
314-
return result;
307+
return FromBlob(ReadFileSync(in));
315308
}
316309

317310
std::vector<char> EmbedderSnapshotData::ToBlob() const {
318311
return impl_->ToBlob();
319312
}
320313

321314
void EmbedderSnapshotData::ToFile(FILE* out) const {
322-
impl_->ToBlob(out);
315+
impl_->ToFile(out);
323316
}
324317

325318
EmbedderSnapshotData::EmbedderSnapshotData(const SnapshotData* impl,

src/env.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,12 @@ struct SnapshotData {
517517
// v8::ScriptCompiler::CachedData is not copyable.
518518
std::vector<builtins::CodeCacheInfo> code_cache;
519519

520-
void ToBlob(FILE* out) const;
520+
void ToFile(FILE* out) const;
521521
std::vector<char> ToBlob() const;
522522
// If returns false, the metadata doesn't match the current Node.js binary,
523523
// and the caller should not consume the snapshot data.
524524
bool Check() const;
525-
static bool FromBlob(SnapshotData* out, FILE* in);
525+
static bool FromFile(SnapshotData* out, FILE* in);
526526
static bool FromBlob(SnapshotData* out, const std::vector<char>& in);
527527
static const SnapshotData* FromEmbedderWrapper(
528528
const EmbedderSnapshotData* data);

src/node.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
11461146

11471147
FILE* fp = fopen(snapshot_blob_path.c_str(), "wb");
11481148
if (fp != nullptr) {
1149-
(*snapshot_data_ptr)->ToBlob(fp);
1149+
(*snapshot_data_ptr)->ToFile(fp);
11501150
fclose(fp);
11511151
} else {
11521152
fprintf(stderr,
@@ -1174,7 +1174,7 @@ ExitCode LoadSnapshotDataAndRun(const SnapshotData** snapshot_data_ptr,
11741174
return exit_code;
11751175
}
11761176
std::unique_ptr<SnapshotData> read_data = std::make_unique<SnapshotData>();
1177-
if (!SnapshotData::FromBlob(read_data.get(), fp)) {
1177+
if (!SnapshotData::FromFile(read_data.get(), fp)) {
11781178
// If we fail to read the customized snapshot, simply exit with 1.
11791179
// TODO(joyeecheung): should be kStartupSnapshotFailure.
11801180
exit_code = ExitCode::kGenericUserError;

src/node_snapshotable.cc

+3-13
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ std::vector<char> SnapshotData::ToBlob() const {
862862
return w.sink;
863863
}
864864

865-
void SnapshotData::ToBlob(FILE* out) const {
865+
void SnapshotData::ToFile(FILE* out) const {
866866
const std::vector<char> sink = ToBlob();
867867
size_t num_written = fwrite(sink.data(), sink.size(), 1, out);
868868
CHECK_EQ(num_written, 1);
@@ -877,18 +877,8 @@ EmbedderSnapshotData::Pointer SnapshotData::AsEmbedderWrapper() const {
877877
return EmbedderSnapshotData::Pointer{new EmbedderSnapshotData(this, false)};
878878
}
879879

880-
bool SnapshotData::FromBlob(SnapshotData* out, FILE* in) {
881-
CHECK_EQ(ftell(in), 0);
882-
int err = fseek(in, 0, SEEK_END);
883-
CHECK_EQ(err, 0);
884-
size_t size = ftell(in);
885-
err = fseek(in, 0, SEEK_SET);
886-
CHECK_EQ(err, 0);
887-
888-
std::vector<char> sink(size);
889-
size_t num_read = fread(sink.data(), size, 1, in);
890-
CHECK_EQ(num_read, 1);
891-
return FromBlob(out, sink);
880+
bool SnapshotData::FromFile(SnapshotData* out, FILE* in) {
881+
return FromBlob(out, ReadFileSync(in));
892882
}
893883

894884
bool SnapshotData::FromBlob(SnapshotData* out, const std::vector<char>& in) {

src/util.cc

+14
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ int ReadFileSync(std::string* result, const char* path) {
267267
return 0;
268268
}
269269

270+
std::vector<char> ReadFileSync(FILE* fp) {
271+
CHECK_EQ(ftell(fp), 0);
272+
int err = fseek(fp, 0, SEEK_END);
273+
CHECK_EQ(err, 0);
274+
size_t size = ftell(fp);
275+
err = fseek(fp, 0, SEEK_SET);
276+
CHECK_EQ(err, 0);
277+
278+
std::vector<char> contents(size);
279+
size_t num_read = fread(contents.data(), size, 1, fp);
280+
CHECK_EQ(num_read, 1);
281+
return contents;
282+
}
283+
270284
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
271285
#ifdef _WIN32
272286
GetLocalTime(tm_struct);

src/util.h

+2
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr) {
867867
// Returns a non-zero code if it fails to open or read the file,
868868
// aborts if it fails to close the file.
869869
int ReadFileSync(std::string* result, const char* path);
870+
// Reads all contents of a FILE*, aborts if it fails.
871+
std::vector<char> ReadFileSync(FILE* fp);
870872

871873
v8::Local<v8::FunctionTemplate> NewFunctionTemplate(
872874
v8::Isolate* isolate,

0 commit comments

Comments
 (0)