blob: 864c431a576f7186191e26499f693ba97966d428 [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;
70 char array[512];
71
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;
75 Name = array;
76
77 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
78 if (length >= 512) goto error;
79 if (fread(array, 1, length, in) != length) goto error;
80 Value = array;
81
82 map<string, string>::iterator pos;
83
84 pos = mValues.find(Name);
85 if (pos != mValues.end()) {
86 pos->second = Value;
87 } else {
88 mValues.insert(make_pair(Name, Value));
89 }
90 }
91error:
92 fclose(in);
93 return 0;
94}
95
96int InfoManager::SaveValues(void) {
97 if (File.empty())
98 return -1;
99
100 PartitionManager.Mount_By_Path(File, true);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600101 LOGINFO("InfoManager saving '%s'\n", File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500102 FILE* out = fopen(File.c_str(), "wb");
103 if (!out)
104 return -1;
105
106 map<string, string>::iterator iter;
107 for (iter = mValues.begin(); iter != mValues.end(); ++iter) {
108 unsigned short length = (unsigned short) iter->first.length() + 1;
109 fwrite(&length, 1, sizeof(unsigned short), out);
110 fwrite(iter->first.c_str(), 1, length, out);
111 length = (unsigned short) iter->second.length() + 1;
112 fwrite(&length, 1, sizeof(unsigned short), out);
113 fwrite(iter->second.c_str(), 1, length, out);
114 }
115 fclose(out);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600116 tw_set_default_metadata(File.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500117 return 0;
118}
119
120int InfoManager::GetValue(const string varName, string& value) {
121 string localStr = varName;
122
123 map<string, string>::iterator pos;
124 pos = mValues.find(localStr);
125 if (pos == mValues.end())
126 return -1;
127
128 value = pos->second;
129 return 0;
130}
131
132int InfoManager::GetValue(const string varName, int& value) {
133 string data;
134
135 if (GetValue(varName,data) != 0)
136 return -1;
137
138 value = atoi(data.c_str());
139 return 0;
140}
141
142int InfoManager::GetValue(const string varName, float& value) {
143 string data;
144
145 if (GetValue(varName,data) != 0)
146 return -1;
147
148 value = atof(data.c_str());
149 return 0;
150}
151
152unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) {
153 string data;
154
155 if (GetValue(varName,data) != 0)
156 return -1;
157
158 value = strtoull(data.c_str(), NULL, 10);
159 return 0;
160}
161
162// This function will return an empty string if the value doesn't exist
163string InfoManager::GetStrValue(const string varName) {
164 string retVal;
165
166 GetValue(varName, retVal);
167 return retVal;
168}
169
170// This function will return 0 if the value doesn't exist
171int InfoManager::GetIntValue(const string varName) {
172 string retVal;
173 GetValue(varName, retVal);
174 return atoi(retVal.c_str());
175}
176
177int InfoManager::SetValue(const string varName, string value) {
178 // Don't allow empty values or numerical starting values
179 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
180 return -1;
181
182 map<string, string>::iterator pos;
183 pos = mValues.find(varName);
184 if (pos == mValues.end())
185 mValues.insert(make_pair(varName, value));
186 else
187 pos->second = value;
188
189 return 0;
190}
191
192int InfoManager::SetValue(const string varName, int value) {
193 ostringstream valStr;
194 valStr << value;
195 return SetValue(varName, valStr.str());
196}
197
198int InfoManager::SetValue(const string varName, float value) {
199 ostringstream valStr;
200 valStr << value;
201 return SetValue(varName, valStr.str());
202}
203
204int InfoManager::SetValue(const string varName, unsigned long long value) {
205 ostringstream valStr;
206 valStr << value;
207 return SetValue(varName, valStr.str());
208}