1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <fcntl.h>
17 #include <google/protobuf/io/zero_copy_stream_impl.h>
18 #include <google/protobuf/text_format.h>
19 
20 #include <filesystem>
21 
22 #include "golddata.pb.h"
23 
24 using namespace std;
25 
26 bool ConvertPbtxtToPb(const filesystem::path& pbtxtFile, const filesystem::path& pbOutDir) {
27     // parse plain text from .pbtxt.
28     android::net::GoldTest goldTest;
29 
30     int fd = open(pbtxtFile.c_str(), O_RDONLY);
31     if (fd < 0) {
32         cerr << "Failed to open " << pbtxtFile << ", " << strerror(errno) << endl;
33         return false;
34     }
35     {
36         google::protobuf::io::FileInputStream fileInput(fd);
37         fileInput.SetCloseOnDelete(true);
38         if (!google::protobuf::TextFormat::Parse(&fileInput, &goldTest)) {
39             cerr << "Failed to parse " << pbtxtFile << endl;
40             return false;
41         }
42     }
43 
44     // write marshalled message into .pb file
45     filesystem::path pbFile = pbOutDir / pbtxtFile.filename();
46     pbFile.replace_extension(".pb");
47     fd = open(pbFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0660);
48     if (fd < 0) {
49         cerr << "Failed to open " << pbFile << ", " << strerror(errno) << endl;
50         return false;
51     }
52     {
53         google::protobuf::io::FileOutputStream fileOutputStream(fd);
54         fileOutputStream.SetCloseOnDelete(true);
55         if (!goldTest.SerializeToZeroCopyStream(&fileOutputStream)) {
56             cerr << "Failed to serialize " << pbFile << endl;
57             filesystem::remove(pbFile);
58             return false;
59         }
60     }
61 
62     cout << "Generate " << pbFile << " successfully" << endl;
63     return true;
64 }
65 
66 int main(int argc, char const* const* argv) {
67     filesystem::path pbtxtFile;
68     filesystem::path pbOutDir;
69     const string arg_in = "--in_file=";
70     const string arg_out = "--out_dir=";
71 
72     for (int i = 1; i < argc; i++) {
73         std::string arg = argv[i];
74         if (arg.find(arg_in) == 0) {
75             pbtxtFile = filesystem::path(arg.substr(arg_in.size()));
76         } else if (arg.find(arg_out) == 0) {
77             pbOutDir = filesystem::path(arg.substr(arg_out.size()));
78         } else {
79             cerr << "Unknown argument: " << arg << endl;
80             exit(1);
81         }
82     }
83 
84     if (pbtxtFile.empty() || pbOutDir.empty()) {
85         cerr << arg_in << " or " << arg_out << " is unassigned" << endl;
86         exit(1);
87     }
88     if (!ConvertPbtxtToPb(pbtxtFile, pbOutDir)) {
89         cerr << "Failed to convert " << pbtxtFile << endl;
90         exit(1);
91     }
92 }
93