mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-24 10:43:05 +00:00
Modernize db.hpp
This commit is contained in:
parent
4771c2810b
commit
5d162f81c4
@ -97,11 +97,12 @@ static bool dload_sqlite() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db_strings::getKeyIdx(string_view key) const {
|
int db_strings::get_idx(string_view key) const {
|
||||||
int idx = DB_STRING_NUM;
|
int idx = 0;
|
||||||
for (int i = 0; i < DB_STRING_NUM; ++i) {
|
for (const char *k : DB_STRING_KEYS) {
|
||||||
if (key == DB_STRING_KEYS[i])
|
if (key == k)
|
||||||
idx = i;
|
break;
|
||||||
|
++idx;
|
||||||
}
|
}
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
@ -114,11 +115,12 @@ db_settings::db_settings() {
|
|||||||
data[HIDE_CONFIG] = false;
|
data[HIDE_CONFIG] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int db_settings::getKeyIdx(string_view key) const {
|
int db_settings::get_idx(string_view key) const {
|
||||||
int idx = DB_SETTINGS_NUM;
|
int idx = 0;
|
||||||
for (int i = 0; i < DB_SETTINGS_NUM; ++i) {
|
for (const char *k : DB_SETTING_KEYS) {
|
||||||
if (key == DB_SETTING_KEYS[i])
|
if (key == k)
|
||||||
idx = i;
|
break;
|
||||||
|
++idx;
|
||||||
}
|
}
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
@ -256,6 +258,14 @@ char *db_exec(const char *sql) {
|
|||||||
return nullptr;
|
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 *db_exec(const char *sql, const db_row_cb &fn) {
|
||||||
char *err;
|
char *err;
|
||||||
if (mDB == nullptr) {
|
if (mDB == nullptr) {
|
||||||
@ -268,13 +278,7 @@ char *db_exec(const char *sql, const db_row_cb &fn) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (mDB) {
|
if (mDB) {
|
||||||
sqlite3_exec(mDB, sql, [](void *cb, int col_num, char **data, char **col_name) -> int {
|
sqlite3_exec(mDB, sql, sqlite_db_row_callback, (void *) &fn, &err);
|
||||||
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);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -289,10 +293,10 @@ int get_db_settings(db_settings &cfg, int key) {
|
|||||||
};
|
};
|
||||||
if (key >= 0) {
|
if (key >= 0) {
|
||||||
char query[128];
|
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);
|
err = db_exec(query, settings_cb);
|
||||||
} else {
|
} 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);
|
db_err_cmd(err, return 1);
|
||||||
return 0;
|
return 0;
|
||||||
@ -302,14 +306,15 @@ int get_db_strings(db_strings &str, int key) {
|
|||||||
char *err;
|
char *err;
|
||||||
auto string_cb = [&](db_row &row) -> bool {
|
auto string_cb = [&](db_row &row) -> bool {
|
||||||
str[row["key"]] = row["value"];
|
str[row["key"]] = row["value"];
|
||||||
|
LOGD("magiskdb: query %s=[%s]\n", row["key"].data(), row["value"].data());
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
if (key >= 0) {
|
if (key >= 0) {
|
||||||
char query[128];
|
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);
|
err = db_exec(query, string_cb);
|
||||||
} else {
|
} 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);
|
db_err_cmd(err, return 1);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -6,15 +6,15 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
template <class T, size_t num>
|
template <class T, size_t N>
|
||||||
class db_data_base {
|
class db_dict {
|
||||||
public:
|
public:
|
||||||
T& operator [](std::string_view key) {
|
T& operator [](std::string_view key) {
|
||||||
return data[getKeyIdx(key)];
|
return data[get_idx(key)];
|
||||||
}
|
}
|
||||||
|
|
||||||
const T& operator [](std::string_view key) const {
|
const T& operator [](std::string_view key) const {
|
||||||
return data[getKeyIdx(key)];
|
return data[get_idx(key)];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& operator [](int key) {
|
T& operator [](int key) {
|
||||||
@ -26,25 +26,22 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
T data[num + 1];
|
T data[N + 1];
|
||||||
virtual int getKeyIdx(std::string_view key) const = 0;
|
virtual int get_idx(std::string_view key) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/***************
|
/***************
|
||||||
* DB Settings *
|
* DB Settings *
|
||||||
***************/
|
***************/
|
||||||
|
|
||||||
#define DB_SETTING_KEYS \
|
constexpr const char *DB_SETTING_KEYS[] = {
|
||||||
((const char *[]) { \
|
"root_access",
|
||||||
"root_access", \
|
"multiuser_mode",
|
||||||
"multiuser_mode", \
|
"mnt_ns",
|
||||||
"mnt_ns", \
|
"magiskhide"
|
||||||
"magiskhide", \
|
};
|
||||||
})
|
|
||||||
|
|
||||||
#define DB_SETTINGS_NUM 4
|
// Settings key indices
|
||||||
|
|
||||||
// Settings keys
|
|
||||||
enum {
|
enum {
|
||||||
ROOT_ACCESS = 0,
|
ROOT_ACCESS = 0,
|
||||||
SU_MULTIUSER_MODE,
|
SU_MULTIUSER_MODE,
|
||||||
@ -74,33 +71,27 @@ enum {
|
|||||||
NAMESPACE_MODE_ISOLATE
|
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:
|
public:
|
||||||
db_settings();
|
db_settings();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int getKeyIdx(std::string_view key) const override;
|
int get_idx(std::string_view key) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**************
|
/**************
|
||||||
* DB Strings *
|
* DB Strings *
|
||||||
**************/
|
**************/
|
||||||
|
|
||||||
#define DB_STRING_KEYS \
|
constexpr const char *DB_STRING_KEYS[] = { "requester" };
|
||||||
((const char *[]) { \
|
|
||||||
"requester", \
|
|
||||||
})
|
|
||||||
|
|
||||||
#define DB_STRING_NUM 1
|
// Strings keys indices
|
||||||
|
|
||||||
// Strings keys
|
|
||||||
enum {
|
enum {
|
||||||
SU_MANAGER = 0
|
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:
|
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;
|
int notify;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define DEFAULT_SU_ACCESS (su_access) { \
|
#define DEFAULT_SU_ACCESS { QUERY, 1, 1 }
|
||||||
.policy = QUERY, \
|
#define SILENT_SU_ACCESS { ALLOW, 0, 0 }
|
||||||
.log = 1, \
|
#define NO_SU_ACCESS { DENY, 0, 0 }
|
||||||
.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 \
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************
|
/********************
|
||||||
* Public Functions *
|
* Public Functions *
|
||||||
********************/
|
********************/
|
||||||
|
|
||||||
typedef std::map<std::string_view, std::string_view> db_row;
|
using db_row = std::map<std::string_view, std::string_view>;
|
||||||
typedef std::function<bool(db_row&)> db_row_cb;
|
using db_row_cb = std::function<bool(db_row&)>;
|
||||||
|
|
||||||
int get_db_settings(db_settings &cfg, int key = -1);
|
int get_db_settings(db_settings &cfg, int key = -1);
|
||||||
int get_db_strings(db_strings &str, int key = -1);
|
int get_db_strings(db_strings &str, int key = -1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user