Skip to content

Commit

Permalink
Merge pull request chenxiaolong#1192 from chenxiaolong/default.prop-s…
Browse files Browse the repository at this point in the history
…ymlink

Store DualBootPatcher properties in /dbp.prop instead of /default.prop
  • Loading branch information
chenxiaolong authored Jun 11, 2018
2 parents 6346528 + 4769a9f commit d7def58
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 110 deletions.
4 changes: 2 additions & 2 deletions mbtool/include/util/property_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PropertyService
void load_boot_props();
void load_system_props();

bool load_properties_file(const std::string &path, std::string_view filter);

private:
bool m_initialized;
int m_setter_fd;
Expand All @@ -61,6 +63,4 @@ class PropertyService
void socket_handle_set_property_impl(SocketConnection &socket,
const std::string &name,
std::string_view value, bool legacy);

bool load_properties_file(const std::string &path, std::string_view filter);
};
35 changes: 14 additions & 21 deletions mbtool/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ static bool properties_setup()
// Load /default.prop
g_property_service.load_boot_props();

// Load DBP props
g_property_service.load_properties_file(DBP_PROP_PATH, {});

// Start properties service (to allow other processes to set properties)
if (!g_property_service.start_thread()) {
LOGW("Failed to start properties service");
Expand Down Expand Up @@ -507,6 +510,12 @@ static bool add_mbtool_services(bool enable_appsync)
" user root\n"
" oneshot\n"
" seclabel " MB_EXEC_CONTEXT "\n"
"\n"
"service mbtoolprops /mbtool properties set-file " DBP_PROP_PATH "\n"
" class core\n"
" user root\n"
" oneshot\n"
" seclabel " MB_EXEC_CONTEXT "\n"
"\n";
static const char *appsync_service =
"service appsync /mbtool appsync\n"
Expand Down Expand Up @@ -634,28 +643,12 @@ static bool strip_manual_mounts()
return true;
}

