mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
NOLINT added, changed using CHECKS to a function returning absl::Status
This commit is contained in:
parent
2276f5d48e
commit
ac7c1ee1ee
|
@ -15,9 +15,9 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "jsonnet_base_sandbox.h"
|
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||||
|
|
||||||
absl::Status JsonnetMain(std::string in_file, std::string out_file){
|
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
|
||||||
// Initialize sandbox.
|
// Initialize sandbox.
|
||||||
JsonnetBaseSandbox sandbox(in_file, out_file);
|
JsonnetBaseSandbox sandbox(in_file, out_file);
|
||||||
SAPI_RETURN_IF_ERROR(sandbox.Init())
|
SAPI_RETURN_IF_ERROR(sandbox.Init())
|
||||||
|
@ -25,23 +25,24 @@ absl::Status JsonnetMain(std::string in_file, std::string out_file){
|
||||||
JsonnetApi api(&sandbox);
|
JsonnetApi api(&sandbox);
|
||||||
|
|
||||||
// Initialize library's main structure.
|
// Initialize library's main structure.
|
||||||
SAPI_ASSIGN_OR_RETURN(JsonnetVm* jsonnet_vm, api.c_jsonnet_make());
|
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
|
||||||
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
|
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
|
||||||
|
|
||||||
// Read input file.
|
// Read input file.
|
||||||
std::string in_file_in_sandboxee(std::string("/input/") +
|
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||||
basename(&in_file[0]));
|
basename(&in_file[0]));
|
||||||
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
||||||
SAPI_ASSIGN_OR_RETURN(char* input, api.c_read_input(false, in_file_var.PtrBefore()));
|
SAPI_ASSIGN_OR_RETURN(char* input,
|
||||||
|
api.c_read_input(false, in_file_var.PtrBefore()));
|
||||||
|
|
||||||
// Process jsonnet data.
|
// Process jsonnet data.
|
||||||
sapi::v::RemotePtr input_pointer(input);
|
sapi::v::RemotePtr input_pointer(input);
|
||||||
sapi::v::Int error;
|
sapi::v::Int error;
|
||||||
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet(
|
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet(
|
||||||
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter()));
|
&vm_pointer, in_file_var.PtrBefore(),
|
||||||
|
&input_pointer, error.PtrAfter()));
|
||||||
CHECK(!error.GetValue())
|
CHECK(!error.GetValue())
|
||||||
<< "Jsonnet code evaluation failed: "
|
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
|
||||||
<< error.GetValue() << "\n"
|
|
||||||
<< "Make sure all files used by your jsonnet file are in the same "
|
<< "Make sure all files used by your jsonnet file are in the same "
|
||||||
"directory as your file.";
|
"directory as your file.";
|
||||||
|
|
||||||
|
@ -51,16 +52,18 @@ absl::Status JsonnetMain(std::string in_file, std::string out_file){
|
||||||
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
||||||
sapi::v::RemotePtr output_pointer(output);
|
sapi::v::RemotePtr output_pointer(output);
|
||||||
|
|
||||||
SAPI_ASSIGN_OR_RETURN(bool success, api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
bool success,
|
||||||
|
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
|
||||||
CHECK(success) << "Writing to output file failed: " << success;
|
CHECK(success) << "Writing to output file failed: " << success;
|
||||||
|
|
||||||
// Clean up.
|
// Clean up.
|
||||||
SAPI_ASSIGN_OR_RETURN(char* result, api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
|
SAPI_ASSIGN_OR_RETURN(char* result,
|
||||||
|
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
|
||||||
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
|
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
|
||||||
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
|
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
|
||||||
|
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "jsonnet_base_transaction.h"
|
#include "jsonnet_base_transaction.h" // NOLINT(build/include)
|
||||||
|
|
||||||
absl::Status JsonnetTransaction::Main() {
|
absl::Status JsonnetTransaction::Main() {
|
||||||
JsonnetApi api(sandbox());
|
JsonnetApi api(sandbox());
|
||||||
|
|
|
@ -28,7 +28,7 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
|
||||||
|
|
||||||
// We need only the input file here, not the whole input directory
|
// We need only the input file here, not the whole input directory
|
||||||
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||||
sandbox2::PolicyBuilder *) override {
|
sandbox2::PolicyBuilder*) override {
|
||||||
return sandbox2::PolicyBuilder()
|
return sandbox2::PolicyBuilder()
|
||||||
.AllowStaticStartup()
|
.AllowStaticStartup()
|
||||||
.AllowOpen()
|
.AllowOpen()
|
||||||
|
@ -51,70 +51,73 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
|
||||||
std::string out_file_;
|
std::string out_file_;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
|
||||||
|
// Initialize sandbox.
|
||||||
|
JsonnetSapiSandbox sandbox(in_file, out_file);
|
||||||
|
SAPI_RETURN_IF_ERROR(sandbox.Init())
|
||||||
|
|
||||||
|
JsonnetApi api(&sandbox);
|
||||||
|
|
||||||
|
// Initialize library's main structure.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
|
||||||
|
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
|
||||||
|
|
||||||
|
// Read input file.
|
||||||
|
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||||
|
basename(&in_file[0]));
|
||||||
|
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* input,
|
||||||
|
api.c_read_input(false, in_file_var.PtrBefore()));
|
||||||
|
|
||||||
|
// Process jsonnet data.
|
||||||
|
sapi::v::RemotePtr input_pointer(input);
|
||||||
|
sapi::v::Int error;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_fmt_snippet(
|
||||||
|
&vm_pointer, in_file_var.PtrBefore(),
|
||||||
|
&input_pointer, error.PtrAfter()));
|
||||||
|
|
||||||
|
CHECK(!error.GetValue()) << "Jsonnet code evaluation failed: "
|
||||||
|
<< error.GetValue() << "\n";
|
||||||
|
|
||||||
|
// Write data to file.
|
||||||
|
std::string out_file_in_sandboxee(std::string("/output/") +
|
||||||
|
basename(&out_file[0]));
|
||||||
|
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
||||||
|
sapi::v::RemotePtr output_pointer(output);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
bool success,
|
||||||
|
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
|
||||||
|
CHECK(success) << "Writing to output file failed: " << success;
|
||||||
|
|
||||||
|
// Clean up.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* result,
|
||||||
|
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
google::InitGoogleLogging(argv[0]);
|
google::InitGoogleLogging(argv[0]);
|
||||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||||
|
|
||||||
if (!(argc == 3)) {
|
if (!(argc == 3)) {
|
||||||
std::cerr << "Usage:\n"
|
std::cerr << "Usage:\n"
|
||||||
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
|
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
|
||||||
<< " absolute/path/to/OUTPUT.jsonnet\n";
|
<< " absolute/path/to/OUTPUT\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string in_file(argv[1]);
|
std::string in_file(argv[1]);
|
||||||
std::string out_file(argv[2]);
|
std::string out_file(argv[2]);
|
||||||
|
|
||||||
// Initialize sandbox.
|
absl::Status status = JsonnetMain(in_file, out_file);
|
||||||
JsonnetSapiSandbox sandbox(in_file, out_file);
|
if (!status.ok()) {
|
||||||
absl::Status status = sandbox.Init();
|
LOG(ERROR) << "Failed: " << status.ToString();
|
||||||
CHECK(status.ok()) << "Sandbox initialization failed " << status;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
JsonnetApi api(&sandbox);
|
|
||||||
|
|
||||||
// Initialize library's main structure.
|
|
||||||
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
|
|
||||||
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
|
|
||||||
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
|
|
||||||
<< jsonnet_vm.status();
|
|
||||||
|
|
||||||
// Read input file.
|
|
||||||
sapi::v::ConstCStr in_file_var(in_file.c_str());
|
|
||||||
sapi::StatusOr<char *> input =
|
|
||||||
api.c_read_input(false, in_file_var.PtrBefore());
|
|
||||||
CHECK(input.ok()) << "Reading input file failed " << input.status();
|
|
||||||
|
|
||||||
// Process jsonnet data.
|
|
||||||
sapi::v::RemotePtr input_pointer(input.value());
|
|
||||||
sapi::v::Int error;
|
|
||||||
sapi::StatusOr<char *> output = api.c_jsonnet_fmt_snippet(
|
|
||||||
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
|
|
||||||
CHECK(output.ok() && !error.GetValue())
|
|
||||||
<< "Jsonnet code evaluation failed: " << output.status() << " "
|
|
||||||
<< error.GetValue();
|
|
||||||
|
|
||||||
// Write data to file.
|
|
||||||
std::string out_file_in_sandboxee(std::string("/output/") +
|
|
||||||
basename(&out_file[0]));
|
|
||||||
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
|
||||||
sapi::v::RemotePtr output_pointer(output.value());
|
|
||||||
sapi::StatusOr<bool> success;
|
|
||||||
|
|
||||||
success = api.c_write_output_file(&output_pointer, out_file_var.PtrBefore());
|
|
||||||
CHECK(success.ok() && success.value())
|
|
||||||
<< "Writing to output file failed " << success.status() << " "
|
|
||||||
<< success.value();
|
|
||||||
|
|
||||||
// Clean up.
|
|
||||||
sapi::StatusOr<char *> result =
|
|
||||||
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0);
|
|
||||||
CHECK(result.ok()) << "JsonnetVm realloc failed: " << result.status();
|
|
||||||
|
|
||||||
status = api.c_jsonnet_destroy(&vm_pointer);
|
|
||||||
CHECK(status.ok()) << "JsonnetVm destroy failed: " << status;
|
|
||||||
|
|
||||||
status = api.c_free_input(&input_pointer);
|
|
||||||
CHECK(status.ok()) << "Input freeing failed: " << status;
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
|
||||||
|
|
||||||
// We need a slightly different policy than the default one
|
// We need a slightly different policy than the default one
|
||||||
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||||
sandbox2::PolicyBuilder *) override {
|
sandbox2::PolicyBuilder*) override {
|
||||||
return sandbox2::PolicyBuilder()
|
return sandbox2::PolicyBuilder()
|
||||||
.AllowStaticStartup()
|
.AllowStaticStartup()
|
||||||
.AllowOpen()
|
.AllowOpen()
|
||||||
|
@ -55,73 +55,74 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
|
||||||
std::string out_directory_;
|
std::string out_directory_;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
|
||||||
|
// Initialize sandbox.
|
||||||
|
JsonnetSapiSandbox sandbox(in_file, out_file);
|
||||||
|
SAPI_RETURN_IF_ERROR(sandbox.Init())
|
||||||
|
|
||||||
|
JsonnetApi api(&sandbox);
|
||||||
|
|
||||||
|
// Initialize library's main structure.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
|
||||||
|
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
|
||||||
|
|
||||||
|
// Read input file.
|
||||||
|
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||||
|
basename(&in_file[0]));
|
||||||
|
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* input,
|
||||||
|
api.c_read_input(false, in_file_var.PtrBefore()));
|
||||||
|
|
||||||
|
// Process jsonnet data.
|
||||||
|
sapi::v::RemotePtr input_pointer(input);
|
||||||
|
sapi::v::Int error;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_multi(
|
||||||
|
&vm_pointer, in_file_var.PtrBefore(),
|
||||||
|
&input_pointer, error.PtrAfter()));
|
||||||
|
CHECK(!error.GetValue())
|
||||||
|
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
|
||||||
|
<< "Make sure all files used by your jsonnet file are in the same "
|
||||||
|
"directory as your file.";
|
||||||
|
|
||||||
|
// Write data to file.
|
||||||
|
std::string out_file_in_sandboxee(std::string("/output/") +
|
||||||
|
basename(&out_file[0]));
|
||||||
|
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
||||||
|
sapi::v::RemotePtr output_pointer(output);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
bool success,
|
||||||
|
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
|
||||||
|
CHECK(success) << "Writing to output file failed: " << success;
|
||||||
|
|
||||||
|
// Clean up.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* result,
|
||||||
|
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
google::InitGoogleLogging(argv[0]);
|
google::InitGoogleLogging(argv[0]);
|
||||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||||
|
|
||||||
if (!(argc == 3)) {
|
if (!(argc == 3)) {
|
||||||
std::cerr << "Usage:\n"
|
std::cerr << "Usage:\n"
|
||||||
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
|
<< basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
|
||||||
<< " absolute/path/to/OUTPUT_DIRECTORY\n";
|
<< " absolute/path/to/OUTPUT\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string in_file(argv[1]);
|
std::string in_file(argv[1]);
|
||||||
std::string out_directory(argv[2]);
|
std::string out_file(argv[2]);
|
||||||
|
|
||||||
// Initialize sandbox.
|
absl::Status status = JsonnetMain(in_file, out_file);
|
||||||
JsonnetSapiSandbox sandbox(in_file, out_directory);
|
if (!status.ok()) {
|
||||||
absl::Status status = sandbox.Init();
|
LOG(ERROR) << "Failed: " << status.ToString();
|
||||||
CHECK(status.ok()) << "Sandbox initialization failed " << status;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
JsonnetApi api(&sandbox);
|
|
||||||
|
|
||||||
// Initialize library's main structure.
|
|
||||||
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
|
|
||||||
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
|
|
||||||
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
|
|
||||||
<< jsonnet_vm.status();
|
|
||||||
|
|
||||||
// Read input file.
|
|
||||||
std::string in_file_in_sandboxee(std::string("/input/") +
|
|
||||||
basename(&in_file[0]));
|
|
||||||
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
|
||||||
sapi::StatusOr<char *> input =
|
|
||||||
api.c_read_input(false, in_file_var.PtrBefore());
|
|
||||||
CHECK(input.ok()) << "Reading input file failed " << input.status();
|
|
||||||
|
|
||||||
// Process jsonnet data.
|
|
||||||
sapi::v::RemotePtr input_pointer(input.value());
|
|
||||||
sapi::v::Int error;
|
|
||||||
sapi::StatusOr<char *> output = api.c_jsonnet_evaluate_snippet_multi(
|
|
||||||
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
|
|
||||||
CHECK(output.ok() && !error.GetValue())
|
|
||||||
<< "Jsonnet code evaluation failed: " << output.status() << " "
|
|
||||||
<< error.GetValue() << "\n"
|
|
||||||
<< "Make sure all files used by your jsonnet file are in the same "
|
|
||||||
"directory as your file.";
|
|
||||||
|
|
||||||
// Write data to file.
|
|
||||||
std::string out_file_in_sandboxee(std::string("/output/"));
|
|
||||||
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
|
||||||
sapi::v::RemotePtr output_pointer(output.value());
|
|
||||||
sapi::StatusOr<bool> success = api.c_write_multi_output_files(
|
|
||||||
&output_pointer, out_file_var.PtrBefore(), true);
|
|
||||||
|
|
||||||
CHECK(success.ok() && success.value())
|
|
||||||
<< "Writing to output file failed " << success.status() << " "
|
|
||||||
<< success.value();
|
|
||||||
|
|
||||||
// Clean up.
|
|
||||||
sapi::StatusOr<char *> result =
|
|
||||||
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0);
|
|
||||||
CHECK(result.ok()) << "JsonnetVm realloc failed: " << result.status();
|
|
||||||
|
|
||||||
status = api.c_jsonnet_destroy(&vm_pointer);
|
|
||||||
CHECK(status.ok()) << "JsonnetVm destroy failed: " << status;
|
|
||||||
|
|
||||||
status = api.c_free_input(&input_pointer);
|
|
||||||
CHECK(status.ok()) << "Input freeing failed: " << status;
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,58 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "jsonnet_base_sandbox.h"
|
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
absl::Status JsonnetMain(std::string in_file, std::string out_file) {
|
||||||
|
// Initialize sandbox.
|
||||||
|
JsonnetBaseSandbox sandbox(in_file, out_file);
|
||||||
|
SAPI_RETURN_IF_ERROR(sandbox.Init())
|
||||||
|
|
||||||
|
JsonnetApi api(&sandbox);
|
||||||
|
|
||||||
|
// Initialize library's main structure.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
|
||||||
|
sapi::v::RemotePtr vm_pointer(jsonnet_vm);
|
||||||
|
|
||||||
|
// Read input file.
|
||||||
|
std::string in_file_in_sandboxee(std::string("/input/") +
|
||||||
|
basename(&in_file[0]));
|
||||||
|
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* input,
|
||||||
|
api.c_read_input(false, in_file_var.PtrBefore()));
|
||||||
|
|
||||||
|
// Process jsonnet data.
|
||||||
|
sapi::v::RemotePtr input_pointer(input);
|
||||||
|
sapi::v::Int error;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_stream(
|
||||||
|
&vm_pointer, in_file_var.PtrBefore(),
|
||||||
|
&input_pointer, error.PtrAfter()));
|
||||||
|
CHECK(!error.GetValue())
|
||||||
|
<< "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
|
||||||
|
<< "Make sure all files used by your jsonnet file are in the same "
|
||||||
|
"directory as your file.";
|
||||||
|
|
||||||
|
// Write data to file.
|
||||||
|
std::string out_file_in_sandboxee(std::string("/output/") +
|
||||||
|
basename(&out_file[0]));
|
||||||
|
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
||||||
|
sapi::v::RemotePtr output_pointer(output);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
bool success,
|
||||||
|
api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
|
||||||
|
CHECK(success) << "Writing to output file failed: " << success;
|
||||||
|
|
||||||
|
// Clean up.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(char* result,
|
||||||
|
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
|
||||||
|
SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
google::InitGoogleLogging(argv[0]);
|
google::InitGoogleLogging(argv[0]);
|
||||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||||
|
|
||||||
|
@ -31,61 +80,11 @@ int main(int argc, char *argv[]) {
|
||||||
std::string in_file(argv[1]);
|
std::string in_file(argv[1]);
|
||||||
std::string out_file(argv[2]);
|
std::string out_file(argv[2]);
|
||||||
|
|
||||||
// Initialize sandbox.
|
absl::Status status = JsonnetMain(in_file, out_file);
|
||||||
JsonnetBaseSandbox sandbox(in_file, out_file);
|
if (!status.ok()) {
|
||||||
absl::Status status = sandbox.Init();
|
LOG(ERROR) << "Failed: " << status.ToString();
|
||||||
CHECK(status.ok()) << "Sandbox initialization failed " << status;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
JsonnetApi api(&sandbox);
|
|
||||||
|
|
||||||
// Initialize library's main structure.
|
|
||||||
sapi::StatusOr<JsonnetVm *> jsonnet_vm = api.c_jsonnet_make();
|
|
||||||
sapi::v::RemotePtr vm_pointer(jsonnet_vm.value());
|
|
||||||
CHECK(jsonnet_vm.ok()) << "JsonnetVm initialization failed: "
|
|
||||||
<< jsonnet_vm.status();
|
|
||||||
|
|
||||||
// Read input file.
|
|
||||||
std::string in_file_in_sandboxee(std::string("/input/") +
|
|
||||||
basename(&in_file[0]));
|
|
||||||
sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
|
|
||||||
sapi::StatusOr<char *> input =
|
|
||||||
api.c_read_input(false, in_file_var.PtrBefore());
|
|
||||||
CHECK(input.ok()) << "Reading input file failed " << input.status();
|
|
||||||
|
|
||||||
// Process jsonnet data.
|
|
||||||
sapi::v::RemotePtr input_pointer(input.value());
|
|
||||||
sapi::v::Int error;
|
|
||||||
sapi::StatusOr<char *> output = api.c_jsonnet_evaluate_snippet_stream(
|
|
||||||
&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
|
|
||||||
CHECK(output.ok() && !error.GetValue())
|
|
||||||
<< "Jsonnet code evaluation failed: " << output.status() << " "
|
|
||||||
<< error.GetValue() << "\n"
|
|
||||||
<< "Make sure all files used by your jsonnet file are in the same "
|
|
||||||
"directory as your file.";
|
|
||||||
|
|
||||||
// Write data to file.
|
|
||||||
std::string out_file_in_sandboxee(std::string("/output/") +
|
|
||||||
basename(&out_file[0]));
|
|
||||||
sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
|
|
||||||
sapi::v::RemotePtr output_pointer(output.value());
|
|
||||||
sapi::StatusOr<bool> success;
|
|
||||||
|
|
||||||
success =
|
|
||||||
api.c_write_output_stream(&output_pointer, out_file_var.PtrBefore());
|
|
||||||
CHECK(success.ok() && success.value())
|
|
||||||
<< "Writing to output file failed " << success.status() << " "
|
|
||||||
<< success.value();
|
|
||||||
|
|
||||||
// Clean up.
|
|
||||||
sapi::StatusOr<char *> result =
|
|
||||||
api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0);
|
|
||||||
CHECK(result.ok()) << "JsonnetVm realloc failed: " << result.status();
|
|
||||||
|
|
||||||
status = api.c_jsonnet_destroy(&vm_pointer);
|
|
||||||
CHECK(status.ok()) << "JsonnetVm destroy failed: " << status;
|
|
||||||
|
|
||||||
status = api.c_free_input(&input_pointer);
|
|
||||||
CHECK(status.ok()) << "Input freeing failed: " << status;
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "jsonnet_base_sandbox.h"
|
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||||
|
|
||||||
class JsonnetTransaction : public sapi::Transaction {
|
class JsonnetTransaction : public sapi::Transaction {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "jsonnet_base_sandbox.h"
|
#include "jsonnet_base_sandbox.h" // NOLINT(build/include)
|
||||||
#include "jsonnet_sapi.sapi.h"
|
#include "jsonnet_sapi.sapi.h"
|
||||||
#include "sandboxed_api/util/flag.h"
|
#include "sandboxed_api/util/flag.h"
|
||||||
#include "sandboxed_api/util/status_matchers.h"
|
#include "sandboxed_api/util/status_matchers.h"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- jsonnet.cpp 2020-09-09 12:15:33.687539042 +0000
|
--- jsonnet.cpp 2020-09-09 12:15:33.687539042 +0000
|
||||||
+++ write_helper.cpp 2020-09-17 15:35:02.684743876 +0000
|
+++ write_helper.cpp 2020-09-25 15:38:37.317147682 +0000
|
||||||
@@ -14,559 +14,125 @@
|
@@ -14,559 +14,125 @@
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
-#include "utils.h"
|
-#include "utils.h"
|
||||||
+#include "jsonnet_helper.h"
|
+#include "jsonnet_helper.h" // NOLINT(build/include)
|
||||||
|
|
||||||
-extern "C" {
|
-extern "C" {
|
||||||
-#include <libjsonnet.h>
|
-#include <libjsonnet.h>
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "jsonnet_helper.h"
|
#include "jsonnet_helper.h" // NOLINT(build/include)
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,11 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
#include "jsonnet/cmd/utils.h"
|
#include "jsonnet/cmd/utils.h" // NOLINT(build/include)
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <libjsonnet.h>
|
#include <libjsonnet.h> // NOLINT(build/include)
|
||||||
#include <libjsonnet_fmt.h>
|
#include <libjsonnet_fmt.h> // NOLINT(build/include)
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" struct JsonnetVm* c_jsonnet_make(void);
|
extern "C" struct JsonnetVm* c_jsonnet_make(void);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user