diff --git a/src/api/embed_helpers.cc b/src/api/embed_helpers.cc index 04c1aa10c8cac0..3f8463f6ae518d 100644 --- a/src/api/embed_helpers.cc +++ b/src/api/embed_helpers.cc @@ -304,14 +304,7 @@ EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromBlob( } EmbedderSnapshotData::Pointer EmbedderSnapshotData::FromFile(FILE* in) { - SnapshotData* snapshot_data = new SnapshotData(); - CHECK_EQ(snapshot_data->data_ownership, SnapshotData::DataOwnership::kOwned); - EmbedderSnapshotData::Pointer result{ - new EmbedderSnapshotData(snapshot_data, true)}; - if (!SnapshotData::FromBlob(snapshot_data, in)) { - return {}; - } - return result; + return FromBlob(ReadFileSync(in)); } std::vector EmbedderSnapshotData::ToBlob() const { @@ -319,7 +312,7 @@ std::vector EmbedderSnapshotData::ToBlob() const { } void EmbedderSnapshotData::ToFile(FILE* out) const { - impl_->ToBlob(out); + impl_->ToFile(out); } EmbedderSnapshotData::EmbedderSnapshotData(const SnapshotData* impl, diff --git a/src/env.h b/src/env.h index ee823e7f7bacfe..c2eb764e740400 100644 --- a/src/env.h +++ b/src/env.h @@ -517,12 +517,12 @@ struct SnapshotData { // v8::ScriptCompiler::CachedData is not copyable. std::vector code_cache; - void ToBlob(FILE* out) const; + void ToFile(FILE* out) const; std::vector ToBlob() const; // If returns false, the metadata doesn't match the current Node.js binary, // and the caller should not consume the snapshot data. bool Check() const; - static bool FromBlob(SnapshotData* out, FILE* in); + static bool FromFile(SnapshotData* out, FILE* in); static bool FromBlob(SnapshotData* out, const std::vector& in); static const SnapshotData* FromEmbedderWrapper( const EmbedderSnapshotData* data); diff --git a/src/node.cc b/src/node.cc index ed6b1c7a1a736d..4957256c02b5ab 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1146,7 +1146,7 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr, FILE* fp = fopen(snapshot_blob_path.c_str(), "wb"); if (fp != nullptr) { - (*snapshot_data_ptr)->ToBlob(fp); + (*snapshot_data_ptr)->ToFile(fp); fclose(fp); } else { fprintf(stderr, @@ -1174,7 +1174,7 @@ ExitCode LoadSnapshotDataAndRun(const SnapshotData** snapshot_data_ptr, return exit_code; } std::unique_ptr read_data = std::make_unique(); - if (!SnapshotData::FromBlob(read_data.get(), fp)) { + if (!SnapshotData::FromFile(read_data.get(), fp)) { // If we fail to read the customized snapshot, simply exit with 1. // TODO(joyeecheung): should be kStartupSnapshotFailure. exit_code = ExitCode::kGenericUserError; diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index dfa5597d5d97a0..1a19f44bec8198 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -862,7 +862,7 @@ std::vector SnapshotData::ToBlob() const { return w.sink; } -void SnapshotData::ToBlob(FILE* out) const { +void SnapshotData::ToFile(FILE* out) const { const std::vector sink = ToBlob(); size_t num_written = fwrite(sink.data(), sink.size(), 1, out); CHECK_EQ(num_written, 1); @@ -877,18 +877,8 @@ EmbedderSnapshotData::Pointer SnapshotData::AsEmbedderWrapper() const { return EmbedderSnapshotData::Pointer{new EmbedderSnapshotData(this, false)}; } -bool SnapshotData::FromBlob(SnapshotData* out, FILE* in) { - CHECK_EQ(ftell(in), 0); - int err = fseek(in, 0, SEEK_END); - CHECK_EQ(err, 0); - size_t size = ftell(in); - err = fseek(in, 0, SEEK_SET); - CHECK_EQ(err, 0); - - std::vector sink(size); - size_t num_read = fread(sink.data(), size, 1, in); - CHECK_EQ(num_read, 1); - return FromBlob(out, sink); +bool SnapshotData::FromFile(SnapshotData* out, FILE* in) { + return FromBlob(out, ReadFileSync(in)); } bool SnapshotData::FromBlob(SnapshotData* out, const std::vector& in) { diff --git a/src/util.cc b/src/util.cc index 8675d9fab14a65..c74b32137164f5 100644 --- a/src/util.cc +++ b/src/util.cc @@ -267,6 +267,20 @@ int ReadFileSync(std::string* result, const char* path) { return 0; } +std::vector ReadFileSync(FILE* fp) { + CHECK_EQ(ftell(fp), 0); + int err = fseek(fp, 0, SEEK_END); + CHECK_EQ(err, 0); + size_t size = ftell(fp); + err = fseek(fp, 0, SEEK_SET); + CHECK_EQ(err, 0); + + std::vector contents(size); + size_t num_read = fread(contents.data(), size, 1, fp); + CHECK_EQ(num_read, 1); + return contents; +} + void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) { #ifdef _WIN32 GetLocalTime(tm_struct); diff --git a/src/util.h b/src/util.h index d50fea060e2b83..d0e4bd41ec92c8 100644 --- a/src/util.h +++ b/src/util.h @@ -867,6 +867,8 @@ std::unique_ptr static_unique_pointer_cast(std::unique_ptr&& ptr) { // Returns a non-zero code if it fails to open or read the file, // aborts if it fails to close the file. int ReadFileSync(std::string* result, const char* path); +// Reads all contents of a FILE*, aborts if it fails. +std::vector ReadFileSync(FILE* fp); v8::Local NewFunctionTemplate( v8::Isolate* isolate,