-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathenv_service.cpp
More file actions
205 lines (179 loc) · 5.97 KB
/
env_service.cpp
File metadata and controls
205 lines (179 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Pterodon Recovery - env handler
Copyright (C) <2020> ATGDroid <bythedroid@gmail.com>
This file is part of Pterodon Recovery Project
Pterodon Recovery is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pterodon Recovery is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Pterodon Recovery. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <stdlib.h>
#include <vector>
#include "strdiv.hpp"
#include "env_service.hpp"
#include "constants.h"
#include "framework.hpp"
#include "version.hpp"
/*
* communication bridge between the paths defined by
* environment variables and a recovery
* Created by ATGDroid (https://t.me/ATGDroid)
*/
static struct pterodon_env {
uint8_t type;
const char *varName;
const std::string default_value;
} table[] = {
{ PTERO_ANDROID_ROOT, "ANDROID_ROOT", "/system" },
{ PTERO_ANDROID_DATA, "ANDROID_DATA", "/data" },
{ PTERO_ANDROID_CACHE, "ANDROID_CACHE", "/cache" },
{ PTERO_EXTERNAL_STORAGE, "EXTERNAL_STORAGE", "/sdcard" },
{ PTERO_ANDROID_STORAGE, "ANDROID_STORAGE", "/storage" },
{ PTERO_EXECUTABLES_PATH, "PATH", "/sbin:/system/bin:/system/xbin:/vendor/bin" },
{ PTERO_LDLIB_PATH, "LD_LIBRARY_PATH", "/sbin" },
{ PTERO_TMP_FOLDER, "TMPDIR", "/tmp" },
{ PTERO_VERSION, "PTERODON_VERSION", Pterodon::VersionManager::GetInfo(PTERODON_INFO_VERSION) },
{ PTERO_CODENAME, "PTERODON_CODENAME", Pterodon::VersionManager::GetInfo(PTERODON_INFO_CODENAME) },
{ 0, 0, ""},
};
static bool use_default_value = false;
namespace Pterodon {
std::string Environment::GetVar(uint8_t kEnv) {
const struct pterodon_env *environment = table;
while (environment->type) {
if (environment->type == kEnv) {
if (use_default_value)
return environment->default_value;
const char* env = getenv(environment->varName);
return (env || env[0] != '\0') ? std::string(env) : environment->default_value;
}
environment++;
}
return "";
}
std::string Environment::GetVar(const std::string& varName) {
const char* env = getenv(varName.c_str());
return (env || env[0] != '\0') ? std::string(env) : "";
}
bool Environment::VarExists(uint8_t kEnv) {
const struct pterodon_env *environment = table;
while (environment->type) {
if (environment->type == kEnv) {
const char* env = getenv(environment->varName);
return (env || env[0] != '\0') ? true : false;
}
environment++;
}
return false;
}
bool Environment::VarExists(const std::string& varName) {
const char* env = getenv(varName.c_str());
return (env || env[0] != '\0') ? true : false;
}
void Environment::InitializeVariables(void) {
const struct pterodon_env *environment = table;
while (environment->type) {
setenv(environment->varName, environment->default_value.c_str(), 0);
environment++;
}
}
void Environment::LogVariables(void) {
const struct pterodon_env *environment = table;
while (environment->type) {
//if (VarExists(environment->type))
LOGI("'%s' = '%s'", environment->varName, GetVar(environment->type).c_str());
environment++;
}
}
void Environment::SetVar(uint8_t kEnv, const std::string& newVar) {
const struct pterodon_env *environment = table;
while (environment->type) {
if (environment->type == kEnv) {
setenv(environment->varName, newVar.c_str(), 1);
// if (environment->type == PTERO_TRUST_ZONE)
/// tzset();
return;
}
environment++;
}
}
void Environment::SetVar(const std::string& varName, const std::string& varValue) {
setenv(varName.c_str(), varValue.c_str(), 1);
if (varName == "TZ") {
LOGI("Changing current timezone to %s", varValue.c_str());
tzset();
}
}
void Environment::RemoveVar(uint8_t kEnv) {
const struct pterodon_env *environment = table;
while (environment->type) {
if (environment->type == kEnv) {
unsetenv(environment->varName);
return;
}
environment++;
}
}
void Environment::RemoveVar(const std::string& varName) {
unsetenv(varName.c_str());
}
/* which $name */
std::string Environment::LocateExecutable(const std::string& name) {
for (const uint8_t env_type : {PTERO_EXECUTABLES_PATH, PTERO_LDLIB_PATH, PTERO_TMP_FOLDER}) {
std::string path_env = GetVar(env_type);
if (path_env.empty())
continue;
pterodon_str_divider div;
std::string split_path, full_path;
while (div.Split(path_env, ":", split_path)) {
full_path = split_path + "/" + name;
if (Pterodon::Framework::FileExists(full_path))
return full_path;
}
}
return "";
}
/* which -a $name */
std::vector<std::string> Environment::LocateExecutableAll(const std::string& name) {
std::vector<std::string> ret;
for (const uint8_t env_type : {PTERO_EXECUTABLES_PATH, PTERO_LDLIB_PATH, PTERO_TMP_FOLDER}) {
std::string path_env = GetVar(env_type);
if (path_env.empty())
continue;
pterodon_str_divider div;
std::string split_path, full_path;
while (div.Split(path_env, ":", split_path)) {
full_path = split_path + "/" + name;
if (Pterodon::Framework::FileExists(full_path))
ret.push_back(full_path);
}
}
return ret;
}
bool Environment::ExecutableExists(const std::string& name) {
return (!LocateExecutable(name).empty());
}
std::string Environment::TranslatePath(const std::string& path) {
if (use_default_value) return path;
const struct pterodon_env *environment = table;
while (environment->type) {
if (environment->default_value == path) {
const char* env = getenv(environment->varName);
return (env || env[0] != '\0') ? std::string(env) : environment->default_value;
}
environment++;
}
return path;
}
void Environment::prefer_default_values(bool val)
{
use_default_value = val;
}
} // namespace Pterodon