1 /*
2 * Copyright (C) 2021 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
17 #include <fstream>
18 #include <string>
19 #include <log/log.h>
20 #include <sys/stat.h>
21
22 extern bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
23
24 namespace {
printUsage()25 int printUsage() {
26 ALOGE("Usage: qemu-export-property [-f] property_name filename");
27 return 1;
28 }
29
exportPropertyImpl(const char * propName,const char * filename)30 int exportPropertyImpl(const char* propName, const char* filename) {
31 std::string propValue;
32 if (!fs_mgr_get_boot_config(propName, &propValue)) {
33 ALOGV("'%s' bootconfig property is not set", propName);
34 return 0;
35 }
36
37 std::ofstream f;
38 f.open(filename);
39 if (f.is_open()) {
40 f << propValue;
41 f.close();
42 return 0;
43 } else {
44 ALOGE("Failed to open '%s'\n", filename);
45 return 1;
46 }
47 }
48 } // namespace
49
main(const int argc,const char * argv[])50 int main(const int argc, const char* argv[]) {
51 if (argc < 2) {
52 return printUsage();
53 }
54
55 if (strcmp(argv[1], "-f") == 0) {
56 if (argc == 4) {
57 return exportPropertyImpl(argv[2], argv[3]);
58 } else {
59 return printUsage();
60 }
61 } else if (argc == 3) {
62 struct stat st;
63 if (stat(argv[2], &st) == 0) {
64 ALOGV("'%s' already exists", argv[2]);
65 return 0;
66 } else {
67 return exportPropertyImpl(argv[1], argv[2]);
68 }
69 } else {
70 return printUsage();
71 }
72 }
73