Skip to content

Commit

Permalink
DataManager Updates
Browse files Browse the repository at this point in the history
The goal of this change is to make DataManager use InfoManager to reduce
code duplication.

Change-Id: Ia4f4c4324453a192995e0f442db0a03628c13e46
  • Loading branch information
Dees-Troy committed Mar 31, 2016
1 parent d4f3082 commit fe91611
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 366 deletions.
462 changes: 178 additions & 284 deletions data.cpp

Large diffs are not rendered by default.

39 changes: 19 additions & 20 deletions data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,39 @@
#define _DATAMANAGER_HPP_HEADER

#include <string>
#include <utility>
#include <map>
#include <pthread.h>
#include "infomanager.hpp"

using namespace std;

class DataManager
{
public:
static int ResetDefaults();
static int LoadValues(const string filename);
static int LoadValues(const string& filename);
static int Flush();

// Core get routines
static int GetValue(const string varName, string& value);
static int GetValue(const string varName, int& value);
static int GetValue(const string varName, float& value);
static unsigned long long GetValue(const string varName, unsigned long long& value);
static int GetValue(const string& varName, string& value);
static int GetValue(const string& varName, int& value);
static int GetValue(const string& varName, float& value);
static unsigned long long GetValue(const string& varName, unsigned long long& value);

// Helper functions
static string GetStrValue(const string varName);
static int GetIntValue(const string varName);
static string GetStrValue(const string& varName);
static int GetIntValue(const string& varName);

// Core set routines
static int SetValue(const string varName, string value, int persist = 0);
static int SetValue(const string varName, int value, int persist = 0);
static int SetValue(const string varName, float value, int persist = 0);
static int SetValue(const string varName, unsigned long long value, int persist = 0);
static int SetProgress(float Fraction);
static int ShowProgress(float Portion, float Seconds);
static int SetValue(const string& varName, const string& value, const int persist = 0);
static int SetValue(const string& varName, const int value, const int persist = 0);
static int SetValue(const string& varName, const float value, const int persist = 0);
static int SetValue(const string& varName, const unsigned long long& value, const int persist = 0);
static int SetProgress(const float Fraction);
static int ShowProgress(const float Portion, const float Seconds);

static void DumpValues();
static void update_tz_environment_variables();
static void Vibrate(const string varName);
static void Vibrate(const string& varName);
static void SetBackupFolder();
static void SetDefaultValues();
static void Output_Version(void); // Outputs the version to a file in the TWRP folder
Expand All @@ -63,18 +62,18 @@ class DataManager
static string GetSettingsStoragePath(void);

protected:
typedef pair<string, int> TStrIntPair;
typedef pair<string, TStrIntPair> TNameValuePair;
static map<string, TStrIntPair> mValues;
static string mBackingFile;
static int mInitialized;
static InfoManager mPersist;
static InfoManager mData;
static InfoManager mConst;

static map<string, string> mConstValues;

protected:
static int SaveValues();

static int GetMagicValue(string varName, string& value);
static int GetMagicValue(const string& varName, string& value);

private:
static void sanitize_device_id(char* device_id);
Expand Down
80 changes: 51 additions & 29 deletions infomanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,7 @@
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

#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/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#include <string>
#include <utility>
#include <map>
#include <fstream>
#include <sstream>
Expand All @@ -43,11 +28,34 @@

using namespace std;

InfoManager::InfoManager(const string filename) {
File = filename;
InfoManager::InfoManager() {
file_version = 0;
is_const = false;
}

InfoManager::InfoManager(const string& filename) {
file_version = 0;
is_const = false;
SetFile(filename);
}

InfoManager::~InfoManager(void) {
Clear();
}

void InfoManager::SetFile(const string& filename) {
File = filename;
}

void InfoManager::SetFileVersion(int version) {
file_version = version;
}

void InfoManager::SetConst(void) {
is_const = true;
}

void InfoManager::Clear(void) {
mValues.clear();
}

Expand All @@ -63,6 +71,16 @@ int InfoManager::LoadValues(void) {
LOGINFO("InfoManager loading from '%s'.\n", File.c_str());
}

if (file_version) {
int read_file_version;
if (fread(&read_file_version, 1, sizeof(int), in) != sizeof(int))
goto error;
if (read_file_version != file_version) {
LOGINFO("InfoManager file version has changed, not reading file\n");
goto error;
}
}

while (!feof(in)) {
string Name;
string Value;
Expand Down Expand Up @@ -105,6 +123,10 @@ int InfoManager::SaveValues(void) {
if (!out)
return -1;

if (file_version) {
fwrite(&file_version, 1, sizeof(int), out);
}

map<string, string>::iterator iter;
for (iter = mValues.begin(); iter != mValues.end(); ++iter) {
unsigned short length = (unsigned short) iter->first.length() + 1;
Expand All @@ -119,7 +141,7 @@ int InfoManager::SaveValues(void) {
return 0;
}

int InfoManager::GetValue(const string varName, string& value) {
int InfoManager::GetValue(const string& varName, string& value) {
string localStr = varName;

map<string, string>::iterator pos;
Expand All @@ -131,7 +153,7 @@ int InfoManager::GetValue(const string varName, string& value) {
return 0;
}

int InfoManager::GetValue(const string varName, int& value) {
int InfoManager::GetValue(const string& varName, int& value) {
string data;

if (GetValue(varName,data) != 0)
Expand All @@ -141,7 +163,7 @@ int InfoManager::GetValue(const string varName, int& value) {
return 0;
}

int InfoManager::GetValue(const string varName, float& value) {
int InfoManager::GetValue(const string& varName, float& value) {
string data;

if (GetValue(varName,data) != 0)
Expand All @@ -151,7 +173,7 @@ int InfoManager::GetValue(const string varName, float& value) {
return 0;
}

unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) {
unsigned long long InfoManager::GetValue(const string& varName, unsigned long long& value) {
string data;

if (GetValue(varName,data) != 0)
Expand All @@ -162,48 +184,48 @@ unsigned long long InfoManager::GetValue(const string varName, unsigned long lon
}

// This function will return an empty string if the value doesn't exist
string InfoManager::GetStrValue(const string varName) {
string InfoManager::GetStrValue(const string& varName) {
string retVal;

GetValue(varName, retVal);
return retVal;
}

// This function will return 0 if the value doesn't exist
int InfoManager::GetIntValue(const string varName) {
int InfoManager::GetIntValue(const string& varName) {
string retVal;
GetValue(varName, retVal);
return atoi(retVal.c_str());
}

int InfoManager::SetValue(const string varName, string value) {
// Don't allow empty values or numerical starting values
int InfoManager::SetValue(const string& varName, const string& value) {
// Don't allow empty names or numerical starting values
if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
return -1;

map<string, string>::iterator pos;
pos = mValues.find(varName);
if (pos == mValues.end())
mValues.insert(make_pair(varName, value));
else
else if (!is_const)
pos->second = value;

return 0;
}

int InfoManager::SetValue(const string varName, int value) {
int InfoManager::SetValue(const string& varName, const int value) {
ostringstream valStr;
valStr << value;
return SetValue(varName, valStr.str());
}

int InfoManager::SetValue(const string varName, float value) {
int InfoManager::SetValue(const string& varName, const float value) {
ostringstream valStr;
valStr << value;
return SetValue(varName, valStr.str());
}

int InfoManager::SetValue(const string varName, unsigned long long value) {
int InfoManager::SetValue(const string& varName, const unsigned long long& value) {
ostringstream valStr;
valStr << value;
return SetValue(varName, valStr.str());
Expand Down
29 changes: 18 additions & 11 deletions infomanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,36 @@ using namespace std;
class InfoManager
{
public:
InfoManager(const string filename);
InfoManager();
explicit InfoManager(const string& filename);
virtual ~InfoManager();
void SetFile(const string& filename);
void SetFileVersion(int version);
void SetConst();
void Clear();
int LoadValues();
int SaveValues();

// Core get routines
int GetValue(const string varName, string& value);
int GetValue(const string varName, int& value);
int GetValue(const string varName, float& value);
unsigned long long GetValue(const string varName, unsigned long long& value);
int GetValue(const string& varName, string& value);
int GetValue(const string& varName, int& value);
int GetValue(const string& varName, float& value);
unsigned long long GetValue(const string& varName, unsigned long long& value);

string GetStrValue(const string varName);
int GetIntValue(const string varName);
string GetStrValue(const string& varName);
int GetIntValue(const string& varName);

// Core set routines
int SetValue(const string varName, string value);
int SetValue(const string varName, int value);
int SetValue(const string varName, float value);
int SetValue(const string varName, unsigned long long value);
int SetValue(const string& varName, const string& value);
int SetValue(const string& varName, const int value);
int SetValue(const string& varName, const float value);
int SetValue(const string& varName, const unsigned long long& value);

private:
string File;
map<string, string> mValues;
int file_version;
bool is_const;

};

Expand Down
6 changes: 0 additions & 6 deletions openrecoveryscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,6 @@ int OpenRecoveryScript::run_script_file(void) {
} else if ((value2[i] == 'R' || value2[i] == 'r') && Partition_List.find("/recovery;") != string::npos) {
Restore_List += "/recovery;";
gui_msg("recovery=Recovery");
} else if (value2[i] == '1' && DataManager::GetIntValue(TW_RESTORE_SP1_VAR) > 0) {
gui_print("%s\n", "Special1 -- No Longer Supported...");
} else if (value2[i] == '2' && DataManager::GetIntValue(TW_RESTORE_SP2_VAR) > 0) {
gui_print("%s\n", "Special2 -- No Longer Supported...");
} else if (value2[i] == '3' && DataManager::GetIntValue(TW_RESTORE_SP3_VAR) > 0) {
gui_print("%s\n", "Special3 -- No Longer Supported...");
} else if ((value2[i] == 'B' || value2[i] == 'b') && Partition_List.find("/boot;") != string::npos) {
Restore_List += "/boot;";
gui_msg("boot=Boot");
Expand Down
1 change: 0 additions & 1 deletion partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
char crypto_blkdev[255];
property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
if (strcmp(crypto_blkdev, "error") != 0) {
DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
DataManager::SetValue(TW_IS_DECRYPTED, 1);
Is_Encrypted = true;
Is_Decrypted = true;
Expand Down
1 change: 0 additions & 1 deletion partitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,6 @@ int TWPartitionManager::Decrypt_Device(string Password) {
} else {
TWPartition* dat = Find_Partition_By_Path("/data");
if (dat != NULL) {
DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
DataManager::SetValue(TW_IS_DECRYPTED, 1);
dat->Is_Decrypted = true;
dat->Decrypted_Block_Device = crypto_blkdev;
Expand Down
3 changes: 2 additions & 1 deletion twrp-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,10 @@ void TWFunc::Update_Intent_File(string Intent) {
// reboot: Reboot the system. Return -1 on error, no return on success
int TWFunc::tw_reboot(RebootCommand command)
{
DataManager::Flush();
Update_Log_File();
// Always force a sync before we reboot
sync();
Update_Log_File();

switch (command) {
case rb_current:
Expand Down
13 changes: 0 additions & 13 deletions variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
#define TW_BACKUP_CACHE_VAR "tw_backup_cache"
#define TW_BACKUP_ANDSEC_VAR "tw_backup_andsec"
#define TW_BACKUP_SDEXT_VAR "tw_backup_sdext"
#define TW_BACKUP_SP1_VAR "tw_backup_sp1"
#define TW_BACKUP_SP2_VAR "tw_backup_sp2"
#define TW_BACKUP_SP3_VAR "tw_backup_sp3"
#define TW_BACKUP_AVG_IMG_RATE "tw_backup_avg_img_rate"
#define TW_BACKUP_AVG_FILE_RATE "tw_backup_avg_file_rate"
#define TW_BACKUP_AVG_FILE_COMP_RATE "tw_backup_avg_file_comp_rate"
Expand All @@ -47,9 +44,6 @@
#define TW_BACKUP_CACHE_SIZE "tw_backup_cache_size"
#define TW_BACKUP_ANDSEC_SIZE "tw_backup_andsec_size"
#define TW_BACKUP_SDEXT_SIZE "tw_backup_sdext_size"
#define TW_BACKUP_SP1_SIZE "tw_backup_sp1_size"
#define TW_BACKUP_SP2_SIZE "tw_backup_sp2_size"
#define TW_BACKUP_SP3_SIZE "tw_backup_sp3_size"
#define TW_STORAGE_FREE_SIZE "tw_storage_free_size"
#define TW_GENERATE_MD5_TEXT "tw_generate_md5_text"

Expand All @@ -61,20 +55,14 @@
#define TW_RESTORE_CACHE_VAR "tw_restore_cache"
#define TW_RESTORE_ANDSEC_VAR "tw_restore_andsec"
#define TW_RESTORE_SDEXT_VAR "tw_restore_sdext"
#define TW_RESTORE_SP1_VAR "tw_restore_sp1"
#define TW_RESTORE_SP2_VAR "tw_restore_sp2"
#define TW_RESTORE_SP3_VAR "tw_restore_sp3"
#define TW_RESTORE_AVG_IMG_RATE "tw_restore_avg_img_rate"
#define TW_RESTORE_AVG_FILE_RATE "tw_restore_avg_file_rate"
#define TW_RESTORE_AVG_FILE_COMP_RATE "tw_restore_avg_file_comp_rate"
#define TW_RESTORE_FILE_DATE "tw_restore_file_date"
#define TW_VERIFY_MD5_TEXT "tw_verify_md5_text"
#define TW_UPDATE_SYSTEM_DETAILS_TEXT "tw_update_system_details_text"

#define TW_SHOW_SPAM_VAR "tw_show_spam"
#define TW_COLOR_THEME_VAR "tw_color_theme"
#define TW_VERSION_VAR "tw_version"
#define TW_SORT_FILES_BY_DATE_VAR "tw_sort_files_by_date"
#define TW_GUI_SORT_ORDER "tw_gui_sort_order"
#define TW_ZIP_LOCATION_VAR "tw_zip_location"
#define TW_ZIP_INTERNAL_VAR "tw_zip_internal"
Expand Down Expand Up @@ -148,7 +136,6 @@
#define TW_CRYPTO_PWTYPE "tw_crypto_pwtype"
#define TW_HAS_CRYPTO "tw_has_crypto"
#define TW_CRYPTO_PASSWORD "tw_crypto_password"
#define TW_DATA_BLK_DEVICE "tw_data_blk_device" // Original block device - not decrypted
#define TW_SDEXT_DISABLE_EXT4 "tw_sdext_disable_ext4"
#define TW_MILITARY_TIME "tw_military_time"

Expand Down

0 comments on commit fe91611

Please sign in to comment.