mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-16 12:39:49 +00:00
Convert indentation to spaces
The tab war is lost
This commit is contained in:
@@ -8,12 +8,12 @@
|
||||
#define PERSISTENT_PROPERTY_DIR "/data/property"
|
||||
|
||||
struct prop_cb {
|
||||
virtual void exec(const char *name, const char *value) {
|
||||
exec(std::string(name), value);
|
||||
}
|
||||
virtual void exec(std::string &&name, const char *value) {
|
||||
exec(name.data(), value);
|
||||
}
|
||||
virtual void exec(const char *name, const char *value) {
|
||||
exec(std::string(name), value);
|
||||
}
|
||||
virtual void exec(std::string &&name, const char *value) {
|
||||
exec(name.data(), value);
|
||||
}
|
||||
};
|
||||
|
||||
extern bool use_pb;
|
||||
@@ -21,15 +21,15 @@ extern bool use_pb;
|
||||
using prop_list = std::map<std::string, std::string>;
|
||||
|
||||
struct prop_collector : prop_cb {
|
||||
explicit prop_collector(prop_list &list) : list(list) {}
|
||||
void exec(const char *name, const char *value) override {
|
||||
list.insert_or_assign(name, value);
|
||||
}
|
||||
void exec(std::string &&name, const char *value) override {
|
||||
list.insert_or_assign(std::move(name), value);
|
||||
}
|
||||
explicit prop_collector(prop_list &list) : list(list) {}
|
||||
void exec(const char *name, const char *value) override {
|
||||
list.insert_or_assign(name, value);
|
||||
}
|
||||
void exec(std::string &&name, const char *value) override {
|
||||
list.insert_or_assign(std::move(name), value);
|
||||
}
|
||||
private:
|
||||
prop_list &list;
|
||||
prop_list &list;
|
||||
};
|
||||
|
||||
std::string persist_getprop(const char *name);
|
||||
|
@@ -23,13 +23,13 @@ using namespace std;
|
||||
|
||||
/* Struct definitions */
|
||||
struct PersistentProperties {
|
||||
pb_callback_t properties;
|
||||
pb_callback_t properties;
|
||||
};
|
||||
|
||||
struct PersistentProperties_PersistentPropertyRecord {
|
||||
pb_callback_t name;
|
||||
bool has_value;
|
||||
char value[92];
|
||||
pb_callback_t name;
|
||||
bool has_value;
|
||||
char value[92];
|
||||
};
|
||||
|
||||
/* Initializer values for message structs */
|
||||
@@ -78,163 +78,163 @@ PB_BIND(PersistentProperties_PersistentPropertyRecord, PersistentProperties_Pers
|
||||
bool use_pb = false;
|
||||
|
||||
static bool name_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||
string &name = *static_cast<string *>(*arg);
|
||||
name.resize(stream->bytes_left);
|
||||
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
|
||||
string &name = *static_cast<string *>(*arg);
|
||||
name.resize(stream->bytes_left);
|
||||
return pb_read(stream, (pb_byte_t *)(name.data()), stream->bytes_left);
|
||||
}
|
||||
|
||||
static bool name_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||
return pb_encode_tag_for_field(stream, field) &&
|
||||
pb_encode_string(stream, (const pb_byte_t *) *arg, strlen((const char *) *arg));
|
||||
return pb_encode_tag_for_field(stream, field) &&
|
||||
pb_encode_string(stream, (const pb_byte_t *) *arg, strlen((const char *) *arg));
|
||||
}
|
||||
|
||||
static bool prop_decode(pb_istream_t *stream, const pb_field_t *field, void **arg) {
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
string name;
|
||||
prop.name.funcs.decode = name_decode;
|
||||
prop.name.arg = &name;
|
||||
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
auto cb = static_cast<prop_cb*>(*arg);
|
||||
cb->exec(std::move(name), prop.value);
|
||||
return true;
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
string name;
|
||||
prop.name.funcs.decode = name_decode;
|
||||
prop.name.arg = &name;
|
||||
if (!pb_decode(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
auto cb = static_cast<prop_cb*>(*arg);
|
||||
cb->exec(std::move(name), prop.value);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
prop.name.funcs.encode = name_encode;
|
||||
prop.has_value = true;
|
||||
auto &list = *static_cast<prop_list *>(*arg);
|
||||
for (auto &p : list) {
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
prop.name.arg = (void *) p.first.data();
|
||||
strcpy(prop.value, p.second.data());
|
||||
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
PersistentProperties_PersistentPropertyRecord prop{};
|
||||
prop.name.funcs.encode = name_encode;
|
||||
prop.has_value = true;
|
||||
auto &list = *static_cast<prop_list *>(*arg);
|
||||
for (auto &p : list) {
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
prop.name.arg = (void *) p.first.data();
|
||||
strcpy(prop.value, p.second.data());
|
||||
if (!pb_encode_submessage(stream, &PersistentProperties_PersistentPropertyRecord_msg, &prop))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) {
|
||||
int fd = (intptr_t)stream->state;
|
||||
return xwrite(fd, buf, count) == count;
|
||||
int fd = (intptr_t)stream->state;
|
||||
return xwrite(fd, buf, count) == count;
|
||||
}
|
||||
|
||||
static pb_ostream_t create_ostream(const char *filename) {
|
||||
int fd = creat(filename, 0644);
|
||||
pb_ostream_t o = {
|
||||
.callback = write_callback,
|
||||
.state = (void*)(intptr_t)fd,
|
||||
.max_size = SIZE_MAX,
|
||||
.bytes_written = 0,
|
||||
};
|
||||
return o;
|
||||
int fd = creat(filename, 0644);
|
||||
pb_ostream_t o = {
|
||||
.callback = write_callback,
|
||||
.state = (void*)(intptr_t)fd,
|
||||
.max_size = SIZE_MAX,
|
||||
.bytes_written = 0,
|
||||
};
|
||||
return o;
|
||||
}
|
||||
|
||||
static void pb_getprop(prop_cb *prop_cb) {
|
||||
LOGD("resetprop: decode with protobuf [" PERSISTENT_PROPERTY_DIR "/persistent_properties]\n");
|
||||
PersistentProperties props = {};
|
||||
props.properties.funcs.decode = prop_decode;
|
||||
props.properties.arg = prop_cb;
|
||||
pb_byte_t *buf;
|
||||
size_t size;
|
||||
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf, size);
|
||||
pb_decode(&stream, &PersistentProperties_msg, &props);
|
||||
munmap(buf, size);
|
||||
LOGD("resetprop: decode with protobuf [" PERSISTENT_PROPERTY_DIR "/persistent_properties]\n");
|
||||
PersistentProperties props = {};
|
||||
props.properties.funcs.decode = prop_decode;
|
||||
props.properties.arg = prop_cb;
|
||||
pb_byte_t *buf;
|
||||
size_t size;
|
||||
mmap_ro(PERSISTENT_PROPERTY_DIR "/persistent_properties", buf, size);
|
||||
pb_istream_t stream = pb_istream_from_buffer(buf, size);
|
||||
pb_decode(&stream, &PersistentProperties_msg, &props);
|
||||
munmap(buf, size);
|
||||
}
|
||||
|
||||
static bool file_getprop(const char *name, char *value) {
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
LOGD("resetprop: read prop from [%s]\n", path);
|
||||
value[read(fd, value, PROP_VALUE_MAX - 1)] = '\0'; // Null terminate the read value
|
||||
close(fd);
|
||||
return value[0] != '\0';
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
LOGD("resetprop: read prop from [%s]\n", path);
|
||||
value[read(fd, value, PROP_VALUE_MAX - 1)] = '\0'; // Null terminate the read value
|
||||
close(fd);
|
||||
return value[0] != '\0';
|
||||
}
|
||||
|
||||
void persist_getprops(prop_cb *prop_cb) {
|
||||
if (use_pb) {
|
||||
pb_getprop(prop_cb);
|
||||
} else {
|
||||
auto dir = open_dir(PERSISTENT_PROPERTY_DIR);
|
||||
if (!dir) return;
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (file_getprop(entry->d_name, value))
|
||||
prop_cb->exec(entry->d_name, value);
|
||||
}
|
||||
}
|
||||
if (use_pb) {
|
||||
pb_getprop(prop_cb);
|
||||
} else {
|
||||
auto dir = open_dir(PERSISTENT_PROPERTY_DIR);
|
||||
if (!dir) return;
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (file_getprop(entry->d_name, value))
|
||||
prop_cb->exec(entry->d_name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct match_prop_name : prop_cb {
|
||||
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
|
||||
void exec(string &&name, const char *val) override {
|
||||
if (name == _name)
|
||||
strcpy(value, val);
|
||||
}
|
||||
char value[PROP_VALUE_MAX];
|
||||
explicit match_prop_name(const char *name) : _name(name) { value[0] = '\0'; }
|
||||
void exec(string &&name, const char *val) override {
|
||||
if (name == _name)
|
||||
strcpy(value, val);
|
||||
}
|
||||
char value[PROP_VALUE_MAX];
|
||||
private:
|
||||
const char *_name;
|
||||
const char *_name;
|
||||
};
|
||||
|
||||
string persist_getprop(const char *name) {
|
||||
if (use_pb) {
|
||||
auto prop = match_prop_name(name);
|
||||
pb_getprop(&prop);
|
||||
if (prop.value[0]) {
|
||||
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, prop.value);
|
||||
return prop.value;
|
||||
}
|
||||
} else {
|
||||
// Try to read from file
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (file_getprop(name, value)) {
|
||||
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return string();
|
||||
if (use_pb) {
|
||||
auto prop = match_prop_name(name);
|
||||
pb_getprop(&prop);
|
||||
if (prop.value[0]) {
|
||||
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, prop.value);
|
||||
return prop.value;
|
||||
}
|
||||
} else {
|
||||
// Try to read from file
|
||||
char value[PROP_VALUE_MAX];
|
||||
if (file_getprop(name, value)) {
|
||||
LOGD("resetprop: getprop (persist) [%s]: [%s]\n", name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return string();
|
||||
}
|
||||
|
||||
bool persist_deleteprop(const char *name) {
|
||||
if (use_pb) {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
persist_getprops(&collector);
|
||||
if (use_pb) {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
persist_getprops(&collector);
|
||||
|
||||
for (auto it = list.begin(); it != list.end(); ++it) {
|
||||
if (it->first == name) {
|
||||
list.erase(it);
|
||||
// Dump the props back
|
||||
PersistentProperties props{};
|
||||
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp");
|
||||
props.properties.funcs.encode = prop_encode;
|
||||
props.properties.arg = &list;
|
||||
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp]\n");
|
||||
if (!pb_encode(&ostream, &PersistentProperties_msg, &props))
|
||||
return false;
|
||||
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
|
||||
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
|
||||
rename(PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp",
|
||||
PERSISTENT_PROPERTY_DIR "/persistent_properties");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
if (unlink(path) == 0) {
|
||||
LOGD("resetprop: unlink [%s]\n", path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
for (auto it = list.begin(); it != list.end(); ++it) {
|
||||
if (it->first == name) {
|
||||
list.erase(it);
|
||||
// Dump the props back
|
||||
PersistentProperties props{};
|
||||
pb_ostream_t ostream = create_ostream(PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp");
|
||||
props.properties.funcs.encode = prop_encode;
|
||||
props.properties.arg = &list;
|
||||
LOGD("resetprop: encode with protobuf [" PERSISTENT_PROPERTY_DIR
|
||||
"/persistent_properties.tmp]\n");
|
||||
if (!pb_encode(&ostream, &PersistentProperties_msg, &props))
|
||||
return false;
|
||||
clone_attr(PERSISTENT_PROPERTY_DIR "/persistent_properties",
|
||||
PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp");
|
||||
rename(PERSISTENT_PROPERTY_DIR "/persistent_properties.tmp",
|
||||
PERSISTENT_PROPERTY_DIR "/persistent_properties");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
char path[4096];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
if (unlink(path) == 0) {
|
||||
LOGD("resetprop: unlink [%s]\n", path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@@ -26,24 +26,24 @@ static int (*system_property_set)(const char*, const char*);
|
||||
static int (*system_property_read)(const prop_info*, char*, char*);
|
||||
static const prop_info *(*system_property_find)(const char*);
|
||||
static void (*system_property_read_callback)(
|
||||
const prop_info*, void (*)(void*, const char*, const char*, uint32_t), void*);
|
||||
const prop_info*, void (*)(void*, const char*, const char*, uint32_t), void*);
|
||||
static int (*system_property_foreach)(void (*)(const prop_info*, void*), void*);
|
||||
|
||||
#define DLOAD(name) \
|
||||
*(void **) &name = dlsym(RTLD_DEFAULT, "__" #name)
|
||||
|
||||
static void load_functions() {
|
||||
DLOAD(system_property_set);
|
||||
DLOAD(system_property_read);
|
||||
DLOAD(system_property_find);
|
||||
DLOAD(system_property_read_callback);
|
||||
DLOAD(system_property_foreach);
|
||||
DLOAD(system_property_set);
|
||||
DLOAD(system_property_read);
|
||||
DLOAD(system_property_find);
|
||||
DLOAD(system_property_read_callback);
|
||||
DLOAD(system_property_foreach);
|
||||
}
|
||||
#undef DLOAD
|
||||
#endif
|
||||
|
||||
[[noreturn]] static void usage(char* arg0) {
|
||||
fprintf(stderr,
|
||||
fprintf(stderr,
|
||||
R"EOF(resetprop - System Property Manipulation Tool
|
||||
|
||||
Usage: %s [flags] [options...]
|
||||
@@ -64,187 +64,187 @@ Flags:
|
||||
(this flag only affects getprop and delprop)
|
||||
|
||||
)EOF", arg0);
|
||||
exit(1);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static bool check_legal_property_name(const char *name) {
|
||||
int namelen = strlen(name);
|
||||
int namelen = strlen(name);
|
||||
|
||||
if (namelen < 1) goto illegal;
|
||||
if (name[0] == '.') goto illegal;
|
||||
if (name[namelen - 1] == '.') goto illegal;
|
||||
if (namelen < 1) goto illegal;
|
||||
if (name[0] == '.') goto illegal;
|
||||
if (name[namelen - 1] == '.') goto illegal;
|
||||
|
||||
/* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
|
||||
/* Don't allow ".." to appear in a property name */
|
||||
for (size_t i = 0; i < namelen; i++) {
|
||||
if (name[i] == '.') {
|
||||
// i=0 is guaranteed to never have a dot. See above.
|
||||
if (name[i-1] == '.') goto illegal;
|
||||
continue;
|
||||
}
|
||||
if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
|
||||
if (name[i] >= 'a' && name[i] <= 'z') continue;
|
||||
if (name[i] >= 'A' && name[i] <= 'Z') continue;
|
||||
if (name[i] >= '0' && name[i] <= '9') continue;
|
||||
goto illegal;
|
||||
}
|
||||
/* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
|
||||
/* Don't allow ".." to appear in a property name */
|
||||
for (size_t i = 0; i < namelen; i++) {
|
||||
if (name[i] == '.') {
|
||||
// i=0 is guaranteed to never have a dot. See above.
|
||||
if (name[i-1] == '.') goto illegal;
|
||||
continue;
|
||||
}
|
||||
if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') continue;
|
||||
if (name[i] >= 'a' && name[i] <= 'z') continue;
|
||||
if (name[i] >= 'A' && name[i] <= 'Z') continue;
|
||||
if (name[i] >= '0' && name[i] <= '9') continue;
|
||||
goto illegal;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
|
||||
illegal:
|
||||
LOGE("Illegal property name: [%s]\n", name);
|
||||
return false;
|
||||
LOGE("Illegal property name: [%s]\n", name);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void read_prop(const prop_info *pi, void *cb) {
|
||||
if (system_property_read_callback) {
|
||||
auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
|
||||
static_cast<prop_cb*>(cb)->exec(name, value);
|
||||
};
|
||||
system_property_read_callback(pi, callback, cb);
|
||||
} else {
|
||||
char name[PROP_NAME_MAX];
|
||||
char value[PROP_VALUE_MAX];
|
||||
name[0] = '\0';
|
||||
value[0] = '\0';
|
||||
system_property_read(pi, name, value);
|
||||
static_cast<prop_cb*>(cb)->exec(name, value);
|
||||
}
|
||||
if (system_property_read_callback) {
|
||||
auto callback = [](void *cb, const char *name, const char *value, uint32_t) {
|
||||
static_cast<prop_cb*>(cb)->exec(name, value);
|
||||
};
|
||||
system_property_read_callback(pi, callback, cb);
|
||||
} else {
|
||||
char name[PROP_NAME_MAX];
|
||||
char value[PROP_VALUE_MAX];
|
||||
name[0] = '\0';
|
||||
value[0] = '\0';
|
||||
system_property_read(pi, name, value);
|
||||
static_cast<prop_cb*>(cb)->exec(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
struct sysprop_stub {
|
||||
virtual int setprop(const char *name, const char *value, bool trigger) { return 1; }
|
||||
virtual string getprop(const char *name, bool persist) { return string(); }
|
||||
virtual void getprops(void (*callback)(const char *, const char *, void *),
|
||||
void *cookie, bool persist) {}
|
||||
virtual int delprop(const char *name, bool persist) { return 1; }
|
||||
virtual int setprop(const char *name, const char *value, bool trigger) { return 1; }
|
||||
virtual string getprop(const char *name, bool persist) { return string(); }
|
||||
virtual void getprops(void (*callback)(const char *, const char *, void *),
|
||||
void *cookie, bool persist) {}
|
||||
virtual int delprop(const char *name, bool persist) { return 1; }
|
||||
};
|
||||
|
||||
struct sysprop : public sysprop_stub {
|
||||
|
||||
int setprop(const char *name, const char *value, bool) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
return system_property_set(name, value);
|
||||
}
|
||||
int setprop(const char *name, const char *value, bool) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
return system_property_set(name, value);
|
||||
}
|
||||
|
||||
struct prop_to_string : prop_cb {
|
||||
explicit prop_to_string(string &s) : val(s) {}
|
||||
void exec(const char *, const char *value) override {
|
||||
val = value;
|
||||
}
|
||||
private:
|
||||
string &val;
|
||||
};
|
||||
struct prop_to_string : prop_cb {
|
||||
explicit prop_to_string(string &s) : val(s) {}
|
||||
void exec(const char *, const char *value) override {
|
||||
val = value;
|
||||
}
|
||||
private:
|
||||
string &val;
|
||||
};
|
||||
|
||||
string getprop(const char *name, bool) override {
|
||||
string val;
|
||||
if (!check_legal_property_name(name))
|
||||
return val;
|
||||
auto pi = system_property_find(name);
|
||||
if (pi == nullptr)
|
||||
return val;
|
||||
auto prop = prop_to_string(val);
|
||||
read_prop(pi, &prop);
|
||||
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
|
||||
return val;
|
||||
}
|
||||
string getprop(const char *name, bool) override {
|
||||
string val;
|
||||
if (!check_legal_property_name(name))
|
||||
return val;
|
||||
auto pi = system_property_find(name);
|
||||
if (pi == nullptr)
|
||||
return val;
|
||||
auto prop = prop_to_string(val);
|
||||
read_prop(pi, &prop);
|
||||
LOGD("resetprop: getprop [%s]: [%s]\n", name, val.data());
|
||||
return val;
|
||||
}
|
||||
|
||||
void getprops(void (*callback)(const char*, const char*, void*), void *cookie, bool) override {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
system_property_foreach(read_prop, &collector);
|
||||
for (auto &[key, val] : list)
|
||||
callback(key.data(), val.data(), cookie);
|
||||
}
|
||||
void getprops(void (*callback)(const char*, const char*, void*), void *cookie, bool) override {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
system_property_foreach(read_prop, &collector);
|
||||
for (auto &[key, val] : list)
|
||||
callback(key.data(), val.data(), cookie);
|
||||
}
|
||||
};
|
||||
|
||||
struct resetprop : public sysprop {
|
||||
|
||||
int setprop(const char *name, const char *value, bool prop_svc) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
int setprop(const char *name, const char *value, bool prop_svc) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
|
||||
const char *msg = prop_svc ? "property_service" : "modifying prop data structure";
|
||||
const char *msg = prop_svc ? "property_service" : "modifying prop data structure";
|
||||
|
||||
int ret;
|
||||
auto pi = const_cast<prop_info *>(__system_property_find(name));
|
||||
if (pi != nullptr) {
|
||||
if (prop_svc) {
|
||||
if (strncmp(name, "ro.", 3) == 0)
|
||||
delprop(name, false);
|
||||
ret = system_property_set(name, value);
|
||||
} else {
|
||||
ret = __system_property_update(pi, value, strlen(value));
|
||||
}
|
||||
LOGD("resetprop: update prop [%s]: [%s] by %s\n", name, value, msg);
|
||||
} else {
|
||||
if (prop_svc) {
|
||||
ret = system_property_set(name, value);
|
||||
} else {
|
||||
ret = __system_property_add(name, strlen(name), value, strlen(value));
|
||||
}
|
||||
LOGD("resetprop: create prop [%s]: [%s] by %s\n", name, value, msg);
|
||||
}
|
||||
int ret;
|
||||
auto pi = const_cast<prop_info *>(__system_property_find(name));
|
||||
if (pi != nullptr) {
|
||||
if (prop_svc) {
|
||||
if (strncmp(name, "ro.", 3) == 0)
|
||||
delprop(name, false);
|
||||
ret = system_property_set(name, value);
|
||||
} else {
|
||||
ret = __system_property_update(pi, value, strlen(value));
|
||||
}
|
||||
LOGD("resetprop: update prop [%s]: [%s] by %s\n", name, value, msg);
|
||||
} else {
|
||||
if (prop_svc) {
|
||||
ret = system_property_set(name, value);
|
||||
} else {
|
||||
ret = __system_property_add(name, strlen(name), value, strlen(value));
|
||||
}
|
||||
LOGD("resetprop: create prop [%s]: [%s] by %s\n", name, value, msg);
|
||||
}
|
||||
|
||||
if (ret)
|
||||
LOGW("resetprop: setprop error\n");
|
||||
if (ret)
|
||||
LOGW("resetprop: setprop error\n");
|
||||
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
string getprop(const char *name, bool persist) override {
|
||||
string val = sysprop::getprop(name, persist);
|
||||
if (val.empty() && persist && strncmp(name, "persist.", 8) == 0)
|
||||
val = persist_getprop(name);
|
||||
if (val.empty())
|
||||
LOGD("resetprop: prop [%s] does not exist\n", name);
|
||||
return val;
|
||||
}
|
||||
string getprop(const char *name, bool persist) override {
|
||||
string val = sysprop::getprop(name, persist);
|
||||
if (val.empty() && persist && strncmp(name, "persist.", 8) == 0)
|
||||
val = persist_getprop(name);
|
||||
if (val.empty())
|
||||
LOGD("resetprop: prop [%s] does not exist\n", name);
|
||||
return val;
|
||||
}
|
||||
|
||||
void getprops(void (*callback)(const char *, const char *, void *),
|
||||
void *cookie, bool persist) override {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
system_property_foreach(read_prop, &collector);
|
||||
if (persist)
|
||||
persist_getprops(&collector);
|
||||
for (auto &[key, val] : list)
|
||||
callback(key.data(), val.data(), cookie);
|
||||
}
|
||||
void getprops(void (*callback)(const char *, const char *, void *),
|
||||
void *cookie, bool persist) override {
|
||||
prop_list list;
|
||||
prop_collector collector(list);
|
||||
system_property_foreach(read_prop, &collector);
|
||||
if (persist)
|
||||
persist_getprops(&collector);
|
||||
for (auto &[key, val] : list)
|
||||
callback(key.data(), val.data(), cookie);
|
||||
}
|
||||
|
||||
int delprop(const char *name, bool persist) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
LOGD("resetprop: delete prop [%s]\n", name);
|
||||
if (persist && strncmp(name, "persist.", 8) == 0)
|
||||
persist = persist_deleteprop(name);
|
||||
return __system_property_delete(name) && !(persist && strncmp(name, "persist.", 8) == 0);
|
||||
}
|
||||
int delprop(const char *name, bool persist) override {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
LOGD("resetprop: delete prop [%s]\n", name);
|
||||
if (persist && strncmp(name, "persist.", 8) == 0)
|
||||
persist = persist_deleteprop(name);
|
||||
return __system_property_delete(name) && !(persist && strncmp(name, "persist.", 8) == 0);
|
||||
}
|
||||
};
|
||||
|
||||
static sysprop_stub *get_impl() {
|
||||
static sysprop_stub *impl = nullptr;
|
||||
if (impl == nullptr) {
|
||||
use_pb = access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
|
||||
static sysprop_stub *impl = nullptr;
|
||||
if (impl == nullptr) {
|
||||
use_pb = access(PERSISTENT_PROPERTY_DIR "/persistent_properties", R_OK) == 0;
|
||||
#ifdef APPLET_STUB_MAIN
|
||||
if (__system_properties_init()) {
|
||||
LOGE("resetprop: __system_properties_init error\n");
|
||||
exit(1);
|
||||
}
|
||||
impl = new resetprop();
|
||||
if (__system_properties_init()) {
|
||||
LOGE("resetprop: __system_properties_init error\n");
|
||||
exit(1);
|
||||
}
|
||||
impl = new resetprop();
|
||||
#else
|
||||
// Load platform implementations
|
||||
load_functions();
|
||||
if (__system_properties_init()) {
|
||||
LOGW("resetprop: __system_properties_init error\n");
|
||||
impl = new sysprop();
|
||||
} else {
|
||||
impl = new resetprop();
|
||||
}
|
||||
// Load platform implementations
|
||||
load_functions();
|
||||
if (__system_properties_init()) {
|
||||
LOGW("resetprop: __system_properties_init error\n");
|
||||
impl = new sysprop();
|
||||
} else {
|
||||
impl = new resetprop();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return impl;
|
||||
}
|
||||
return impl;
|
||||
}
|
||||
|
||||
/***********************************
|
||||
@@ -252,94 +252,94 @@ static sysprop_stub *get_impl() {
|
||||
***********************************/
|
||||
|
||||
static void print_props(bool persist) {
|
||||
getprops([](const char *name, const char *value, auto) {
|
||||
printf("[%s]: [%s]\n", name, value);
|
||||
}, nullptr, persist);
|
||||
getprops([](const char *name, const char *value, auto) {
|
||||
printf("[%s]: [%s]\n", name, value);
|
||||
}, nullptr, persist);
|
||||
}
|
||||
|
||||
string getprop(const char *name, bool persist) {
|
||||
return get_impl()->getprop(name, persist);
|
||||
return get_impl()->getprop(name, persist);
|
||||
}
|
||||
|
||||
void getprops(void (*callback)(const char *, const char *, void *), void *cookie, bool persist) {
|
||||
get_impl()->getprops(callback, cookie, persist);
|
||||
get_impl()->getprops(callback, cookie, persist);
|
||||
}
|
||||
|
||||
int setprop(const char *name, const char *value, bool prop_svc) {
|
||||
return get_impl()->setprop(name, value, prop_svc);
|
||||
return get_impl()->setprop(name, value, prop_svc);
|
||||
}
|
||||
|
||||
int delprop(const char *name, bool persist) {
|
||||
return get_impl()->delprop(name, persist);
|
||||
return get_impl()->delprop(name, persist);
|
||||
}
|
||||
|
||||
void load_prop_file(const char *filename, bool prop_svc) {
|
||||
auto impl = get_impl();
|
||||
LOGD("resetprop: Parse prop file [%s]\n", filename);
|
||||
parse_prop_file(filename, [=](auto key, auto val) -> bool {
|
||||
impl->setprop(key.data(), val.data(), prop_svc);
|
||||
return true;
|
||||
});
|
||||
auto impl = get_impl();
|
||||
LOGD("resetprop: Parse prop file [%s]\n", filename);
|
||||
parse_prop_file(filename, [=](auto key, auto val) -> bool {
|
||||
impl->setprop(key.data(), val.data(), prop_svc);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
int resetprop_main(int argc, char *argv[]) {
|
||||
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };
|
||||
log_cb.d = [](auto fmt, auto ap) -> int { return verbose ? vfprintf(stderr, fmt, ap) : 0; };
|
||||
|
||||
bool prop_svc = true;
|
||||
bool persist = false;
|
||||
char *argv0 = argv[0];
|
||||
bool prop_svc = true;
|
||||
bool persist = false;
|
||||
char *argv0 = argv[0];
|
||||
|
||||
--argc;
|
||||
++argv;
|
||||
--argc;
|
||||
++argv;
|
||||
|
||||
// Parse flags and -- options
|
||||
while (argc && argv[0][0] == '-') {
|
||||
for (int idx = 1; true; ++idx) {
|
||||
switch (argv[0][idx]) {
|
||||
case '-':
|
||||
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
||||
load_prop_file(argv[1], prop_svc);
|
||||
return 0;
|
||||
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
|
||||
return delprop(argv[1], persist);
|
||||
} else if (strcmp(argv[0], "--help") == 0) {
|
||||
usage(argv0);
|
||||
}
|
||||
case 'v':
|
||||
verbose = true;
|
||||
continue;
|
||||
case 'p':
|
||||
persist = true;
|
||||
continue;
|
||||
case 'n':
|
||||
prop_svc = false;
|
||||
continue;
|
||||
case '\0':
|
||||
break;
|
||||
case 'h':
|
||||
default:
|
||||
usage(argv0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
--argc;
|
||||
++argv;
|
||||
}
|
||||
// Parse flags and -- options
|
||||
while (argc && argv[0][0] == '-') {
|
||||
for (int idx = 1; true; ++idx) {
|
||||
switch (argv[0][idx]) {
|
||||
case '-':
|
||||
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
||||
load_prop_file(argv[1], prop_svc);
|
||||
return 0;
|
||||
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
|
||||
return delprop(argv[1], persist);
|
||||
} else if (strcmp(argv[0], "--help") == 0) {
|
||||
usage(argv0);
|
||||
}
|
||||
case 'v':
|
||||
verbose = true;
|
||||
continue;
|
||||
case 'p':
|
||||
persist = true;
|
||||
continue;
|
||||
case 'n':
|
||||
prop_svc = false;
|
||||
continue;
|
||||
case '\0':
|
||||
break;
|
||||
case 'h':
|
||||
default:
|
||||
usage(argv0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
--argc;
|
||||
++argv;
|
||||
}
|
||||
|
||||
switch (argc) {
|
||||
case 0:
|
||||
print_props(persist);
|
||||
return 0;
|
||||
case 1: {
|
||||
string prop = getprop(argv[0], persist);
|
||||
if (prop.empty())
|
||||
return 1;
|
||||
printf("%s\n", prop.data());
|
||||
return 0;
|
||||
}
|
||||
case 2:
|
||||
return setprop(argv[0], argv[1], prop_svc);
|
||||
default:
|
||||
usage(argv0);
|
||||
}
|
||||
switch (argc) {
|
||||
case 0:
|
||||
print_props(persist);
|
||||
return 0;
|
||||
case 1: {
|
||||
string prop = getprop(argv[0], persist);
|
||||
if (prop.empty())
|
||||
return 1;
|
||||
printf("%s\n", prop.data());
|
||||
return 0;
|
||||
}
|
||||
case 2:
|
||||
return setprop(argv[0], argv[1], prop_svc);
|
||||
default:
|
||||
usage(argv0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user