Support only read properties from storage

This commit is contained in:
topjohnwu 2023-05-19 01:53:40 -07:00
parent dc61033b2c
commit 91773c3311
2 changed files with 17 additions and 8 deletions

View File

@ -29,9 +29,11 @@ struct PropFlags {
void setSkipSvc() { flags |= 1; } void setSkipSvc() { flags |= 1; }
void setPersist() { flags |= (1 << 1); } void setPersist() { flags |= (1 << 1); }
void setContext() { flags |= (1 << 2); } void setContext() { flags |= (1 << 2); }
void setPersistOnly() { flags |= (1 << 3); setPersist(); }
bool isSkipSvc() const { return flags & 1; } bool isSkipSvc() const { return flags & 1; }
bool isPersist() const { return flags & (1 << 1); } bool isPersist() const { return flags & (1 << 1); }
bool isContext() const { return flags & (1 << 2); } bool isContext() const { return flags & (1 << 2); }
bool isPersistOnly() const { return flags & (1 << 3); }
private: private:
uint32_t flags = 0; uint32_t flags = 0;
}; };
@ -56,12 +58,13 @@ General flags:
-v print verbose output to stderr -v print verbose output to stderr
Read mode flags: Read mode flags:
-Z get property context instead of value
-p also read persistent props from storage -p also read persistent props from storage
-P only read persistent props from storage
-Z get property context instead of value
Write mode flags: Write mode flags:
-n set properties bypassing property_service -n set properties bypassing property_service
-p always write persistent props changes to storage -p always write persistent prop changes to storage
)EOF", arg0); )EOF", arg0);
exit(1); exit(1);
@ -178,11 +181,13 @@ static string get_prop(const char *name, PropFlags flags) {
} }
string val; string val;
if (!flags.isPersistOnly()) {
if (auto pi = system_property_find(name)) { if (auto pi = system_property_find(name)) {
prop_to_string cb(val); prop_to_string cb(val);
read_prop_with_cb(pi, &cb); read_prop_with_cb(pi, &cb);
LOGD("resetprop: get prop [%s]: [%s]\n", name, val.data()); LOGD("resetprop: get prop [%s]: [%s]\n", name, val.data());
} }
}
if (val.empty() && flags.isPersist() && str_starts(name, "persist.")) if (val.empty() && flags.isPersist() && str_starts(name, "persist."))
val = persist_get_prop(name); val = persist_get_prop(name);
@ -195,6 +200,7 @@ static string get_prop(const char *name, PropFlags flags) {
static void print_props(PropFlags flags) { static void print_props(PropFlags flags) {
prop_list list; prop_list list;
prop_collector collector(list); prop_collector collector(list);
if (!flags.isPersistOnly())
system_property_foreach(read_prop_with_cb, &collector); system_property_foreach(read_prop_with_cb, &collector);
if (flags.isPersist()) if (flags.isPersist())
persist_get_props(&collector); persist_get_props(&collector);
@ -291,6 +297,9 @@ int resetprop_main(int argc, char *argv[]) {
case 'p': case 'p':
flags.setPersist(); flags.setPersist();
continue; continue;
case 'P':
flags.setPersistOnly();
continue;
case 'v': case 'v':
set_log_level_state(LogLevel::Debug, true); set_log_level_state(LogLevel::Debug, true);
continue; continue;

View File

@ -15,7 +15,7 @@ using prop_list = std::map<std::string, std::string>;
struct prop_collector : prop_cb { struct prop_collector : prop_cb {
explicit prop_collector(prop_list &list) : list(list) {} explicit prop_collector(prop_list &list) : list(list) {}
void exec(const char *name, const char *value) override { void exec(const char *name, const char *value) override {
list.insert_or_assign(name, value); list.insert({name, value});
} }
private: private:
prop_list &list; prop_list &list;