blob: 90c60165e23f4f503115e43cb7258040d996655c [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";
28 if (access(MD5_File.c_str(), F_OK ) != -1) {
29 DirPath = Get_Path(File);
30 chdir(DirPath.c_str());
31 MD5_File = Get_Filename(MD5_File);
32 Command = "/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);
41
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 }
59 fclose(cs);
60 } 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;
230}