From 37b1469fa9259d78c7af53c661d84d3d1cd5c8a7 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Fri, 28 Jul 2023 18:52:53 -0700 Subject: [PATCH] Switched to using std::ofstream instead of Google's protobuf stream for JSON. --- .../textplan/converter/SaveBinary.cpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/substrait/textplan/converter/SaveBinary.cpp b/src/substrait/textplan/converter/SaveBinary.cpp index 5b6feba2..e7347269 100644 --- a/src/substrait/textplan/converter/SaveBinary.cpp +++ b/src/substrait/textplan/converter/SaveBinary.cpp @@ -42,27 +42,23 @@ absl::Status savePlanToBinary( absl::Status savePlanToJson( const ::substrait::proto::Plan& plan, std::string_view output_filename) { - int outputFileDescriptor = - creat(std::string{output_filename}.c_str(), S_IREAD | S_IWRITE); - if (outputFileDescriptor == -1) { - return absl::ErrnoToStatus( - errno, + std::ofstream stream(std::string{output_filename}); + if ((stream.fail())) { + return absl::UnavailableError( fmt::format("Failed to open file {} for writing", output_filename)); } - auto stream = - new google::protobuf::io::FileOutputStream(outputFileDescriptor); std::string output; auto status = ::google::protobuf::util::MessageToJsonString(plan, &output); if (!status.ok()) { return absl::UnknownError("Failed to save plan as a JSON protobuf."); } - if (!stream->WriteCord(absl::Cord(output))) { + stream << output; + if (stream.fail()) { return absl::UnknownError("Failed to write the plan as a JSON protobuf."); } - delete stream; - close(outputFileDescriptor); + stream.close(); return absl::OkStatus(); } @@ -70,6 +66,10 @@ absl::Status savePlanToText( const ::substrait::proto::Plan& plan, std::string_view output_filename) { std::ofstream stream(std::string{output_filename}); + if ((stream.fail())) { + return absl::UnavailableError( + fmt::format("Failed to open file {} for writing", output_filename)); + } auto result = parseBinaryPlan(plan); auto errors = result.getAllErrors(); @@ -77,6 +77,9 @@ absl::Status savePlanToText( return absl::UnknownError(joinLines(errors)); } stream << SymbolTablePrinter::outputToText(result.getSymbolTable()); + if (stream.fail()) { + return absl::UnknownError("Failed to write the plan as text."); + } stream.close(); return absl::OkStatus(); }