mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-02-17 12:08:29 +00:00
Modernize db.hpp
This commit is contained in:
parent
4771c2810b
commit
5d162f81c4
@ -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<const db_row_cb*>(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<const db_row_cb*>(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;
|
||||
|
@ -6,15 +6,15 @@
|
||||
#include <string_view>
|
||||
#include <functional>
|
||||
|
||||
template <class T, size_t num>
|
||||
class db_data_base {
|
||||
template <class T, size_t N>
|
||||
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<int, DB_SETTINGS_NUM> {
|
||||
class db_settings : public db_dict<int, std::size(DB_SETTING_KEYS)> {
|
||||
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<std::string, DB_STRING_NUM> {
|
||||
class db_strings : public db_dict<std::string, std::size(DB_STRING_KEYS)> {
|
||||
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<std::string_view, std::string_view> db_row;
|
||||
typedef std::function<bool(db_row&)> db_row_cb;
|
||||
using db_row = std::map<std::string_view, std::string_view>;
|
||||
using db_row_cb = std::function<bool(db_row&)>;
|
||||
|
||||
int get_db_settings(db_settings &cfg, int key = -1);
|
||||
int get_db_strings(db_strings &str, int key = -1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user