Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
5d162f81
Commit
5d162f81
authored
Aug 27, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modernize db.hpp
parent
4771c281
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
68 deletions
+50
-68
db.cpp
native/jni/core/db.cpp
+26
-21
db.hpp
native/jni/include/db.hpp
+24
-47
No files found.
native/jni/core/db.cpp
View file @
5d162f81
...
...
@@ -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
];
s
printf
(
query
,
"SELECT key, value
FROM settings WHERE key='%s'"
,
DB_SETTING_KEYS
[
key
]);
s
nprintf
(
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
];
s
printf
(
query
,
"SELECT key, value
FROM strings WHERE key='%s'"
,
DB_STRING_KEYS
[
key
]);
s
nprintf
(
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
;
...
...
native/jni/include/db.hpp
View file @
5d162f81
...
...
@@ -6,15 +6,15 @@
#include <string_view>
#include <functional>
template
<
class
T
,
size_t
num
>
class
db_d
ata_base
{
template
<
class
T
,
size_t
N
>
class
db_d
ict
{
public
:
T
&
operator
[](
std
::
string_view
key
)
{
return
data
[
get
KeyI
dx
(
key
)];
return
data
[
get
_i
dx
(
key
)];
}
const
T
&
operator
[](
std
::
string_view
key
)
const
{
return
data
[
get
KeyI
dx
(
key
)];
return
data
[
get
_i
dx
(
key
)];
}
T
&
operator
[](
int
key
)
{
...
...
@@ -26,25 +26,22 @@ public:
}
protected
:
T
data
[
num
+
1
];
virtual
int
get
KeyI
dx
(
std
::
string_view
key
)
const
=
0
;
T
data
[
N
+
1
];
virtual
int
get
_i
dx
(
std
::
string_view
key
)
const
=
0
;
};
/***************
* DB Settings *
***************/
#define DB_SETTING_KEYS \
((const char *[]) { \
"root_access", \
"multiuser_mode", \
"mnt_ns", \
"magiskhide", \
})
#define DB_SETTINGS_NUM 4
constexpr
const
char
*
DB_SETTING_KEYS
[]
=
{
"root_access"
,
"multiuser_mode"
,
"mnt_ns"
,
"magiskhide"
};
// Settings keys
// Settings key
indice
s
enum
{
ROOT_ACCESS
=
0
,
SU_MULTIUSER_MODE
,
...
...
@@ -74,33 +71,27 @@ enum {
NAMESPACE_MODE_ISOLATE
};
class
db_settings
:
public
db_d
ata_base
<
int
,
DB_SETTINGS_NUM
>
{
class
db_settings
:
public
db_d
ict
<
int
,
std
::
size
(
DB_SETTING_KEYS
)
>
{
public
:
db_settings
();
protected
:
int
get
KeyI
dx
(
std
::
string_view
key
)
const
override
;
int
get
_i
dx
(
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_d
ata_base
<
std
::
string
,
DB_STRING_NUM
>
{
class
db_strings
:
public
db_d
ict
<
std
::
string
,
std
::
size
(
DB_STRING_KEYS
)
>
{
protected
:
int
get
KeyI
dx
(
std
::
string_view
key
)
const
override
;
int
get
_i
dx
(
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment