blob: 0b2258a9055e6f78785775c5da256d060f9f6674 [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_Troy38bd7602012-09-14 13:33:53 -040015
16/* Checks md5 for a path
17 Return values:
18 -1 : MD5 does not exist
19 0 : Failed
20 1 : Success */
21int TWFunc::Check_MD5(string File) {
22 int ret;
23 string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK;
24 char line[255];
25 size_t pos;
26
27 MD5_File = File + ".md5";
Dees_Troy2a923582012-09-20 12:13:34 -040028 if (Path_Exists(MD5_File)) {
Dees_Troy38bd7602012-09-14 13:33:53 -040029 DirPath = Get_Path(File);
Dees_Troy38bd7602012-09-14 13:33:53 -040030 MD5_File = Get_Filename(MD5_File);
Dees_Troy2a923582012-09-20 12:13:34 -040031 Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output";
Dees_Troy8170a922012-09-18 15:40:25 -040032 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -040033 FILE * cs = fopen("/tmp/md5output", "r");
34 if (cs == NULL) {
35 LOGE("Unable to open md5 output file.\n");
36 return 0;
37 }
38
39 fgets(line, sizeof(line), cs);
Dees_Troy2a923582012-09-20 12:13:34 -040040 fclose(cs);
Dees_Troy38bd7602012-09-14 13:33:53 -040041
42 Sline = line;
43 pos = Sline.find(":");
44 if (pos != string::npos) {
45 Filename = Get_Filename(File);
46 MD5_File_Filename = Sline.substr(0, pos);
47 OK = Sline.substr(pos + 2, Sline.size() - pos - 2);
48 if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) {
49 //MD5 is good, return 1
50 ret = 1;
51 } else {
52 // MD5 is bad, return 0
53 ret = 0;
54 }
55 } else {
56 // MD5 is bad, return 0
57 ret = 0;
58 }
Dees_Troy38bd7602012-09-14 13:33:53 -040059 } else {
60 //No md5 file, return -1
61 ret = -1;
62 }
63
64 return ret;
65}
66
67// Returns "file.name" from a full /path/to/file.name
68string TWFunc::Get_Filename(string Path) {
69 size_t pos = Path.find_last_of("/");
70 if (pos != string::npos) {
71 string Filename;
72 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
73 return Filename;
74 } else
75 return Path;
76}
77
78// Returns "/path/to/" from a full /path/to/file.name
79string TWFunc::Get_Path(string Path) {
80 size_t pos = Path.find_last_of("/");
81 if (pos != string::npos) {
82 string Pathonly;
83 Pathonly = Path.substr(0, pos + 1);
84 return Pathonly;
85 } else
86 return Path;
87}
88
89// Returns "/path" from a full /path/to/file.name
90string TWFunc::Get_Root_Path(string Path) {
91 string Local_Path = Path;
92
93 // Make sure that we have a leading slash
94 if (Local_Path.substr(0, 1) != "/")
95 Local_Path = "/" + Local_Path;
96
97 // Trim the path to get the root path only
98 size_t position = Local_Path.find("/", 2);
99 if (position != string::npos) {
100 Local_Path.resize(position);
101 }
102 return Local_Path;
103}
104
105void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400106 int need_libs = 0;
107
108 if (!PartitionManager.Mount_By_Path("/system", true))
109 return;
110
111 if (!PartitionManager.Mount_By_Path("/data", true))
112 return;
113
114 ui_print("Installing HTC Dumlock to system...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400115 system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock");
116 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400117 ui_print("Installing flash_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400118 system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image");
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 need_libs = 1;
120 } else
121 ui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400122 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 ui_print("Installing dump_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400124 system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image");
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 need_libs = 1;
126 } else
127 ui_print("dump_image is already installed, skipping...\n");
128 if (need_libs) {
129 ui_print("Installing libs needed for flash_image and dump_image...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400130 system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so");
131 system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so");
132 system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so");
133 system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so");
Dees_Troy38bd7602012-09-14 13:33:53 -0400134 }
135 ui_print("Installing HTC Dumlock app...\n");
136 mkdir("/data/app", 0777);
Dees_Troy8170a922012-09-18 15:40:25 -0400137 system("rm /data/app/com.teamwin.htcdumlock*");
138 system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk");
Dees_Troy38bd7602012-09-14 13:33:53 -0400139 sync();
140 ui_print("HTC Dumlock is installed.\n");
141}
142
143void TWFunc::htc_dumlock_restore_original_boot(void) {
144 if (!PartitionManager.Mount_By_Path("/sdcard", true))
145 return;
146
147 ui_print("Restoring original boot...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400148 system("htcdumlock restore");
Dees_Troy38bd7602012-09-14 13:33:53 -0400149 ui_print("Original boot restored.\n");
150}
151
152void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
153 if (!PartitionManager.Mount_By_Path("/sdcard", true))
154 return;
155
156 ui_print("Reflashing recovery to boot...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400157 system("htcdumlock recovery noreboot");
Dees_Troy38bd7602012-09-14 13:33:53 -0400158 ui_print("Recovery is flashed to boot.\n");
159}
Dees_Troy43d8b002012-09-17 16:00:01 -0400160
161int TWFunc::Recursive_Mkdir(string Path) {
162 string pathCpy = Path;
163 string wholePath;
164 size_t pos = pathCpy.find("/", 2);
165
166 while (pos != string::npos)
167 {
168 wholePath = pathCpy.substr(0, pos);
169 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
170 LOGE("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
171 return false;
172 }
173
174 pos = pathCpy.find("/", pos + 1);
175 }
176 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
177 return false;
178 return true;
179}
180
181unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) {
182 DIR* d;
183 struct dirent* de;
184 struct stat st;
185 char path2[1024], filename[1024];
186 unsigned long long dusize = 0;
187
188 // Make a copy of path in case the data in the pointer gets overwritten later
189 strcpy(path2, Path.c_str());
190
191 d = opendir(path2);
192 if (d == NULL)
193 {
194 LOGE("error opening '%s'\n", path2);
195 return 0;
196 }
197
198 while ((de = readdir(d)) != NULL)
199 {
200 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
201 {
202 strcpy(filename, path2);
203 strcat(filename, "/");
204 strcat(filename, de->d_name);
205 dusize += Get_Folder_Size(filename, Display_Error);
206 }
207 else if (de->d_type == DT_REG)
208 {
209 strcpy(filename, path2);
210 strcat(filename, "/");
211 strcat(filename, de->d_name);
212 stat(filename, &st);
213 dusize += (unsigned long long)(st.st_size);
214 }
215 }
216 closedir(d);
217
218 return dusize;
219}
220
221bool TWFunc::Path_Exists(string Path) {
222 // Check to see if the Path exists
223 struct statfs st;
224
225 if (statfs(Path.c_str(), &st) != 0)
226 return false;
227 else
228 return true;
229}