blob: 275c70c6e5cfbc793fe1bfd940a66194643fd829 [file] [log] [blame]
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001/*
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 Yonker1b7a31b2014-07-03 15:09:22 -050019#include <string>
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050020#include <map>
21#include <fstream>
22#include <sstream>
23
24#include "infomanager.hpp"
25#include "twcommon.h"
26#include "partitions.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060027#include "set_metadata.h"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050028
29using namespace std;
30
Ethan Yonkerfe916112016-03-14 14:54:37 -050031InfoManager::InfoManager() {
32 file_version = 0;
33 is_const = false;
34}
35
36InfoManager::InfoManager(const string& filename) {
37 file_version = 0;
38 is_const = false;
39 SetFile(filename);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050040}
41
42InfoManager::~InfoManager(void) {
Ethan Yonkerfe916112016-03-14 14:54:37 -050043 Clear();
44}
45
46void InfoManager::SetFile(const string& filename) {
47 File = filename;
48}
49
50void InfoManager::SetFileVersion(int version) {
51 file_version = version;
52}
53
54void InfoManager::SetConst(void) {
55 is_const = true;
56}
57
58void InfoManager::Clear(void) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050059 mValues.clear();
60}
61
62int 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 Yonkerfe916112016-03-14 14:54:37 -050074 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 Yonker1b7a31b2014-07-03 15:09:22 -050084 while (!feof(in)) {
85 string Name;
86 string Value;
87 unsigned short length;
Ethan Yonker7dad6252015-10-22 14:38:01 -050088 char array[513];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050089
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 Yonker7dad6252015-10-22 14:38:01 -050093 array[length+1] = '\0';
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050094 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 Yonker7dad6252015-10-22 14:38:01 -050099 array[length+1] = '\0';
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500100 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 }
111error:
112 fclose(in);
113 return 0;
114}
115
116int InfoManager::SaveValues(void) {
117 if (File.empty())
118 return -1;
119
120 PartitionManager.Mount_By_Path(File, true);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600121 LOGINFO("InfoManager saving '%s'\n", File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500122 FILE* out = fopen(File.c_str(), "wb");
123 if (!out)
124 return -1;
125
Ethan Yonkerfe916112016-03-14 14:54:37 -0500126 if (file_version) {
127 fwrite(&file_version, 1, sizeof(int), out);
128 }
129
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500130 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 Yonker4b94cfd2014-12-11 10:00:45 -0600140 tw_set_default_metadata(File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500141 return 0;
142}
143
Ethan Yonkerfe916112016-03-14 14:54:37 -0500144int InfoManager::GetValue(const string& varName, string& value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500145 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 Yonkerfe916112016-03-14 14:54:37 -0500156int InfoManager::GetValue(const string& varName, int& value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500157 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 Yonkerfe916112016-03-14 14:54:37 -0500166int InfoManager::GetValue(const string& varName, float& value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500167 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 Yonkerfe916112016-03-14 14:54:37 -0500176unsigned long long InfoManager::GetValue(const string& varName, unsigned long long& value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500177 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 Yonkerfe916112016-03-14 14:54:37 -0500187string InfoManager::GetStrValue(const string& varName) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500188 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 Yonkerfe916112016-03-14 14:54:37 -0500195int InfoManager::GetIntValue(const string& varName) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500196 string retVal;
197 GetValue(varName, retVal);
198 return atoi(retVal.c_str());
199}
200
Ethan Yonkerfe916112016-03-14 14:54:37 -0500201int InfoManager::SetValue(const string& varName, const string& value) {
202 // Don't allow empty names or numerical starting values
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500203 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 Yonkerfe916112016-03-14 14:54:37 -0500210 else if (!is_const)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500211 pos->second = value;
212
213 return 0;
214}
215
Ethan Yonkerfe916112016-03-14 14:54:37 -0500216int InfoManager::SetValue(const string& varName, const int value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500217 ostringstream valStr;
218 valStr << value;
219 return SetValue(varName, valStr.str());
220}
221
Ethan Yonkerfe916112016-03-14 14:54:37 -0500222int InfoManager::SetValue(const string& varName, const float value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500223 ostringstream valStr;
224 valStr << value;
225 return SetValue(varName, valStr.str());
226}
227
Ethan Yonkerfe916112016-03-14 14:54:37 -0500228int InfoManager::SetValue(const string& varName, const unsigned long long& value) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500229 ostringstream valStr;
230 valStr << value;
231 return SetValue(varName, valStr.str());
232}