1 Star 0 Fork 1

10808012/TWRP_CN

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
data.cpp 36.63 KB
一键复制 编辑 原始数据 按行查看 历史
syhost 提交于 2012-11-26 23:54 . first commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/input.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <map>
#include <fstream>
#include <sstream>
#include "variables.h"
#include "data.hpp"
#include "partitions.hpp"
#include "twrp-functions.hpp"
extern "C"
{
#include "common.h"
#include "data.h"
#include "gui/pages.h"
void gui_notifyVarChange(const char *name, const char* value);
}
#define FILE_VERSION 0x00010001
using namespace std;
map<string, DataManager::TStrIntPair> DataManager::mValues;
map<string, string> DataManager::mConstValues;
string DataManager::mBackingFile;
int DataManager::mInitialized = 0;
// Device ID functions
void DataManager::sanitize_device_id(char* device_id) {
const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._";
char str[50];
char* c = str;
strcpy(str, device_id);
memset(device_id, 0, sizeof(device_id));
while (*c) {
if (strchr(whitelist, *c))
strncat(device_id, c, 1);
c++;
}
return;
}
#define CMDLINE_SERIALNO "androidboot.serialno="
#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
#define CPUINFO_SERIALNO "Serial"
#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
#define CPUINFO_HARDWARE "Hardware"
#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
void DataManager::get_device_id(void) {
FILE *fp;
char line[2048];
char hardware_id[32], device_id[64];
char* token;
// Assign a blank device_id to start with
device_id[0] = 0;
#ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID
// First, try the cmdline to see if the serial number was supplied
fp = fopen("/proc/cmdline", "rt");
if (fp != NULL)
{
// First step, read the line. For cmdline, it's one long line
fgets(line, sizeof(line), fp);
fclose(fp);
// Now, let's tokenize the string
token = strtok(line, " ");
// Let's walk through the line, looking for the CMDLINE_SERIALNO token
while (token)
{
// We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
{
// We found the serial number!
strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
sanitize_device_id((char *)device_id);
mConstValues.insert(make_pair("device_id", device_id));
return;
}
token = strtok(NULL, " ");
}
}
#endif
// Now we'll try cpuinfo for a serial number
fp = fopen("/proc/cpuinfo", "rt");
if (fp != NULL)
{
while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
{
// We found the serial number!
token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
if (*token != 0) {
token[30] = 0;
if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
memset(device_id, 0, sizeof(device_id));
strncpy(device_id, token, strlen(token) - 1);
} else {
strcpy(device_id, token);
}
LOGI("=> serial from cpuinfo: '%s'\n", device_id);
fclose(fp);
sanitize_device_id((char *)device_id);
mConstValues.insert(make_pair("device_id", device_id));
return;
}
} else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
// We found the hardware ID
token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
if (*token != 0) {
token[30] = 0;
if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
memset(hardware_id, 0, sizeof(hardware_id));
strncpy(hardware_id, token, strlen(token) - 1);
} else {
strcpy(hardware_id, token);
}
LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id);
}
}
}
fclose(fp);
}
if (hardware_id[0] != 0) {
LOGW("\nusing hardware id for device id: '%s'\n", hardware_id);
strcpy(device_id, hardware_id);
sanitize_device_id((char *)device_id);
mConstValues.insert(make_pair("device_id", device_id));
return;
}
strcpy(device_id, "serialno");
LOGE("=> device id not found, using '%s'.", device_id);
mConstValues.insert(make_pair("device_id", device_id));
return;
}
int DataManager::ResetDefaults()
{
mValues.clear();
mConstValues.clear();
SetDefaultValues();
return 0;
}
int DataManager::LoadValues(const string filename)
{
string str, dev_id;
if (!mInitialized)
SetDefaultValues();
GetValue("device_id", dev_id);
// Save off the backing file for set operations
mBackingFile = filename;
// Read in the file, if possible
FILE* in = fopen(filename.c_str(), "rb");
if (!in) return 0;
int file_version;
if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error;
if (file_version != FILE_VERSION) goto error;
while (!feof(in))
{
string Name;
string Value;
unsigned short length;
char array[512];
if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
if (length >= 512) goto error;
if (fread(array, 1, length, in) != length) goto error;
Name = array;
if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
if (length >= 512) goto error;
if (fread(array, 1, length, in) != length) goto error;
Value = array;
map<string, TStrIntPair>::iterator pos;
pos = mValues.find(Name);
if (pos != mValues.end())
{
pos->second.first = Value;
pos->second.second = 1;
}
else
mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1)));
}
fclose(in);
str = GetCurrentStoragePath();
str += "/TWRP/BACKUPS/";
str += dev_id;
SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
return 0;
error:
// File version mismatch. Use defaults.
fclose(in);
str = GetCurrentStoragePath();
str += "/TWRP/BACKUPS/";
str += dev_id;
SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
return -1;
}
int DataManager::Flush()
{
return SaveValues();
}
int DataManager::SaveValues()
{
if (mBackingFile.empty()) return -1;
string mount_path = GetSettingsStoragePath();
PartitionManager.Mount_By_Path(mount_path.c_str(), 1);
FILE* out = fopen(mBackingFile.c_str(), "wb");
if (!out) return -1;
int file_version = FILE_VERSION;
fwrite(&file_version, 1, sizeof(int), out);
map<string, TStrIntPair>::iterator iter;
for (iter = mValues.begin(); iter != mValues.end(); ++iter)
{
// Save only the persisted data
if (iter->second.second != 0)
{
unsigned short length = (unsigned short) iter->first.length() + 1;
fwrite(&length, 1, sizeof(unsigned short), out);
fwrite(iter->first.c_str(), 1, length, out);
length = (unsigned short) iter->second.first.length() + 1;
fwrite(&length, 1, sizeof(unsigned short), out);
fwrite(iter->second.first.c_str(), 1, length, out);
}
}
fclose(out);
return 0;
}
int DataManager::GetValue(const string varName, string& value)
{
string localStr = varName;
if (!mInitialized)
SetDefaultValues();
// Strip off leading and trailing '%' if provided
if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%')
{
localStr.erase(0, 1);
localStr.erase(localStr.length() - 1, 1);
}
// Handle magic values
if (GetMagicValue(localStr, value) == 0) return 0;
map<string, string>::iterator constPos;
constPos = mConstValues.find(localStr);
if (constPos != mConstValues.end())
{
value = constPos->second;
return 0;
}
map<string, TStrIntPair>::iterator pos;
pos = mValues.find(localStr);
if (pos == mValues.end())
return -1;
value = pos->second.first;
return 0;
}
int DataManager::GetValue(const string varName, int& value)
{
string data;
if (GetValue(varName,data) != 0)
return -1;
value = atoi(data.c_str());
return 0;
}
// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str
string& DataManager::GetValueRef(const string varName)
{
if (!mInitialized)
SetDefaultValues();
map<string, string>::iterator constPos;
constPos = mConstValues.find(varName);
if (constPos != mConstValues.end())
return constPos->second;
map<string, TStrIntPair>::iterator pos;
pos = mValues.find(varName);
if (pos == mValues.end())
pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first;
return pos->second.first;
}
// This function will return an empty string if the value doesn't exist
string DataManager::GetStrValue(const string varName)
{
string retVal;
GetValue(varName, retVal);
return retVal;
}
// This function will return 0 if the value doesn't exist
int DataManager::GetIntValue(const string varName)
{
string retVal;
GetValue(varName, retVal);
return atoi(retVal.c_str());
}
int DataManager::SetValue(const string varName, string value, int persist /* = 0 */)
{
if (!mInitialized)
SetDefaultValues();
// Don't allow empty values or numerical starting values
if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
return -1;
map<string, string>::iterator constChk;
constChk = mConstValues.find(varName);
if (constChk != mConstValues.end())
return -1;
map<string, TStrIntPair>::iterator pos;
pos = mValues.find(varName);
if (pos == mValues.end())
pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first;
else
pos->second.first = value;
if (pos->second.second != 0)
SaveValues();
gui_notifyVarChange(varName.c_str(), value.c_str());
return 0;
}
int DataManager::SetValue(const string varName, int value, int persist /* = 0 */)
{
ostringstream valStr;
valStr << value;
if (varName == "tw_use_external_storage") {
string str;
if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
if (value == 0) {
str = GetStrValue(TW_INTERNAL_PATH);
} else {
str = GetStrValue(TW_EXTERNAL_PATH);
}
} else if (GetIntValue(TW_HAS_INTERNAL) == 1)
str = GetStrValue(TW_INTERNAL_PATH);
else
str = GetStrValue(TW_EXTERNAL_PATH);
string dev_id;
GetValue("device_id", dev_id);
str += "/TWRP/BACKUPS/";
str += dev_id;
SetValue(TW_BACKUPS_FOLDER_VAR, str);
}
return SetValue(varName, valStr.str(), persist);;
}
int DataManager::SetValue(const string varName, float value, int persist /* = 0 */)
{
ostringstream valStr;
valStr << value;
return SetValue(varName, valStr.str(), persist);;
}
void DataManager::DumpValues()
{
map<string, TStrIntPair>::iterator iter;
ui_print("Data Manager dump - Values with leading X are persisted.\n");
for (iter = mValues.begin(); iter != mValues.end(); ++iter)
{
ui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str());
}
}
void DataManager::update_tz_environment_variables(void) {
setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
tzset();
}
void DataManager::SetBackupFolder()
{
string str = GetCurrentStoragePath();
str += "/TWRP/BACKUPS/";
string dev_id;
GetValue("device_id", dev_id);
str += dev_id;
SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
}
void DataManager::SetDefaultValues()
{
string str, path;
get_device_id();
mInitialized = 1;
mConstValues.insert(make_pair("true", "1"));
mConstValues.insert(make_pair("false", "0"));
mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR));
#ifdef BOARD_HAS_NO_REAL_SDCARD
mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0"));
#else
mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1"));
#endif
#ifdef TW_INCLUDE_DUMLOCK
mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1"));
#else
mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0"));
#endif
#ifdef TW_INTERNAL_STORAGE_PATH
LOGI("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH));
mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1)));
mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair(EXPAND(TW_INTERNAL_STORAGE_PATH), 0)));
mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT)));
path.clear();
path = "/";
path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT);
mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path));
#ifdef TW_EXTERNAL_STORAGE_PATH
LOGI("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
// Device has dual storage
mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
path.clear();
path = "/";
path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
} else {
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
}
#else
LOGI("Just has internal storage.\n");
// Just has internal storage
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0"));
mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0"));
mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0"));
#endif
#else
#ifdef RECOVERY_SDCARD_ON_DATA
#ifdef TW_EXTERNAL_STORAGE_PATH
LOGI("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
// Device has /data/media + external storage
mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1"));
#else
LOGI("Single storage only -- data/media.\n");
// Device just has external storage
mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0"));
#endif
#else
LOGI("Single storage only.\n");
// Device just has external storage
mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0"));
#endif
#ifdef RECOVERY_SDCARD_ON_DATA
LOGI("Device has /data/media defined.\n");
// Device has /data/media
mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0"));
mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1"));
mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("/data/media", 0)));
mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data"));
mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data"));
#ifdef TW_EXTERNAL_STORAGE_PATH
if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1)));
} else {
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
}
#else
mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1)));
#endif
#else
LOGI("No internal storage defined.\n");
// Device has no internal storage
mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1"));
mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0"));
mValues.insert(make_pair(TW_INTERNAL_PATH, make_pair("0", 0)));
mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0"));
mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0"));
#endif
#ifdef TW_EXTERNAL_STORAGE_PATH
LOGI("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH));
// External has custom definition
mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH)));
mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT)));
mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1)));
path.clear();
path = "/";
path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT);
mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path));
#else
#ifndef RECOVERY_SDCARD_ON_DATA
LOGI("No storage defined, defaulting to /sdcard.\n");
// Standard external definition
mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1"));
mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard"));
mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard"));
mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard"));
mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1)));
#endif
#endif
#endif
#ifdef TW_DEFAULT_EXTERNAL_STORAGE
SetValue(TW_USE_EXTERNAL_STORAGE, 1);
LOGI("Defaulting to external storage.\n");
#endif
#ifdef RECOVERY_SDCARD_ON_DATA
if (PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists("/data/media/0"))
SetValue(TW_INTERNAL_PATH, "/data/media/0");
#endif
str = GetCurrentStoragePath();
#ifdef RECOVERY_SDCARD_ON_DATA
#ifndef TW_EXTERNAL_STORAGE_PATH
SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
#else
if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1);
} else {
SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1);
}
#endif
#else
SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1);
#endif
str += "/TWRP/BACKUPS/";
string dev_id;
GetValue("device_id", dev_id);
str += dev_id;
SetValue(TW_BACKUPS_FOLDER_VAR, str, 0);
#ifdef SP1_DISPLAY_NAME
if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME)));
#else
#ifdef SP1_NAME
if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME)));
#endif
#endif
#ifdef SP2_DISPLAY_NAME
if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME)));
#else
#ifdef SP2_NAME
if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP2_NAME)));
#endif
#endif
#ifdef SP3_DISPLAY_NAME
if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME)));
#else
#ifdef SP3_NAME
if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP3_NAME)));
#endif
#endif
mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1"));
#ifdef TW_NO_REBOOT_RECOVERY
mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0"));
#else
mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1"));
#endif
mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1"));
#ifdef TW_NO_REBOOT_BOOTLOADER
mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0"));
#else
mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1"));
#endif
#ifdef RECOVERY_SDCARD_ON_DATA
mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1"));
#else
mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0"));
#endif
#ifdef TW_NO_BATT_PERCENT
mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1"));
#else
mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0"));
#endif
#ifdef TW_CUSTOM_POWER_BUTTON
mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON)));
#else
mConstValues.insert(make_pair(TW_POWER_BUTTON, "0"));
#endif
#ifdef TW_ALWAYS_RMRF
mConstValues.insert(make_pair(TW_RM_RF_VAR, "1"));
#endif
#ifdef TW_NEVER_UNMOUNT_SYSTEM
mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1"));
#else
mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0"));
#endif
#ifdef TW_NO_USB_STORAGE
mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
#else
mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
#endif
#ifdef TW_INCLUDE_INJECTTWRP
mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1"));
mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1)));
#else
mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0"));
mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1)));
#endif
#ifdef TW_FLASH_FROM_STORAGE
mConstValues.insert(make_pair(TW_FLASH_ZIP_IN_PLACE, "1"));
#endif
#ifdef TW_HAS_DOWNLOAD_MODE
mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1"));
#endif
#ifdef TW_INCLUDE_CRYPTO
mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1"));
LOGI("Device has crypto support compiled into recovery.\n");
#endif
#ifdef TW_SDEXT_NO_EXT4
mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1"));
#else
mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0"));
#endif
mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE));
mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0)));
mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1)));
mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1)));
mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1)));
mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0)));
mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_IGNORE_IMAGE_SIZE, make_pair("0", 1)));
mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1)));
mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1)));
mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1)));
mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1)));
mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1)));
mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1)));
mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1)));
mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1)));
mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1)));
mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0)));
mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1)));
mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1)));
mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1)));
mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1)));
mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1)));
if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0)
SetValue(TW_HAS_USB_STORAGE, 0, 0);
else
SetValue(TW_HAS_USB_STORAGE, 1, 0);
mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0)));
mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0)));
mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0)));
mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1)));
mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1)));
mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0)));
mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0)));
mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0)));
mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0)));
mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0)));
mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0)));
mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0)));
}
// Magic Values
int DataManager::GetMagicValue(const string varName, string& value)
{
// Handle special dynamic cases
if (varName == "tw_time")
{
char tmp[32];
struct tm *current;
time_t now;
now = time(0);
current = localtime(&now);
if (current->tm_hour >= 12)
sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
else
sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
value = tmp;
return 0;
}
if (varName == "tw_time_cn")
{
char tmp[32];
struct tm *current;
time_t now;
now = time(0);
current = localtime(&now);
if (current->tm_hour >= 12)
//sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
sprintf(tmp, "%d:%02d下午", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min);
else
//sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
sprintf(tmp, "%d:%02d上午", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min);
value = tmp;
return 0;
}
if (varName == "tw_battery")
{
char tmp[16];
static char charging = ' ';
static int lastVal = -1;
static time_t nextSecCheck = 0;
struct timeval curTime;
gettimeofday(&curTime, NULL);
if (curTime.tv_sec > nextSecCheck)
{
char cap_s[4];
FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
if (!cap)
cap = fopen("/sys/class/power_supply/batterys/capacity","rt");
if (cap){
fgets(cap_s, 4, cap);
fclose(cap);
lastVal = atoi(cap_s);
if (lastVal > 100) lastVal = 101;
if (lastVal < 0) lastVal = 0;
}
cap = fopen("/sys/class/power_supply/battery/status","rt");
if (cap) {
fgets(cap_s, 2, cap);
fclose(cap);
if (cap_s[0] == 'C')
charging = '+';
else
charging = ' ';
}
nextSecCheck = curTime.tv_sec + 60;
}
sprintf(tmp, "%i%%%c", lastVal, charging);
value = tmp;
return 0;
}
if (varName == "tw_battery_cn")
{
char tmp[16];
static char charging = ' ';
static int lastVal = -1;
static time_t nextSecCheck = 0;
struct timeval curTime;
gettimeofday(&curTime, NULL);
if (curTime.tv_sec > nextSecCheck)
{
char cap_s[4];
FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
if (!cap)
cap = fopen("/sys/class/power_supply/batterys/capacity","rt");
if (cap){
fgets(cap_s, 4, cap);
fclose(cap);
lastVal = atoi(cap_s);
if (lastVal > 100) lastVal = 101;
if (lastVal < 0) lastVal = 0;
}
cap = fopen("/sys/class/power_supply/battery/status","rt");
if (cap) {
fgets(cap_s, 2, cap);
fclose(cap);
if (cap_s[0] == 'C')
//charging = '+';
charging = 1;
else
//charging = ' ';
charging = 0;
}
nextSecCheck = curTime.tv_sec + 60;
}
sprintf(tmp, "%i%%%s", lastVal, charging?" 充电中...":" ");
value = tmp;
return 0;
}
return -1;
}
void DataManager::Output_Version(void) {
string Path, Command;
char version[255];
Path = DataManager::GetSettingsStoragePath();
if (!PartitionManager.Mount_By_Path(Path, false)) {
LOGI("Unable to mount '%s' to write version number.\n", Path.c_str());
return;
}
Path += "/TWRP/.version";
if (TWFunc::Path_Exists(Path)) {
Command = "rm -f " + Path;
system(Command.c_str());
}
FILE *fp = fopen(Path.c_str(), "w");
if (fp == NULL) {
LOGE("Unable to open '%s'.\n", Path.c_str());
return;
}
strcpy(version, TW_VERSION_STR);
fwrite(version, sizeof(version[0]), strlen(version) / sizeof(version[0]), fp);
fclose(fp);
sync();
LOGI("Version number saved to '%s'\n", Path.c_str());
}
void DataManager::ReadSettingsFile(void)
{
// Load up the values for TWRP - Sleep to let the card be ready
char mkdir_path[255], settings_file[255];
int is_enc, has_dual, use_ext, has_data_media, has_ext;
GetValue(TW_IS_ENCRYPTED, is_enc);
GetValue(TW_HAS_DATA_MEDIA, has_data_media);
if (is_enc == 1 && has_data_media == 1) {
LOGI("Cannot load settings -- encrypted.\n");
return;
}
memset(mkdir_path, 0, sizeof(mkdir_path));
memset(settings_file, 0, sizeof(settings_file));
sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath());
sprintf(settings_file, "%s/.twrps", mkdir_path);
if (!PartitionManager.Mount_Settings_Storage(false))
{
usleep(500000);
if (!PartitionManager.Mount_Settings_Storage(false))
LOGE("Unable to mount %s when trying to read settings file.\n", DataManager_GetSettingsStorageMount());
}
mkdir(mkdir_path, 0777);
LOGI("Attempt to load settings from settings file...\n");
LoadValues(settings_file);
Output_Version();
GetValue(TW_HAS_DUAL_STORAGE, has_dual);
GetValue(TW_USE_EXTERNAL_STORAGE, use_ext);
GetValue(TW_HAS_EXTERNAL, has_ext);
if (has_dual != 0 && use_ext == 1) {
// Attempt to switch to using external storage
if (!PartitionManager.Mount_Current_Storage(false)) {
LOGE("Failed to mount external storage, using internal storage.\n");
// Remount failed, default back to internal storage
SetValue(TW_USE_EXTERNAL_STORAGE, 0);
PartitionManager.Mount_Current_Storage(true);
string int_zip_path;
GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path);
SetValue(TW_USE_EXTERNAL_STORAGE, 0);
SetValue(TW_ZIP_LOCATION_VAR, int_zip_path);
}
} else {
PartitionManager.Mount_Current_Storage(true);
}
if (has_ext) {
string ext_path;
GetValue(TW_EXTERNAL_PATH, ext_path);
PartitionManager.Mount_By_Path(ext_path, 0);
}
update_tz_environment_variables();
}
string DataManager::GetCurrentStoragePath(void)
{
if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
return GetStrValue(TW_INTERNAL_PATH);
else
return GetStrValue(TW_EXTERNAL_PATH);
} else if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetStrValue(TW_INTERNAL_PATH);
else
return GetStrValue(TW_EXTERNAL_PATH);
}
string& DataManager::CGetCurrentStoragePath()
{
if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
return GetValueRef(TW_INTERNAL_PATH);
else
return GetValueRef(TW_EXTERNAL_PATH);
} else if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetValueRef(TW_INTERNAL_PATH);
else
return GetValueRef(TW_EXTERNAL_PATH);
}
string DataManager::GetCurrentStorageMount(void)
{
if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
return GetStrValue(TW_INTERNAL_MOUNT);
else
return GetStrValue(TW_EXTERNAL_MOUNT);
} else if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetStrValue(TW_INTERNAL_MOUNT);
else
return GetStrValue(TW_EXTERNAL_MOUNT);
}
string& DataManager::CGetCurrentStorageMount()
{
if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) {
if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0)
return GetValueRef(TW_INTERNAL_MOUNT);
else
return GetValueRef(TW_EXTERNAL_MOUNT);
} else if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetValueRef(TW_INTERNAL_MOUNT);
else
return GetValueRef(TW_EXTERNAL_MOUNT);
}
string DataManager::GetSettingsStoragePath(void)
{
if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetStrValue(TW_INTERNAL_PATH);
else
return GetStrValue(TW_EXTERNAL_PATH);
}
string& DataManager::CGetSettingsStoragePath()
{
if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetValueRef(TW_INTERNAL_PATH);
else
return GetValueRef(TW_EXTERNAL_PATH);
}
string DataManager::GetSettingsStorageMount(void)
{
if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetStrValue(TW_INTERNAL_MOUNT);
else
return GetStrValue(TW_EXTERNAL_MOUNT);
}
string& DataManager::CGetSettingsStorageMount()
{
if (GetIntValue(TW_HAS_INTERNAL) == 1)
return GetValueRef(TW_INTERNAL_MOUNT);
else
return GetValueRef(TW_EXTERNAL_MOUNT);
}
extern "C" int DataManager_ResetDefaults()
{
return DataManager::ResetDefaults();
}
extern "C" void DataManager_LoadDefaults()
{
return DataManager::SetDefaultValues();
}
extern "C" int DataManager_LoadValues(const char* filename)
{
return DataManager::LoadValues(filename);
}
extern "C" int DataManager_Flush()
{
return DataManager::Flush();
}
extern "C" int DataManager_GetValue(const char* varName, char* value)
{
int ret;
string str;
ret = DataManager::GetValue(varName, str);
if (ret == 0)
strcpy(value, str.c_str());
return ret;
}
extern "C" const char* DataManager_GetStrValue(const char* varName)
{
string& str = DataManager::GetValueRef(varName);
return str.c_str();
}
extern "C" const char* DataManager_GetCurrentStoragePath(void)
{
string& str = DataManager::CGetCurrentStoragePath();
return str.c_str();
}
extern "C" const char* DataManager_GetSettingsStoragePath(void)
{
string& str = DataManager::CGetSettingsStoragePath();
return str.c_str();
}
extern "C" const char* DataManager_GetCurrentStorageMount(void)
{
string& str = DataManager::CGetCurrentStorageMount();
return str.c_str();
}
extern "C" const char* DataManager_GetSettingsStorageMount(void)
{
string& str = DataManager::CGetSettingsStorageMount();
return str.c_str();
}
extern "C" int DataManager_GetIntValue(const char* varName)
{
return DataManager::GetIntValue(varName);
}
extern "C" int DataManager_SetStrValue(const char* varName, char* value)
{
return DataManager::SetValue(varName, value, 0);
}
extern "C" int DataManager_SetIntValue(const char* varName, int value)
{
return DataManager::SetValue(varName, value, 0);
}
extern "C" int DataManager_SetFloatValue(const char* varName, float value)
{
return DataManager::SetValue(varName, value, 0);
}
extern "C" int DataManager_ToggleIntValue(const char* varName)
{
if (DataManager::GetIntValue(varName))
return DataManager::SetValue(varName, 0);
else
return DataManager::SetValue(varName, 1);
}
extern "C" void DataManager_DumpValues()
{
return DataManager::DumpValues();
}
extern "C" void DataManager_ReadSettingsFile()
{
return DataManager::ReadSettingsFile();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ubuntu-flyme/TWRP_CN.git
git@gitee.com:ubuntu-flyme/TWRP_CN.git
ubuntu-flyme
TWRP_CN
TWRP_CN
master

搜索帮助