Google style applied

This commit is contained in:
Katarzyna Miernikiewicz 2020-09-08 11:11:08 +00:00
parent 9f0bdf1c63
commit c8da7c3db5
3 changed files with 78 additions and 64 deletions

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <syscall.h>
#include <libgen.h>
#include <syscall.h>
#include <cstdlib>
#include <iostream>
@ -27,7 +27,7 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
: in_file_(std::move(in_file)), out_file_(std::move(out_file)) {}
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
sandbox2::PolicyBuilder *) override {
return sandbox2::PolicyBuilder()
.AllowStaticStartup()
.AllowOpen()
@ -44,64 +44,75 @@ class JsonnetSapiSandbox : public JsonnetSandbox {
.AddDirectoryAt(dirname(&out_file_[0]), "/output", /*is_ro=*/false)
.BuildOrDie();
}
private:
std::string in_file_;
std::string out_file_;
private:
std::string in_file_;
std::string out_file_;
};
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
int main(int argc, char *argv[]) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (argc != 3) {
std::cerr << "Usage: " << basename(argv[0]) << " absolute/path/to/INPUT.jsonnet"
if (argc != 3) {
std::cerr << "Usage: " << basename(argv[0])
<< " absolute/path/to/INPUT.jsonnet"
<< " absolute/path/to/OUTPUT\n";
return EXIT_FAILURE;
}
}
std::string in_file(argv[1]);
std::string out_file(argv[2]);
std::string in_file(argv[1]);
std::string out_file(argv[2]);
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_file);
absl::Status status = sandbox.Init();
CHECK(status.ok()) << "Sandbox initialization failed " << status;
// Initialize sandbox.
JsonnetSapiSandbox sandbox(in_file, out_file);
absl::Status status = sandbox.Init();
CHECK(status.ok()) << "Sandbox initialization failed " << status;
JsonnetApi api(&sandbox);
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();
// 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();
// 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_evaluate_snippet(&vm_pointer, in_file_var.PtrBefore(), &input_pointer, error.PtrAfter());
CHECK(output.ok() && !error.GetValue()) << "Jsonnet code evaluation failed" << output.status() << " " << error.GetValue();
// Process jsonnet data.
sapi::v::RemotePtr input_pointer(input.value());
sapi::v::Int error;
sapi::StatusOr<char *> output = api.c_jsonnet_evaluate_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 = 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();
// 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 =
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();
// 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_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;
status = api.c_free_input(&input_pointer);
CHECK(status.ok()) << "Input freeing failed " << status;
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -13,39 +13,39 @@
// limitations under the License.
#include "jsonnet_helper.h"
#include <string.h>
struct JsonnetVm* c_jsonnet_make(void) {
return jsonnet_make();
return jsonnet_make();
}
void c_jsonnet_destroy(struct JsonnetVm* vm) {
return jsonnet_destroy(vm);
}
void c_jsonnet_destroy(struct JsonnetVm* vm) { return jsonnet_destroy(vm); }
char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename, char* snippet, int* error) {
return jsonnet_evaluate_snippet(vm, filename, snippet, error);
char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename,
char* snippet, int* error) {
return jsonnet_evaluate_snippet(vm, filename, snippet, error);
}
char* c_read_input(bool filename_is_code, const char* filename) {
std::string s_filename(filename);
std::string s_input;
bool check = read_input(filename_is_code, &s_filename, &s_input);
char* c_input = strdup(s_input.c_str());
if (check) return c_input;
return nullptr;
std::string s_filename(filename);
std::string s_input;
bool check = read_input(filename_is_code, &s_filename, &s_input);
char* c_input = strdup(s_input.c_str());
if (check) return c_input;
return nullptr;
}
void c_free_input(char* input) {
free(input);
return;
free(input);
return;
}
bool c_write_output_file(const char* output, const char* output_file) {
std::string s_output_file(output_file);
return write_output_file(output, s_output_file);
std::string s_output_file(output_file);
return write_output_file(output, s_output_file);
}
char* c_jsonnet_realloc(JsonnetVm* vm, char* str, size_t sz) {
return jsonnet_realloc(vm, str, sz);
return jsonnet_realloc(vm, str, sz);
}

View File

@ -22,12 +22,15 @@ extern "C" struct JsonnetVm* c_jsonnet_make(void);
extern "C" void c_jsonnet_destroy(struct JsonnetVm* vm);
extern "C" char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm, const char* filename, char* snippet, int* error);
extern "C" char* c_jsonnet_evaluate_snippet(struct JsonnetVm* vm,
const char* filename, char* snippet,
int* error);
extern "C" char* c_read_input(bool filename_is_code, const char* filename);
extern "C" void c_free_input(char* input);
extern "C" bool c_write_output_file(const char* output, const char* output_file);
extern "C" bool c_write_output_file(const char* output,
const char* output_file);
extern "C" char* c_jsonnet_realloc(JsonnetVm* vm, char* str, size_t sz);