blob: a681de280a673c60cf7d5435128c49f9ca733333 [file] [log] [blame]
Dees_Troy38bd7602012-09-14 13:33:53 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/stat.h>
5#include <sys/vfs.h>
6#include <unistd.h>
7#include <vector>
8#include <dirent.h>
9#include <time.h>
Dees_Troy43d8b002012-09-17 16:00:01 -040010#include <errno.h>
Dees_Troy38bd7602012-09-14 13:33:53 -040011
12#include "twrp-functions.hpp"
13#include "partitions.hpp"
14#include "common.h"
Dees_Troyb46a6842012-09-25 11:06:46 -040015#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040016
17/* Checks md5 for a path
18 Return values:
19 -1 : MD5 does not exist
20 0 : Failed
21 1 : Success */
22int TWFunc::Check_MD5(string File) {
23 int ret;
24 string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK;
25 char line[255];
26 size_t pos;
27
28 MD5_File = File + ".md5";
Dees_Troy2a923582012-09-20 12:13:34 -040029 if (Path_Exists(MD5_File)) {
Dees_Troy38bd7602012-09-14 13:33:53 -040030 DirPath = Get_Path(File);
Dees_Troy38bd7602012-09-14 13:33:53 -040031 MD5_File = Get_Filename(MD5_File);
Dees_Troy2a923582012-09-20 12:13:34 -040032 Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output";
Dees_Troy8170a922012-09-18 15:40:25 -040033 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -040034 FILE * cs = fopen("/tmp/md5output", "r");
35 if (cs == NULL) {
36 LOGE("Unable to open md5 output file.\n");
37 return 0;
38 }
39
40 fgets(line, sizeof(line), cs);
Dees_Troy2a923582012-09-20 12:13:34 -040041 fclose(cs);
Dees_Troy38bd7602012-09-14 13:33:53 -040042
43 Sline = line;
44 pos = Sline.find(":");
45 if (pos != string::npos) {
46 Filename = Get_Filename(File);
47 MD5_File_Filename = Sline.substr(0, pos);
48 OK = Sline.substr(pos + 2, Sline.size() - pos - 2);
49 if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) {
50 //MD5 is good, return 1
51 ret = 1;
52 } else {
53 // MD5 is bad, return 0
54 ret = 0;
55 }
56 } else {
57 // MD5 is bad, return 0
58 ret = 0;
59 }
Dees_Troy38bd7602012-09-14 13:33:53 -040060 } else {
61 //No md5 file, return -1
62 ret = -1;
63 }
64
65 return ret;
66}
67
68// Returns "file.name" from a full /path/to/file.name
69string TWFunc::Get_Filename(string Path) {
70 size_t pos = Path.find_last_of("/");
71 if (pos != string::npos) {
72 string Filename;
73 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
74 return Filename;
75 } else
76 return Path;
77}
78
79// Returns "/path/to/" from a full /path/to/file.name
80string TWFunc::Get_Path(string Path) {
81 size_t pos = Path.find_last_of("/");
82 if (pos != string::npos) {
83 string Pathonly;
84 Pathonly = Path.substr(0, pos + 1);
85 return Pathonly;
86 } else
87 return Path;
88}
89
90// Returns "/path" from a full /path/to/file.name
91string TWFunc::Get_Root_Path(string Path) {
92 string Local_Path = Path;
93
94 // Make sure that we have a leading slash
95 if (Local_Path.substr(0, 1) != "/")
96 Local_Path = "/" + Local_Path;
97
98 // Trim the path to get the root path only
99 size_t position = Local_Path.find("/", 2);
100 if (position != string::npos) {
101 Local_Path.resize(position);
102 }
103 return Local_Path;
104}
105
106void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400107 int need_libs = 0;
108
109 if (!PartitionManager.Mount_By_Path("/system", true))
110 return;
111
112 if (!PartitionManager.Mount_By_Path("/data", true))
113 return;
114
115 ui_print("Installing HTC Dumlock to system...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400116 system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock");
117 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400118 ui_print("Installing flash_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400119 system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image");
Dees_Troy38bd7602012-09-14 13:33:53 -0400120 need_libs = 1;
121 } else
122 ui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400123 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 ui_print("Installing dump_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400125 system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image");
Dees_Troy38bd7602012-09-14 13:33:53 -0400126 need_libs = 1;
127 } else
128 ui_print("dump_image is already installed, skipping...\n");
129 if (need_libs) {
130 ui_print("Installing libs needed for flash_image and dump_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400131 system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so");
132 system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so");
133 system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so");
134 system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so");
Dees_Troy38bd7602012-09-14 13:33:53 -0400135 }
136 ui_print("Installing HTC Dumlock app...\n");
137 mkdir("/data/app", 0777);
Dees_Troy8170a922012-09-18 15:40:25 -0400138 system("rm /data/app/com.teamwin.htcdumlock*");
139 system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk");
Dees_Troy38bd7602012-09-14 13:33:53 -0400140 sync();
141 ui_print("HTC Dumlock is installed.\n");
142}
143
144void TWFunc::htc_dumlock_restore_original_boot(void) {
145 if (!PartitionManager.Mount_By_Path("/sdcard", true))
146 return;
147
148 ui_print("Restoring original boot...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400149 system("htcdumlock restore");
Dees_Troy38bd7602012-09-14 13:33:53 -0400150 ui_print("Original boot restored.\n");
151}
152
153void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
154 if (!PartitionManager.Mount_By_Path("/sdcard", true))
155 return;
156
157 ui_print("Reflashing recovery to boot...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400158 system("htcdumlock recovery noreboot");
Dees_Troy38bd7602012-09-14 13:33:53 -0400159 ui_print("Recovery is flashed to boot.\n");
160}
Dees_Troy43d8b002012-09-17 16:00:01 -0400161
162int TWFunc::Recursive_Mkdir(string Path) {
163 string pathCpy = Path;
164 string wholePath;
165 size_t pos = pathCpy.find("/", 2);
166
167 while (pos != string::npos)
168 {
169 wholePath = pathCpy.substr(0, pos);
170 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
171 LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
172 return false;
173 }
174
175 pos = pathCpy.find("/", pos + 1);
176 }
177 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
178 return false;
179 return true;
180}
181
182unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) {
183 DIR* d;
184 struct dirent* de;
185 struct stat st;
186 char path2[1024], filename[1024];
187 unsigned long long dusize = 0;
188
189 // Make a copy of path in case the data in the pointer gets overwritten later
190 strcpy(path2, Path.c_str());
191
192 d = opendir(path2);
193 if (d == NULL)
194 {
195 LOGE("error opening '%s'\n", path2);
196 return 0;
197 }
198
199 while ((de = readdir(d)) != NULL)
200 {
201 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
202 {
203 strcpy(filename, path2);
204 strcat(filename, "/");
205 strcat(filename, de->d_name);
206 dusize += Get_Folder_Size(filename, Display_Error);
207 }
208 else if (de->d_type == DT_REG)
209 {
210 strcpy(filename, path2);
211 strcat(filename, "/");
212 strcat(filename, de->d_name);
213 stat(filename, &st);
214 dusize += (unsigned long long)(st.st_size);
215 }
216 }
217 closedir(d);
218
219 return dusize;
220}
221
222bool TWFunc::Path_Exists(string Path) {
223 // Check to see if the Path exists
224 struct statfs st;
225
226 if (statfs(Path.c_str(), &st) != 0)
227 return false;
228 else
229 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400230}
231
232void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
233 string Display_Text;
234
235 DataManager::GetValue(Read_Value, Display_Text);
236 if (Display_Text.empty())
237 Display_Text = Default_Text;
238
239 DataManager::SetValue("tw_operation", Display_Text);
240 DataManager::SetValue("tw_partition", "");
241}
242
243void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
244 string Display_Text;
245
246 DataManager::GetValue(Read_Value, Display_Text);
247 if (Display_Text.empty())
248 Display_Text = Default_Text;
249
250 DataManager::SetValue("tw_operation", Display_Text);
251 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400252}