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