Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 19 | #include <string> |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <fstream> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include "infomanager.hpp" |
| 25 | #include "twcommon.h" |
| 26 | #include "partitions.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 27 | #include "set_metadata.h" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 28 | |
| 29 | using namespace std; |
| 30 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 31 | InfoManager::InfoManager() { |
| 32 | file_version = 0; |
| 33 | is_const = false; |
| 34 | } |
| 35 | |
| 36 | InfoManager::InfoManager(const string& filename) { |
| 37 | file_version = 0; |
| 38 | is_const = false; |
| 39 | SetFile(filename); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | InfoManager::~InfoManager(void) { |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 43 | Clear(); |
| 44 | } |
| 45 | |
| 46 | void InfoManager::SetFile(const string& filename) { |
| 47 | File = filename; |
| 48 | } |
| 49 | |
| 50 | void InfoManager::SetFileVersion(int version) { |
| 51 | file_version = version; |
| 52 | } |
| 53 | |
| 54 | void InfoManager::SetConst(void) { |
| 55 | is_const = true; |
| 56 | } |
| 57 | |
| 58 | void InfoManager::Clear(void) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 59 | mValues.clear(); |
| 60 | } |
| 61 | |
| 62 | int InfoManager::LoadValues(void) { |
| 63 | string str; |
| 64 | |
| 65 | // Read in the file, if possible |
| 66 | FILE* in = fopen(File.c_str(), "rb"); |
| 67 | if (!in) { |
| 68 | LOGINFO("InfoManager file '%s' not found.\n", File.c_str()); |
| 69 | return -1; |
| 70 | } else { |
| 71 | LOGINFO("InfoManager loading from '%s'.\n", File.c_str()); |
| 72 | } |
| 73 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 74 | if (file_version) { |
| 75 | int read_file_version; |
| 76 | if (fread(&read_file_version, 1, sizeof(int), in) != sizeof(int)) |
| 77 | goto error; |
| 78 | if (read_file_version != file_version) { |
| 79 | LOGINFO("InfoManager file version has changed, not reading file\n"); |
| 80 | goto error; |
| 81 | } |
| 82 | } |
| 83 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 84 | while (!feof(in)) { |
| 85 | string Name; |
| 86 | string Value; |
| 87 | unsigned short length; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 88 | char array[513]; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 89 | |
| 90 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 91 | if (length >= 512) goto error; |
| 92 | if (fread(array, 1, length, in) != length) goto error; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 93 | array[length+1] = '\0'; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 94 | Name = array; |
| 95 | |
| 96 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 97 | if (length >= 512) goto error; |
| 98 | if (fread(array, 1, length, in) != length) goto error; |
Ethan Yonker | 7dad625 | 2015-10-22 14:38:01 -0500 | [diff] [blame] | 99 | array[length+1] = '\0'; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 100 | Value = array; |
| 101 | |
| 102 | map<string, string>::iterator pos; |
| 103 | |
| 104 | pos = mValues.find(Name); |
| 105 | if (pos != mValues.end()) { |
| 106 | pos->second = Value; |
| 107 | } else { |
| 108 | mValues.insert(make_pair(Name, Value)); |
| 109 | } |
| 110 | } |
| 111 | error: |
| 112 | fclose(in); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int InfoManager::SaveValues(void) { |
| 117 | if (File.empty()) |
| 118 | return -1; |
| 119 | |
| 120 | PartitionManager.Mount_By_Path(File, true); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 121 | LOGINFO("InfoManager saving '%s'\n", File.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 122 | FILE* out = fopen(File.c_str(), "wb"); |
| 123 | if (!out) |
| 124 | return -1; |
| 125 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 126 | if (file_version) { |
| 127 | fwrite(&file_version, 1, sizeof(int), out); |
| 128 | } |
| 129 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 130 | map<string, string>::iterator iter; |
| 131 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) { |
| 132 | unsigned short length = (unsigned short) iter->first.length() + 1; |
| 133 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 134 | fwrite(iter->first.c_str(), 1, length, out); |
| 135 | length = (unsigned short) iter->second.length() + 1; |
| 136 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 137 | fwrite(iter->second.c_str(), 1, length, out); |
| 138 | } |
| 139 | fclose(out); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 140 | tw_set_default_metadata(File.c_str()); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 144 | int InfoManager::GetValue(const string& varName, string& value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 145 | string localStr = varName; |
| 146 | |
| 147 | map<string, string>::iterator pos; |
| 148 | pos = mValues.find(localStr); |
| 149 | if (pos == mValues.end()) |
| 150 | return -1; |
| 151 | |
| 152 | value = pos->second; |
| 153 | return 0; |
| 154 | } |
| 155 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 156 | int InfoManager::GetValue(const string& varName, int& value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 157 | string data; |
| 158 | |
| 159 | if (GetValue(varName,data) != 0) |
| 160 | return -1; |
| 161 | |
| 162 | value = atoi(data.c_str()); |
| 163 | return 0; |
| 164 | } |
| 165 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 166 | int InfoManager::GetValue(const string& varName, float& value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 167 | string data; |
| 168 | |
| 169 | if (GetValue(varName,data) != 0) |
| 170 | return -1; |
| 171 | |
| 172 | value = atof(data.c_str()); |
| 173 | return 0; |
| 174 | } |
| 175 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 176 | unsigned long long InfoManager::GetValue(const string& varName, unsigned long long& value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 177 | string data; |
| 178 | |
| 179 | if (GetValue(varName,data) != 0) |
| 180 | return -1; |
| 181 | |
| 182 | value = strtoull(data.c_str(), NULL, 10); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | // This function will return an empty string if the value doesn't exist |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 187 | string InfoManager::GetStrValue(const string& varName) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 188 | string retVal; |
| 189 | |
| 190 | GetValue(varName, retVal); |
| 191 | return retVal; |
| 192 | } |
| 193 | |
| 194 | // This function will return 0 if the value doesn't exist |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 195 | int InfoManager::GetIntValue(const string& varName) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 196 | string retVal; |
| 197 | GetValue(varName, retVal); |
| 198 | return atoi(retVal.c_str()); |
| 199 | } |
| 200 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 201 | int InfoManager::SetValue(const string& varName, const string& value) { |
| 202 | // Don't allow empty names or numerical starting values |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 203 | if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9')) |
| 204 | return -1; |
| 205 | |
| 206 | map<string, string>::iterator pos; |
| 207 | pos = mValues.find(varName); |
| 208 | if (pos == mValues.end()) |
| 209 | mValues.insert(make_pair(varName, value)); |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 210 | else if (!is_const) |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 211 | pos->second = value; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 216 | int InfoManager::SetValue(const string& varName, const int value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 217 | ostringstream valStr; |
| 218 | valStr << value; |
| 219 | return SetValue(varName, valStr.str()); |
| 220 | } |
| 221 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 222 | int InfoManager::SetValue(const string& varName, const float value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 223 | ostringstream valStr; |
| 224 | valStr << value; |
| 225 | return SetValue(varName, valStr.str()); |
| 226 | } |
| 227 | |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 228 | int InfoManager::SetValue(const string& varName, const unsigned long long& value) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 229 | ostringstream valStr; |
| 230 | valStr << value; |
| 231 | return SetValue(varName, valStr.str()); |
| 232 | } |