mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-02-17 04:27:08 +00:00
close file descriptor (#248)
This commit is contained in:
parent
c5920c4ef4
commit
db8f42507a
@ -1,9 +1,6 @@
|
|||||||
#include <inttypes.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <iostream>
|
||||||
#include <sstream> // std::ostringstream
|
#include <sstream> // std::ostringstream
|
||||||
#include <algorithm> // std::copy
|
|
||||||
#include <iterator> // std::ostream_iterator
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <napi.h>
|
#include <napi.h>
|
||||||
#include "c_kzg_4844.h"
|
#include "c_kzg_4844.h"
|
||||||
@ -132,22 +129,40 @@ inline Bytes48 *get_bytes48(const Napi::Env &env, const Napi::Value &val, std::s
|
|||||||
|
|
||||||
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
|
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
|
|
||||||
|
// Check if the trusted setup is already loaded
|
||||||
KzgAddonData *data = env.GetInstanceData<KzgAddonData>();
|
KzgAddonData *data = env.GetInstanceData<KzgAddonData>();
|
||||||
if (data->is_setup) {
|
if (data->is_setup) {
|
||||||
Napi::Error::New(env, "kzg bindings are already setup").ThrowAsJavaScriptException();
|
Napi::Error::New(env, "Error trusted setup is already loaded").ThrowAsJavaScriptException();
|
||||||
return env.Undefined();
|
return env.Undefined();
|
||||||
}
|
}
|
||||||
// the validation checks for this happen in JS
|
|
||||||
const std::string file_path = info[0].As<Napi::String>().Utf8Value();
|
// Open the trusted setup file
|
||||||
|
std::string file_path = info[0].As<Napi::String>().Utf8Value();
|
||||||
FILE *file_handle = fopen(file_path.c_str(), "r");
|
FILE *file_handle = fopen(file_path.c_str(), "r");
|
||||||
if (file_handle == NULL) {
|
if (file_handle == nullptr) {
|
||||||
Napi::Error::New(env, "Error opening trusted setup file: " + file_path).ThrowAsJavaScriptException();
|
Napi::Error::New(env, "Error opening trusted setup file: " + file_path).ThrowAsJavaScriptException();
|
||||||
return env.Undefined();
|
return env.Undefined();
|
||||||
}
|
}
|
||||||
if (load_trusted_setup_file(&(data->settings), file_handle) != C_KZG_OK) {
|
|
||||||
Napi::Error::New(env, "Error loading trusted setup file: " + file_path).ThrowAsJavaScriptException();
|
// Load the trusted setup from that file
|
||||||
return env.Undefined();
|
C_KZG_RET ret = load_trusted_setup_file(&(data->settings), file_handle);
|
||||||
|
|
||||||
|
// Close the trusted setup file
|
||||||
|
if (fclose(file_handle) != 0) {
|
||||||
|
if (ret == C_KZG_OK) {
|
||||||
|
free_trusted_setup(&(data->settings));
|
||||||
|
}
|
||||||
|
Napi::Error::New(env, "Error closing trusted setup file").ThrowAsJavaScriptException();
|
||||||
|
return env.Undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check that it was successful
|
||||||
|
if (ret != C_KZG_OK) {
|
||||||
|
Napi::Error::New(env, "Error loading trusted setup file: " + file_path).ThrowAsJavaScriptException();
|
||||||
|
return env.Undefined();
|
||||||
|
}
|
||||||
|
|
||||||
data->is_setup = true;
|
data->is_setup = true;
|
||||||
return env.Undefined();
|
return env.Undefined();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user