blob: 2a4c3c89215488e01a1f03a975e59539e49ef6b0 [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
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <unistd.h>
31#include <stdlib.h>
32
33#include <string>
34#include <utility>
35#include <map>
36#include <fstream>
37#include <sstream>
38
39#include "infomanager.hpp"
40#include "twcommon.h"
41#include "partitions.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060042#include "set_metadata.h"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043
44using namespace std;
45
46InfoManager::InfoManager(const string filename) {
47 File = filename;
48}
49
50InfoManager::~InfoManager(void) {
51 mValues.clear();
52}
53
54int InfoManager::LoadValues(void) {
55 string str;
56
57 // Read in the file, if possible
58 FILE* in = fopen(File.c_str(), "rb");
59 if (!in) {
60 LOGINFO("InfoManager file '%s' not found.\n", File.c_str());
61 return -1;
62 } else {
63 LOGINFO("InfoManager loading from '%s'.\n", File.c_str());
64 }
65
66 while (!feof(in)) {
67 string Name;
68 string Value;
69 unsigned short length;
Ethan Yonker7dad6252015-10-22 14:38:01 -050070 char array[513];
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050071
72 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
73 if (length >= 512) goto error;
74 if (fread(array, 1, length, in) != length) goto error;
Ethan Yonker7dad6252015-10-22 14:38:01 -050075 array[length+1] = '\0';
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050076 Name = array;
77
78 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
79 if (length >= 512) goto error;
80 if (fread(array, 1, length, in) != length) goto error;
Ethan Yonker7dad6252015-10-22 14:38:01 -050081 array[length+1] = '\0';
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050082 Value = array;
83
84 map<string, string>::iterator pos;
85
86 pos = mValues.find(Name);
87 if (pos != mValues.end()) {
88 pos->second = Value;
89 } else {
90 mValues.insert(make_pair(Name, Value));
91 }
92 }
93error:
94 fclose(in);
95 return 0;
96}
97
98int InfoManager::SaveValues(void) {
99 if (File.empty())
100 return -1;
101
102 PartitionManager.Mount_By_Path(File, true);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600103 LOGINFO("InfoManager saving '%s'\n", File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500104 FILE* out = fopen(File.c_str(), "wb");
105 if (!out)
106 return -1;
107
108 map<string, string>::iterator iter;
109 for (iter = mValues.begin(); iter != mValues.end(); ++iter) {
110 unsigned short length = (unsigned short) iter->first.length() + 1;
111 fwrite(&length, 1, sizeof(unsigned short), out);
112 fwrite(iter->first.c_str(), 1, length, out);
113 length = (unsigned short) iter->second.length() + 1;
114 fwrite(&length, 1, sizeof(unsigned short), out);
115 fwrite(iter->second.c_str(), 1, length, out);
116 }
117 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600118 tw_set_default_metadata(File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500119 return 0;
120}
121
122int InfoManager::GetValue(const string varName, string& value) {
123 string localStr = varName;
124
125 map<string, string>::iterator pos;
126 pos = mValues.find(localStr);
127 if (pos == mValues.end())
128 return -1;
129
130 value = pos->second;
131 return 0;
132}
133
134int InfoManager::GetValue(const string varName, int& value) {
135 string data;
136
137 if (GetValue(varName,data) != 0)
138 return -1;
139
140 value = atoi(data.c_str());
141 return 0;
142}
143
144int InfoManager::GetValue(const string varName, float& value) {
145 string data;
146
147 if (GetValue(varName,data) != 0)
148 return -1;
149
150 value = atof(data.c_str());
151 return 0;
152}
153
154unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) {
155 string data;
156
157 if (GetValue(varName,data) != 0)
158 return -1;
159
160 value = strtoull(data.c_str(), NULL, 10);
161 return 0;
162}
163
164// This function will return an empty string if the value doesn't exist
165string InfoManager::GetStrValue(const string varName) {
166 string retVal;
167
168 GetValue(varName, retVal);
169 return retVal;
170}
171
172// This function will return 0 if the value doesn't exist
173int InfoManager::GetIntValue(const string varName) {
174 string retVal;
175 GetValue(varName, retVal);
176 return atoi(retVal.c_str());
177}
178
179int InfoManager::SetValue(const string varName, string value) {
180 // Don't allow empty values or numerical starting values
181 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
182 return -1;
183
184 map<string, string>::iterator pos;
185 pos = mValues.find(varName);
186 if (pos == mValues.end())
187 mValues.insert(make_pair(varName, value));
188 else
189 pos->second = value;
190
191 return 0;
192}
193
194int InfoManager::SetValue(const string varName, int value) {
195 ostringstream valStr;
196 valStr << value;
197 return SetValue(varName, valStr.str());
198}
199
200int InfoManager::SetValue(const string varName, float value) {
201 ostringstream valStr;
202 valStr << value;
203 return SetValue(varName, valStr.str());
204}
205
206int InfoManager::SetValue(const string varName, unsigned long long value) {
207 ostringstream valStr;
208 valStr << value;
209 return SetValue(varName, valStr.str());
210}