From 5d162f81c4cc7cb6f787472b779f8ea92718c643 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 27 Aug 2021 01:06:03 -0700 Subject: [PATCH] Modernize db.hpp --- native/jni/core/db.cpp | 47 ++++++++++++++------------ native/jni/include/db.hpp | 71 +++++++++++++-------------------------- 2 files changed, 50 insertions(+), 68 deletions(-) diff --git a/native/jni/core/db.cpp b/native/jni/core/db.cpp index bda1e7b03..49f6bee36 100644 --- a/native/jni/core/db.cpp +++ b/native/jni/core/db.cpp @@ -97,11 +97,12 @@ static bool dload_sqlite() { return true; } -int db_strings::getKeyIdx(string_view key) const { - int idx = DB_STRING_NUM; - for (int i = 0; i < DB_STRING_NUM; ++i) { - if (key == DB_STRING_KEYS[i]) - idx = i; +int db_strings::get_idx(string_view key) const { + int idx = 0; + for (const char *k : DB_STRING_KEYS) { + if (key == k) + break; + ++idx; } return idx; } @@ -114,11 +115,12 @@ db_settings::db_settings() { data[HIDE_CONFIG] = false; } -int db_settings::getKeyIdx(string_view key) const { - int idx = DB_SETTINGS_NUM; - for (int i = 0; i < DB_SETTINGS_NUM; ++i) { - if (key == DB_SETTING_KEYS[i]) - idx = i; +int db_settings::get_idx(string_view key) const { + int idx = 0; + for (const char *k : DB_SETTING_KEYS) { + if (key == k) + break; + ++idx; } return idx; } @@ -256,6 +258,14 @@ char *db_exec(const char *sql) { return nullptr; } +static int sqlite_db_row_callback(void *cb, int col_num, char **data, char **col_name) { + auto &func = *static_cast(cb); + db_row row; + for (int i = 0; i < col_num; ++i) + row[col_name[i]] = data[i]; + return func(row) ? 0 : 1; +} + char *db_exec(const char *sql, const db_row_cb &fn) { char *err; if (mDB == nullptr) { @@ -268,13 +278,7 @@ char *db_exec(const char *sql, const db_row_cb &fn) { ); } if (mDB) { - sqlite3_exec(mDB, sql, [](void *cb, int col_num, char **data, char **col_name) -> int { - auto &func = *reinterpret_cast(cb); - db_row row; - for (int i = 0; i < col_num; ++i) - row[col_name[i]] = data[i]; - return func(row) ? 0 : 1; - }, (void *) &fn, &err); + sqlite3_exec(mDB, sql, sqlite_db_row_callback, (void *) &fn, &err); return err; } return nullptr; @@ -289,10 +293,10 @@ int get_db_settings(db_settings &cfg, int key) { }; if (key >= 0) { char query[128]; - sprintf(query, "SELECT key, value FROM settings WHERE key='%s'", DB_SETTING_KEYS[key]); + snprintf(query, sizeof(query), "SELECT * FROM settings WHERE key='%s'", DB_SETTING_KEYS[key]); err = db_exec(query, settings_cb); } else { - err = db_exec("SELECT key, value FROM settings", settings_cb); + err = db_exec("SELECT * FROM settings", settings_cb); } db_err_cmd(err, return 1); return 0; @@ -302,14 +306,15 @@ int get_db_strings(db_strings &str, int key) { char *err; auto string_cb = [&](db_row &row) -> bool { str[row["key"]] = row["value"]; + LOGD("magiskdb: query %s=[%s]\n", row["key"].data(), row["value"].data()); return true; }; if (key >= 0) { char query[128]; - sprintf(query, "SELECT key, value FROM strings WHERE key='%s'", DB_STRING_KEYS[key]); + snprintf(query, sizeof(query), "SELECT * FROM strings WHERE key='%s'", DB_STRING_KEYS[key]); err = db_exec(query, string_cb); } else { - err = db_exec("SELECT key, value FROM strings", string_cb); + err = db_exec("SELECT * FROM strings", string_cb); } db_err_cmd(err, return 1); return 0; diff --git a/native/jni/include/db.hpp b/native/jni/include/db.hpp index b03f45a85..c2294be77 100644 --- a/native/jni/include/db.hpp +++ b/native/jni/include/db.hpp @@ -6,15 +6,15 @@ #include #include -template -class db_data_base { +template +class db_dict { public: T& operator [](std::string_view key) { - return data[getKeyIdx(key)]; + return data[get_idx(key)]; } const T& operator [](std::string_view key) const { - return data[getKeyIdx(key)]; + return data[get_idx(key)]; } T& operator [](int key) { @@ -26,25 +26,22 @@ public: } protected: - T data[num + 1]; - virtual int getKeyIdx(std::string_view key) const = 0; + T data[N + 1]; + virtual int get_idx(std::string_view key) const = 0; }; /*************** * DB Settings * ***************/ -#define DB_SETTING_KEYS \ -((const char *[]) { \ -"root_access", \ -"multiuser_mode", \ -"mnt_ns", \ -"magiskhide", \ -}) +constexpr const char *DB_SETTING_KEYS[] = { + "root_access", + "multiuser_mode", + "mnt_ns", + "magiskhide" +}; -#define DB_SETTINGS_NUM 4 - -// Settings keys +// Settings key indices enum { ROOT_ACCESS = 0, SU_MULTIUSER_MODE, @@ -74,33 +71,27 @@ enum { NAMESPACE_MODE_ISOLATE }; -class db_settings : public db_data_base { +class db_settings : public db_dict { public: db_settings(); - protected: - int getKeyIdx(std::string_view key) const override; + int get_idx(std::string_view key) const override; }; /************** * DB Strings * **************/ -#define DB_STRING_KEYS \ -((const char *[]) { \ -"requester", \ -}) +constexpr const char *DB_STRING_KEYS[] = { "requester" }; -#define DB_STRING_NUM 1 - -// Strings keys +// Strings keys indices enum { SU_MANAGER = 0 }; -class db_strings : public db_data_base { +class db_strings : public db_dict { protected: - int getKeyIdx(std::string_view key) const override; + int get_idx(std::string_view key) const override; }; /************* @@ -119,30 +110,16 @@ struct su_access { int notify; }; -#define DEFAULT_SU_ACCESS (su_access) { \ -.policy = QUERY, \ -.log = 1, \ -.notify = 1 \ -} - -#define SILENT_SU_ACCESS (su_access) { \ -.policy = ALLOW, \ -.log = 0, \ -.notify = 0 \ -} - -#define NO_SU_ACCESS (su_access) { \ -.policy = DENY, \ -.log = 0, \ -.notify = 0 \ -} +#define DEFAULT_SU_ACCESS { QUERY, 1, 1 } +#define SILENT_SU_ACCESS { ALLOW, 0, 0 } +#define NO_SU_ACCESS { DENY, 0, 0 } /******************** * Public Functions * ********************/ -typedef std::map db_row; -typedef std::function db_row_cb; +using db_row = std::map; +using db_row_cb = std::function; int get_db_settings(db_settings &cfg, int key = -1); int get_db_strings(db_strings &str, int key = -1);