From 9fe8741a021aa107c3f2707744b2e1ea3245913e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 21 May 2023 23:51:30 -0700 Subject: [PATCH] Export get_prop to Rust --- native/src/base/Android.mk | 6 ++- native/src/core/core.hpp | 2 + native/src/core/lib.rs | 10 +++++ native/src/core/resetprop/persist.cpp | 7 ++-- native/src/core/resetprop/resetprop.cpp | 53 ++++++++++++++++--------- native/src/core/resetprop/resetprop.hpp | 2 +- 6 files changed, 55 insertions(+), 25 deletions(-) diff --git a/native/src/base/Android.mk b/native/src/base/Android.mk index ae3f9561b..40cb6a7c4 100644 --- a/native/src/base/Android.mk +++ b/native/src/base/Android.mk @@ -4,7 +4,11 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libbase -LOCAL_C_INCLUDES := src/include $(LOCAL_PATH)/include out/generated +LOCAL_C_INCLUDES := \ + src/include \ + $(LOCAL_PATH)/include \ + $(LOCAL_PATH)/../external/cxx-rs/include \ + out/generated LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) LOCAL_EXPORT_STATIC_LIBRARIES := libcxx LOCAL_STATIC_LIBRARIES := libcxx diff --git a/native/src/core/core.hpp b/native/src/core/core.hpp index d1f0b530f..4fa8be7f4 100644 --- a/native/src/core/core.hpp +++ b/native/src/core/core.hpp @@ -2,6 +2,7 @@ #include #include +#include extern bool RECOVERY_MODE; extern std::atomic pkg_xml_ino; @@ -28,6 +29,7 @@ void clear_pkg(const char *pkg, int user_id); [[noreturn]] void install_module(const char *file); // System properties +rust::String get_prop_rs(const char *name, bool persist); std::string get_prop(const char *name, bool persist = false); int delete_prop(const char *name, bool persist = false); int set_prop(const char *name, const char *value, bool skip_svc = false); diff --git a/native/src/core/lib.rs b/native/src/core/lib.rs index 61647e497..84eeb26b0 100644 --- a/native/src/core/lib.rs +++ b/native/src/core/lib.rs @@ -1,12 +1,18 @@ pub use base; use daemon::*; use logging::*; +use std::ffi::CStr; mod daemon; mod logging; #[cxx::bridge] pub mod ffi { + extern "C++" { + include!("core.hpp"); + unsafe fn get_prop_rs(name: *const c_char, persist: bool) -> String; + } + extern "Rust" { fn rust_test_entry(); fn android_logging(); @@ -28,3 +34,7 @@ pub mod ffi { } fn rust_test_entry() {} + +pub fn get_prop(name: &CStr, persist: bool) -> String { + unsafe { ffi::get_prop_rs(name.as_ptr(), persist) } +} diff --git a/native/src/core/resetprop/persist.cpp b/native/src/core/resetprop/persist.cpp index 4eb8b873b..ea13d86f1 100644 --- a/native/src/core/resetprop/persist.cpp +++ b/native/src/core/resetprop/persist.cpp @@ -223,23 +223,22 @@ private: string_view _name; }; -string persist_get_prop(const char *name) { +void persist_get_prop(const char *name, prop_cb *prop_cb) { if (check_pb()) { match_prop_name cb(name); pb_get_prop(&cb); if (cb.value[0]) { LOGD("resetprop: get prop (persist) [%s]: [%s]\n", name, cb.value); - return cb.value; + prop_cb->exec(name, cb.value); } } else { // Try to read from file char value[PROP_VALUE_MAX]; if (file_get_prop(name, value)) { LOGD("resetprop: get prop (persist) [%s]: [%s]\n", name, value); - return value; + prop_cb->exec(name, value); } } - return ""; } bool persist_delete_prop(const char *name) { diff --git a/native/src/core/resetprop/resetprop.cpp b/native/src/core/resetprop/resetprop.cpp index 1ab277987..bc8c7045b 100644 --- a/native/src/core/resetprop/resetprop.cpp +++ b/native/src/core/resetprop/resetprop.cpp @@ -115,15 +115,19 @@ static void read_prop_with_cb(const prop_info *pi, void *cb) { } } +template 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; + StringType val; }; +template<> void prop_to_string::exec(const char *, const char *value) { + // We do not want to crash when values are not UTF-8 + val = rust::String::lossy(value); +} + static int set_prop(const char *name, const char *value, PropFlags flags) { if (!check_legal_property_name(name)) return 1; @@ -170,31 +174,33 @@ static int set_prop(const char *name, const char *value, PropFlags flags) { return ret; } -static string get_prop(const char *name, PropFlags flags) { +template +static StringType get_prop(const char *name, PropFlags flags) { if (!check_legal_property_name(name)) - return ""; + return {}; + + prop_to_string cb; if (flags.isContext()) { - auto val = __system_property_get_context(name) ?: ""; - LOGD("resetprop: prop context [%s]: [%s]\n", name, val); - return val; + auto context = __system_property_get_context(name) ?: ""; + LOGD("resetprop: prop context [%s]: [%s]\n", name, context); + cb.exec(name, context); + return cb.val; } - string val; if (!flags.isPersistOnly()) { if (auto pi = system_property_find(name)) { - prop_to_string cb(val); read_prop_with_cb(pi, &cb); - LOGD("resetprop: get prop [%s]: [%s]\n", name, val.data()); + LOGD("resetprop: get prop [%s]: [%s]\n", name, cb.val.c_str()); } } - if (val.empty() && flags.isPersist() && str_starts(name, "persist.")) - val = persist_get_prop(name); - - if (val.empty()) + if (cb.val.empty() && flags.isPersist() && str_starts(name, "persist.")) + persist_get_prop(name, &cb); + if (cb.val.empty()) LOGD("resetprop: prop [%s] does not exist\n", name); - return val; + + return cb.val; } static void print_props(PropFlags flags) { @@ -335,7 +341,7 @@ int resetprop_main(int argc, char *argv[]) { print_props(flags); return 0; case 1: { - string val = get_prop(argv[0], flags); + auto val = get_prop(argv[0], flags); if (val.empty()) return 1; printf("%s\n", val.data()); @@ -352,11 +358,20 @@ int resetprop_main(int argc, char *argv[]) { * Public APIs ****************/ -string get_prop(const char *name, bool persist) { +template +static StringType get_prop_impl(const char *name, bool persist) { InitOnce(); PropFlags flags; if (persist) flags.setPersist(); - return get_prop(name, flags); + return get_prop(name, flags); +} + +rust::String get_prop_rs(const char *name, bool persist) { + return get_prop_impl(name, persist); +} + +string get_prop(const char *name, bool persist) { + return get_prop_impl(name, persist); } int delete_prop(const char *name, bool persist) { diff --git a/native/src/core/resetprop/resetprop.hpp b/native/src/core/resetprop/resetprop.hpp index 01636e046..9a78c4b3e 100644 --- a/native/src/core/resetprop/resetprop.hpp +++ b/native/src/core/resetprop/resetprop.hpp @@ -21,7 +21,7 @@ private: prop_list &list; }; -std::string persist_get_prop(const char *name); +void persist_get_prop(const char *name, prop_cb *prop_cb); void persist_get_props(prop_cb *prop_cb); bool persist_delete_prop(const char *name); bool persist_set_prop(const char *name, const char *value);