summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Cherry2018-01-11 10:35:49 -0600
committerTom Cherry2018-01-12 13:26:46 -0600
commit8573b55f0ffba110f4df5c406c61d13750709e89 (patch)
tree231d683960cab0541a382927b84a5d26cd3464a2 /toolbox
parenteac86b0ea90f19f9a4116e9a1ca401f0959ceb03 (diff)
downloadplatform-system-core-8573b55f0ffba110f4df5c406c61d13750709e89.tar.gz
platform-system-core-8573b55f0ffba110f4df5c406c61d13750709e89.tar.xz
platform-system-core-8573b55f0ffba110f4df5c406c61d13750709e89.zip
getprop: add -T option to return a property's type
Bug: 70858511 Test: Return property types correctly Change-Id: I506582d88c12a7f52d699635a41b24de4bc80289
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/getprop.cpp74
1 files changed, 54 insertions, 20 deletions
diff --git a/toolbox/getprop.cpp b/toolbox/getprop.cpp
index 611e2448b..9e324a06f 100644
--- a/toolbox/getprop.cpp
+++ b/toolbox/getprop.cpp
@@ -29,7 +29,13 @@ using android::properties::PropertyInfoAreaFile;
29 29
30PropertyInfoAreaFile property_info_file; 30PropertyInfoAreaFile property_info_file;
31 31
32void PrintAllProperties(bool print_property_context) { 32enum class ResultType {
33 Value,
34 Context,
35 Type,
36};
37
38void PrintAllProperties(ResultType result_type) {
33 std::vector<std::pair<std::string, std::string>> properties; 39 std::vector<std::pair<std::string, std::string>> properties;
34 __system_property_foreach( 40 __system_property_foreach(
35 [](const prop_info* pi, void* cookie) { 41 [](const prop_info* pi, void* cookie) {
@@ -46,11 +52,16 @@ void PrintAllProperties(bool print_property_context) {
46 52
47 std::sort(properties.begin(), properties.end()); 53 std::sort(properties.begin(), properties.end());
48 54
49 if (print_property_context) { 55 if (result_type != ResultType::Value) {
50 for (auto& [name, value] : properties) { 56 for (auto& [name, value] : properties) {
51 const char* context = nullptr; 57 const char* context = nullptr;
52 property_info_file->GetPropertyInfo(name.c_str(), &context, nullptr); 58 const char* type = nullptr;
53 value = context; 59 property_info_file->GetPropertyInfo(name.c_str(), &context, &type);
60 if (result_type == ResultType::Context) {
61 value = context;
62 } else {
63 value = type;
64 }
54 } 65 }
55 } 66 }
56 67
@@ -59,18 +70,28 @@ void PrintAllProperties(bool print_property_context) {
59 } 70 }
60} 71}
61 72
62void PrintProperty(const char* name, const char* default_value, bool print_property_context) { 73void PrintProperty(const char* name, const char* default_value, ResultType result_type) {
63 if (print_property_context) { 74 switch (result_type) {
64 const char* context = nullptr; 75 case ResultType::Value:
65 property_info_file->GetPropertyInfo(name, &context, nullptr); 76 std::cout << GetProperty(name, default_value) << std::endl;
66 std::cout << context << std::endl; 77 break;
67 } else { 78 case ResultType::Context: {
68 std::cout << GetProperty(name, default_value) << std::endl; 79 const char* context = nullptr;
80 property_info_file->GetPropertyInfo(name, &context, nullptr);
81 std::cout << context << std::endl;
82 break;
83 }
84 case ResultType::Type: {
85 const char* type = nullptr;
86 property_info_file->GetPropertyInfo(name, nullptr, &type);
87 std::cout << type << std::endl;
88 break;
89 }
69 } 90 }
70} 91}
71 92
72extern "C" int getprop_main(int argc, char** argv) { 93extern "C" int getprop_main(int argc, char** argv) {
73 bool print_property_context = false; 94 auto result_type = ResultType::Value;
74 95
75 while (true) { 96 while (true) {
76 static const struct option long_options[] = { 97 static const struct option long_options[] = {
@@ -78,7 +99,7 @@ extern "C" int getprop_main(int argc, char** argv) {
78 {nullptr, 0, nullptr, 0}, 99 {nullptr, 0, nullptr, 0},
79 }; 100 };
80 101
81 int arg = getopt_long(argc, argv, "Z", long_options, nullptr); 102 int arg = getopt_long(argc, argv, "TZ", long_options, nullptr);
82 103
83 if (arg == -1) { 104 if (arg == -1) {
84 break; 105 break;
@@ -86,13 +107,27 @@ extern "C" int getprop_main(int argc, char** argv) {
86 107
87 switch (arg) { 108 switch (arg) {
88 case 'h': 109 case 'h':
89 std::cout << "usage: getprop [-Z] [NAME [DEFAULT]]\n\n" 110 std::cout << "usage: getprop [-TZ] [NAME [DEFAULT]]\n"
111 "\n"
90 "Gets an Android system property, or lists them all.\n" 112 "Gets an Android system property, or lists them all.\n"
91 "Use -Z to return the property context instead of the property value\n" 113 "\n"
114 "-T\tShow property types instead of values\n"
115 "-Z\tShow property contexts instead of values\n"
92 << std::endl; 116 << std::endl;
93 return 0; 117 return 0;
118 case 'T':
119 if (result_type != ResultType::Value) {
120 std::cerr << "Only one of -T or -Z may be specified" << std::endl;
121 return -1;
122 }
123 result_type = ResultType::Type;
124 break;
94 case 'Z': 125 case 'Z':
95 print_property_context = true; 126 if (result_type != ResultType::Value) {
127 std::cerr << "Only one of -T or -Z may be specified" << std::endl;
128 return -1;
129 }
130 result_type = ResultType::Context;
96 break; 131 break;
97 case '?': 132 case '?':
98 return -1; 133 return -1;
@@ -102,7 +137,7 @@ extern "C" int getprop_main(int argc, char** argv) {
102 } 137 }
103 } 138 }
104 139
105 if (print_property_context) { 140 if (result_type != ResultType::Value) {
106 property_info_file.LoadDefaultPath(); 141 property_info_file.LoadDefaultPath();
107 if (!property_info_file) { 142 if (!property_info_file) {
108 std::cerr << "Unable to load property info file" << std::endl; 143 std::cerr << "Unable to load property info file" << std::endl;
@@ -111,7 +146,7 @@ extern "C" int getprop_main(int argc, char** argv) {
111 } 146 }
112 147
113 if (optind >= argc) { 148 if (optind >= argc) {
114 PrintAllProperties(print_property_context); 149 PrintAllProperties(result_type);
115 return 0; 150 return 0;
116 } 151 }
117 152
@@ -120,8 +155,7 @@ extern "C" int getprop_main(int argc, char** argv) {
120 return -1; 155 return -1;
121 } 156 }
122 157
123 PrintProperty(argv[optind], (optind == argc - 1) ? "" : argv[optind + 1], 158 PrintProperty(argv[optind], (optind == argc - 1) ? "" : argv[optind + 1], result_type);
124 print_property_context);
125 159
126 return 0; 160 return 0;
127} 161}