static bool add_props_to_default_prop()
static bool add_props_to_dbp_prop()
{
ScopedFILE fp(fopen(DEFAULT_PROP_PATH, "r+be"), fclose);
ScopedFILE fp(fopen(DBP_PROP_PATH, "abe"), fclose);
if (!fp) {
if (errno == ENOENT) {
return true;
} else {
LOGE("%s: Failed to open file: %s",
DEFAULT_PROP_PATH, strerror(errno));
return false;
}
}

// Add newline if the last character isn't already one
if (std::fseek(fp.get(), -1, SEEK_END) == 0) {
char lastchar;
if (std::fread(&lastchar, 1, 1, fp.get()) == 1 && lastchar != '\n') {
fputs("\n", fp.get());
}
} else if (std::fseek(fp.get(), 0, SEEK_END) < 0) {
LOGE("%s: Failed to seek to end of file: %s",
DEFAULT_PROP_PATH, strerror(errno));
LOGE("%s: Failed to open file: %s",
DBP_PROP_PATH, strerror(errno));
return false;
}

Expand Down Expand Up @@ -1193,7 +1186,7 @@ int init_main(int argc, char *argv[])
// Symlink by-name directory to /dev/block/by-name (ugh... ASUS)
symlink_base_dir(device);

add_props_to_default_prop();
add_props_to_dbp_prop();

// initialize properties
properties_setup();
Expand Down
3 changes: 2 additions & 1 deletion mbtool/installer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,8 @@ Installer::ProceedState Installer::install_stage_finish()

std::vector<std::function<RamdiskPatcherFn>> rps{
rp_write_rom_id(_rom->id),
rp_patch_default_prop(_detected_device, _use_fuse_exfat),
rp_restore_default_prop(),
rp_add_dbp_prop(_detected_device, _use_fuse_exfat),
rp_add_binaries(_temp + "/binaries"),
rp_symlink_fuse_exfat(),
rp_symlink_init(),
Expand Down
3 changes: 1 addition & 2 deletions mbtool/mount_fstab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ static bool try_extsd_mount(const char *block_dev, const char *mount_point)
use_fuse_exfat = true;
}

std::string value = util::property_file_get_string(
DEFAULT_PROP_PATH, PROP_USE_FUSE_EXFAT, "");
std::string value = util::property_get_string(PROP_USE_FUSE_EXFAT, "");
if (!value.empty()) {
LOGD("fuse-exfat override: %s", value.c_str());
if (value == "true") {
Expand Down
1 change: 1 addition & 0 deletions mbtool/multiboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define PACKAGES_XML "/data/system/packages.xml"

#define DEFAULT_PROP_PATH "/default.prop"
#define DBP_PROP_PATH "/dbp.prop"

#define DEVICE_JSON_PATH "/device.json"

Expand Down
92 changes: 69 additions & 23 deletions mbtool/properties.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2017-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -34,11 +34,67 @@
namespace mb
{

static void print_prop(std::string_view key, std::string_view value)
{
if (!key.empty()) {
fwrite(key.data(), 1, key.size(), stdout);
fputc('=', stdout);
}
fwrite(value.data(), 1, value.size(), stdout);
fputc('\n', stdout);
}

static bool set_if_possible(const std::string &key, const std::string &value,
bool force)
{
bool ret;

if (force) {
ret = util::property_set_direct(key, value);
} else {
if (auto r = util::property_get(key); r && starts_with(key, "ro.")) {
fprintf(stderr, "Cannot overwrite read-only property '%s'"
" without -f/--force\n", key.c_str());
return false;
}

ret = util::property_set(key, value);
}

if (!ret) {
fprintf(stderr, "Failed to set property '%s'='%s'\n",
key.c_str(), value.c_str());
return false;
}

return true;
}

static bool load_prop_file(const char *path, bool force)
{
bool ret = true;

if (!util::property_file_iter(path, {}, [&](std::string_view key,
std::string_view value) {
if (!set_if_possible(std::string(key), std::string(value), force)) {
ret = false;
}

return util::PropertyIterAction::Continue;
})) {
fprintf(stderr, "%s: Failed to load properties file\n", path);
return false;
}

return ret;
}

static void properties_usage(FILE *stream)
{
fprintf(stream,
"Usage: properties get <property>\n"
" OR: properties set <property> <value> [--force]\n"
" OR: properties set-file <file> [--force]\n"
"\n"
"Options:\n"
" -f, --force Allow overwriting read-only properties\n");
Expand Down Expand Up @@ -89,17 +145,11 @@ int properties_main(int argc, char *argv[])
if (argc - optind == 0) {
util::property_iter([](std::string_view key,
std::string_view value) {
fwrite(key.data(), 1, key.size(), stdout);
fputc('=', stdout);
fwrite(value.data(), 1, value.size(), stdout);
fputc('\n', stdout);
print_prop(key, value);
return util::PropertyIterAction::Continue;
});
} else if (argc - optind == 1) {
std::string value = util::property_get_string(argv[optind], {});

fputs(value.c_str(), stdout);
fputc('\n', stdout);
print_prop({}, util::property_get_string(argv[optind], {}));
} else {
properties_usage(stderr);
return EXIT_FAILURE;
Expand All @@ -112,23 +162,19 @@ int properties_main(int argc, char *argv[])

const char *key = argv[optind];
const char *value = argv[optind + 1];
bool ret;

if (force) {
ret = util::property_set_direct(key, value);
} else {
if (auto r = util::property_get(key);
r && starts_with(key, "ro.")) {
fprintf(stderr, "Cannot overwrite read-only property '%s'"
" without -f/--force\n", key);
return EXIT_FAILURE;
}

ret = util::property_set(key, value);
if (!set_if_possible(key, value, force)) {
return EXIT_FAILURE;
}
} else if (strcmp(action, "set-file") == 0) {
if (argc - optind != 1) {
properties_usage(stderr);
return EXIT_FAILURE;
}

const char *path = argv[optind];

if (!ret) {
fprintf(stderr, "Failed to set property '%s'='%s'\n", key, value);
if (!load_prop_file(path, force)) {
return EXIT_FAILURE;
}
} else {
Expand Down
Loading

0 comments on commit d7def58

Please sign in to comment.