Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 21 | #include <string> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <vector> |
| 24 | #include <dirent.h> |
| 25 | #include <time.h> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 26 | #include <errno.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <sys/mount.h> |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 29 | #include <sys/reboot.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 30 | #include <sys/sendfile.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/vfs.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/wait.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | #include <iostream> |
| 36 | #include <fstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 37 | #include <sstream> |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 38 | #include <cctype> |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 39 | #include <algorithm> |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 40 | #include <selinux/label.h> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 41 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 42 | #include "twcommon.h" |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 43 | #include "gui/gui.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 44 | #ifndef BUILD_TWRPTAR_MAIN |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 45 | #include "data.hpp" |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 46 | #include "partitions.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 47 | #include "variables.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 48 | #include "bootloader_message_twrp/include/bootloader_message_twrp/bootloader_message.h" |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 49 | #include "cutils/properties.h" |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 50 | #include "cutils/android_reboot.h" |
| 51 | #include <sys/reboot.h> |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 52 | #endif // ndef BUILD_TWRPTAR_MAIN |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 53 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 54 | #include "openaes/inc/oaes_lib.h" |
| 55 | #endif |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 56 | #include "set_metadata.h" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 57 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 58 | extern "C" { |
| 59 | #include "libcrecovery/common.h" |
| 60 | } |
| 61 | |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 62 | #ifdef TW_INCLUDE_LIBRESETPROP |
| 63 | #include <resetprop.h> |
| 64 | #endif |
| 65 | |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 66 | struct selabel_handle *selinux_handle; |
| 67 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | /* Execute a command */ |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 69 | int TWFunc::Exec_Cmd(const string& cmd, string &result, bool combine_stderr) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 70 | FILE* exec; |
| 71 | char buffer[130]; |
| 72 | int ret = 0; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 73 | std::string popen_cmd = cmd; |
| 74 | if (combine_stderr) |
| 75 | popen_cmd = cmd + " 2>&1"; |
| 76 | exec = __popen(popen_cmd.c_str(), "r"); |
| 77 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 78 | while (!feof(exec)) { |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 79 | if (fgets(buffer, 128, exec) != NULL) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 81 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 82 | } |
Dees_Troy | 29a0635 | 2013-08-24 12:06:47 +0000 | [diff] [blame] | 83 | ret = __pclose(exec); |
| 84 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 87 | int TWFunc::Exec_Cmd(const string& cmd, bool Show_Errors) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 88 | pid_t pid; |
| 89 | int status; |
| 90 | switch(pid = fork()) |
| 91 | { |
| 92 | case -1: |
bigbiff bigbiff | 731df79 | 2014-02-20 18:26:13 -0500 | [diff] [blame] | 93 | LOGERR("Exec_Cmd(): vfork failed: %d!\n", errno); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 94 | return -1; |
| 95 | case 0: // child |
| 96 | execl("/sbin/sh", "sh", "-c", cmd.c_str(), NULL); |
| 97 | _exit(127); |
| 98 | break; |
| 99 | default: |
| 100 | { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 101 | if (TWFunc::Wait_For_Child(pid, &status, cmd, Show_Errors) != 0) |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 102 | return -1; |
| 103 | else |
| 104 | return 0; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 109 | // Returns "file.name" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 110 | string TWFunc::Get_Filename(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 111 | size_t pos = Path.find_last_of("/"); |
| 112 | if (pos != string::npos) { |
| 113 | string Filename; |
| 114 | Filename = Path.substr(pos + 1, Path.size() - pos - 1); |
| 115 | return Filename; |
| 116 | } else |
| 117 | return Path; |
| 118 | } |
| 119 | |
| 120 | // Returns "/path/to/" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 121 | string TWFunc::Get_Path(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 122 | size_t pos = Path.find_last_of("/"); |
| 123 | if (pos != string::npos) { |
| 124 | string Pathonly; |
| 125 | Pathonly = Path.substr(0, pos + 1); |
| 126 | return Pathonly; |
| 127 | } else |
| 128 | return Path; |
| 129 | } |
| 130 | |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 131 | int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 132 | pid_t rc_pid; |
| 133 | |
| 134 | rc_pid = waitpid(pid, status, 0); |
| 135 | if (rc_pid > 0) { |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 136 | if (WIFSIGNALED(*status)) { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 137 | if (Show_Errors) |
| 138 | gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 139 | return -1; |
Vojtech Bocek | 0fdcbec | 2015-03-20 15:36:40 +0100 | [diff] [blame] | 140 | } else if (WEXITSTATUS(*status) == 0) { |
| 141 | LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success |
| 142 | } else { |
Ethan Yonker | 7e94158 | 2019-03-22 08:18:21 -0500 | [diff] [blame] | 143 | if (Show_Errors) |
| 144 | gui_msg(Msg(msg::kError, "pid_error={1} process ended with ERROR: {2}")(Child_Name)(WEXITSTATUS(*status))); // Graceful exit, but there was an error |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | } else { // no PID returned |
| 148 | if (errno == ECHILD) |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 149 | LOGERR("%s no child process exist\n", Child_Name.c_str()); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 150 | else { |
that | 2252d24 | 2015-04-03 22:33:04 +0200 | [diff] [blame] | 151 | LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
Ethan Yonker | ddb63e2 | 2017-01-19 14:01:57 -0600 | [diff] [blame] | 158 | int TWFunc::Wait_For_Child_Timeout(pid_t pid, int *status, const string& Child_Name, int timeout) { |
| 159 | pid_t retpid = waitpid(pid, status, WNOHANG); |
| 160 | for (; retpid == 0 && timeout; --timeout) { |
| 161 | sleep(1); |
| 162 | retpid = waitpid(pid, status, WNOHANG); |
| 163 | } |
| 164 | if (retpid == 0 && timeout == 0) { |
| 165 | LOGERR("%s took too long, killing process\n", Child_Name.c_str()); |
| 166 | kill(pid, SIGKILL); |
Ethan Yonker | ddb63e2 | 2017-01-19 14:01:57 -0600 | [diff] [blame] | 167 | for (timeout = 5; retpid == 0 && timeout; --timeout) { |
| 168 | sleep(1); |
| 169 | retpid = waitpid(pid, status, WNOHANG); |
| 170 | } |
| 171 | if (retpid) |
| 172 | LOGINFO("Child process killed successfully\n"); |
| 173 | else |
| 174 | LOGINFO("Child process took too long to kill, may be a zombie process\n"); |
| 175 | return -1; |
| 176 | } else if (retpid > 0) { |
| 177 | if (WIFSIGNALED(*status)) { |
| 178 | gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination |
| 179 | return -1; |
| 180 | } |
| 181 | } else if (retpid < 0) { // no PID returned |
| 182 | if (errno == ECHILD) |
| 183 | LOGERR("%s no child process exist\n", Child_Name.c_str()); |
| 184 | else { |
| 185 | LOGERR("%s Unexpected error %d\n", Child_Name.c_str(), errno); |
| 186 | return -1; |
| 187 | } |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 192 | bool TWFunc::Path_Exists(string Path) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 193 | struct stat st; |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 194 | return stat(Path.c_str(), &st) == 0; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 195 | } |
| 196 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 197 | Archive_Type TWFunc::Get_File_Type(string fn) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 198 | string::size_type i = 0; |
| 199 | int firstbyte = 0, secondbyte = 0; |
| 200 | char header[3]; |
| 201 | |
| 202 | ifstream f; |
| 203 | f.open(fn.c_str(), ios::in | ios::binary); |
| 204 | f.get(header, 3); |
| 205 | f.close(); |
| 206 | firstbyte = header[i] & 0xff; |
| 207 | secondbyte = header[++i] & 0xff; |
| 208 | |
| 209 | if (firstbyte == 0x1f && secondbyte == 0x8b) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 210 | return COMPRESSED; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 211 | else if (firstbyte == 0x4f && secondbyte == 0x41) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 212 | return ENCRYPTED; |
| 213 | return UNCOMPRESSED; // default |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int TWFunc::Try_Decrypting_File(string fn, string password) { |
| 217 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 218 | OAES_CTX * ctx = NULL; |
| 219 | uint8_t _key_data[32] = ""; |
| 220 | FILE *f; |
| 221 | uint8_t buffer[4096]; |
| 222 | uint8_t *buffer_out = NULL; |
| 223 | uint8_t *ptr = NULL; |
| 224 | size_t read_len = 0, out_len = 0; |
Matt Mower | 23d8aae | 2017-01-06 14:30:33 -0600 | [diff] [blame] | 225 | int firstbyte = 0, secondbyte = 0; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 226 | size_t _j = 0; |
| 227 | size_t _key_data_len = 0; |
| 228 | |
| 229 | // mostly kanged from OpenAES oaes.c |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 230 | for ( _j = 0; _j < 32; _j++ ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 231 | _key_data[_j] = _j + 1; |
| 232 | _key_data_len = password.size(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 233 | if ( 16 >= _key_data_len ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 234 | _key_data_len = 16; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 235 | else if ( 24 >= _key_data_len ) |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 236 | _key_data_len = 24; |
| 237 | else |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 238 | _key_data_len = 32; |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 239 | memcpy(_key_data, password.c_str(), password.size()); |
| 240 | |
| 241 | ctx = oaes_alloc(); |
| 242 | if (ctx == NULL) { |
| 243 | LOGERR("Failed to allocate OAES\n"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | oaes_key_import_data(ctx, _key_data, _key_data_len); |
| 248 | |
| 249 | f = fopen(fn.c_str(), "rb"); |
| 250 | if (f == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 251 | LOGERR("Failed to open '%s' to try decrypt: %s\n", fn.c_str(), strerror(errno)); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 252 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | read_len = fread(buffer, sizeof(uint8_t), 4096, f); |
| 256 | if (read_len <= 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 257 | LOGERR("Read size during try decrypt failed: %s\n", strerror(errno)); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 258 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 259 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 260 | return -1; |
| 261 | } |
| 262 | if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) { |
| 263 | LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n"); |
| 264 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 265 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | buffer_out = (uint8_t *) calloc(out_len, sizeof(char)); |
| 269 | if (buffer_out == NULL) { |
| 270 | LOGERR("Failed to allocate output buffer for try decrypt.\n"); |
| 271 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 272 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) { |
| 276 | LOGERR("Failed to decrypt file '%s'\n", fn.c_str()); |
| 277 | fclose(f); |
| 278 | free(buffer_out); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 279 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | fclose(f); |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 283 | oaes_free(&ctx); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 284 | if (out_len < 2) { |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 285 | LOGINFO("Successfully decrypted '%s' but read length too small.\n", fn.c_str()); |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 286 | free(buffer_out); |
| 287 | return 1; // Decrypted successfully |
| 288 | } |
| 289 | ptr = buffer_out; |
| 290 | firstbyte = *ptr & 0xff; |
| 291 | ptr++; |
| 292 | secondbyte = *ptr & 0xff; |
| 293 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 294 | LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str()); |
| 295 | free(buffer_out); |
| 296 | return 3; // Compressed |
| 297 | } |
| 298 | if (out_len >= 262) { |
| 299 | ptr = buffer_out + 257; |
| 300 | if (strncmp((char*)ptr, "ustar", 5) == 0) { |
| 301 | LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str()); |
| 302 | free(buffer_out); |
| 303 | return 2; // Tar |
| 304 | } |
| 305 | } |
| 306 | free(buffer_out); |
| 307 | LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str()); |
| 308 | return 1; // Decrypted successfully |
| 309 | #else |
| 310 | LOGERR("Encrypted backup support not included.\n"); |
| 311 | return -1; |
| 312 | #endif |
| 313 | } |
| 314 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 315 | unsigned long TWFunc::Get_File_Size(const string& Path) { |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 316 | struct stat st; |
| 317 | |
| 318 | if (stat(Path.c_str(), &st) != 0) |
| 319 | return 0; |
| 320 | return st.st_size; |
| 321 | } |
| 322 | |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 323 | std::string TWFunc::Remove_Trailing_Slashes(const std::string& path, bool leaveLast) |
| 324 | { |
| 325 | std::string res; |
| 326 | size_t last_idx = 0, idx = 0; |
| 327 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 328 | while (last_idx != std::string::npos) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 329 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 330 | if (last_idx != 0) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 331 | res += '/'; |
| 332 | |
| 333 | idx = path.find_first_of('/', last_idx); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 334 | if (idx == std::string::npos) { |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 335 | res += path.substr(last_idx, idx); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | res += path.substr(last_idx, idx-last_idx); |
| 340 | last_idx = path.find_first_not_of('/', idx); |
| 341 | } |
| 342 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 343 | if (leaveLast) |
Vojtech Bocek | 05f87d6 | 2014-03-11 22:08:23 +0100 | [diff] [blame] | 344 | res += '/'; |
| 345 | return res; |
| 346 | } |
| 347 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 348 | void TWFunc::Strip_Quotes(char* &str) { |
| 349 | if (strlen(str) > 0 && str[0] == '\"') |
| 350 | str++; |
| 351 | if (strlen(str) > 0 && str[strlen(str)-1] == '\"') |
| 352 | str[strlen(str)-1] = 0; |
| 353 | } |
| 354 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 355 | vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) { |
| 356 | vector<string> res; |
| 357 | |
| 358 | if (in.empty() || del == '\0') |
| 359 | return res; |
| 360 | |
| 361 | string field; |
| 362 | istringstream f(in); |
| 363 | if (del == '\n') { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 364 | while (getline(f, field)) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 365 | if (field.empty() && skip_empty) |
| 366 | continue; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 367 | res.push_back(field); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 368 | } |
| 369 | } else { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 370 | while (getline(f, field, del)) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 371 | if (field.empty() && skip_empty) |
| 372 | continue; |
| 373 | res.push_back(field); |
| 374 | } |
| 375 | } |
| 376 | return res; |
| 377 | } |
| 378 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 379 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 380 | { |
| 381 | timespec temp; |
| 382 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 383 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 384 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 385 | } else { |
| 386 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 387 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 388 | } |
| 389 | return temp; |
| 390 | } |
| 391 | |
| 392 | int32_t TWFunc::timespec_diff_ms(timespec& start, timespec& end) |
| 393 | { |
| 394 | return ((end.tv_sec * 1000) + end.tv_nsec/1000000) - |
| 395 | ((start.tv_sec * 1000) + start.tv_nsec/1000000); |
| 396 | } |
| 397 | |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 398 | #ifndef BUILD_TWRPTAR_MAIN |
| 399 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 400 | // Returns "/path" from a full /path/to/file.name |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 401 | string TWFunc::Get_Root_Path(const string& Path) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 402 | string Local_Path = Path; |
| 403 | |
| 404 | // Make sure that we have a leading slash |
| 405 | if (Local_Path.substr(0, 1) != "/") |
| 406 | Local_Path = "/" + Local_Path; |
| 407 | |
| 408 | // Trim the path to get the root path only |
| 409 | size_t position = Local_Path.find("/", 2); |
| 410 | if (position != string::npos) { |
| 411 | Local_Path.resize(position); |
| 412 | } |
| 413 | return Local_Path; |
| 414 | } |
| 415 | |
| 416 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 417 | int need_libs = 0; |
| 418 | |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 419 | if (!PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 420 | return; |
| 421 | |
| 422 | if (!PartitionManager.Mount_By_Path("/data", true)) |
| 423 | return; |
| 424 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 425 | gui_msg("install_dumlock=Installing HTC Dumlock to system..."); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 426 | copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 427 | if (!Path_Exists("/system/bin/flash_image")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 428 | LOGINFO("Installing flash_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 429 | copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 430 | need_libs = 1; |
| 431 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 432 | LOGINFO("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 433 | if (!Path_Exists("/system/bin/dump_image")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 434 | LOGINFO("Installing dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 435 | copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 436 | need_libs = 1; |
| 437 | } else |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 438 | LOGINFO("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 439 | if (need_libs) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 440 | LOGINFO("Installing libs needed for flash_image and dump_image...\n"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 441 | copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644); |
| 442 | copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644); |
| 443 | copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644); |
| 444 | copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 445 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 446 | LOGINFO("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 447 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 448 | unlink("/data/app/com.teamwin.htcdumlock*"); |
Dees Troy | 3454ade | 2015-01-20 19:21:04 +0000 | [diff] [blame] | 449 | copy_file(TWHTCD_PATH "HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 450 | sync(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 451 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
| 455 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 456 | return; |
| 457 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 458 | gui_msg("dumlock_restore=Restoring original boot..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 459 | Exec_Cmd("htcdumlock restore"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 460 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
| 464 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 465 | return; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 466 | gui_msg("dumlock_reflash=Reflashing recovery to boot..."); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 467 | Exec_Cmd("htcdumlock recovery noreboot"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 468 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 469 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 470 | |
| 471 | int TWFunc::Recursive_Mkdir(string Path) { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 472 | std::vector<std::string> parts = Split_String(Path, "/", true); |
| 473 | std::string cur_path; |
| 474 | for (size_t i = 0; i < parts.size(); ++i) { |
| 475 | cur_path += "/" + parts[i]; |
| 476 | if (!TWFunc::Path_Exists(cur_path)) { |
| 477 | if (mkdir(cur_path.c_str(), 0777)) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 478 | gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(cur_path)(strerror(errno))); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 479 | return false; |
| 480 | } else { |
that | f1408b3 | 2016-01-03 11:09:15 +0100 | [diff] [blame] | 481 | tw_set_default_metadata(cur_path.c_str()); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 482 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 483 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 484 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 485 | return true; |
| 486 | } |
| 487 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 488 | void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) { |
| 489 | string Display_Text; |
| 490 | |
| 491 | DataManager::GetValue(Read_Value, Display_Text); |
| 492 | if (Display_Text.empty()) |
| 493 | Display_Text = Default_Text; |
| 494 | |
| 495 | DataManager::SetValue("tw_operation", Display_Text); |
| 496 | DataManager::SetValue("tw_partition", ""); |
| 497 | } |
| 498 | |
| 499 | void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) { |
| 500 | string Display_Text; |
| 501 | |
| 502 | DataManager::GetValue(Read_Value, Display_Text); |
| 503 | if (Display_Text.empty()) |
| 504 | Display_Text = Default_Text; |
| 505 | |
| 506 | DataManager::SetValue("tw_operation", Display_Text); |
| 507 | DataManager::SetValue("tw_partition", Partition_Name); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 510 | void TWFunc::Copy_Log(string Source, string Destination) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 511 | int logPipe[2]; |
| 512 | int pigz_pid; |
| 513 | int destination_fd; |
| 514 | std::string destLogBuffer; |
| 515 | |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 516 | PartitionManager.Mount_By_Path(Destination, false); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 517 | |
| 518 | size_t extPos = Destination.find(".gz"); |
| 519 | std::string uncompressedLog(Destination); |
| 520 | uncompressedLog.replace(extPos, Destination.length(), ""); |
| 521 | |
| 522 | if (Path_Exists(Destination)) { |
| 523 | Archive_Type type = Get_File_Type(Destination); |
| 524 | if (type == COMPRESSED) { |
| 525 | std::string destFileBuffer; |
| 526 | std::string getCompressedContents = "pigz -c -d " + Destination; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 527 | if (Exec_Cmd(getCompressedContents, destFileBuffer, false) < 0) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 528 | LOGINFO("Unable to get destination logfile contents.\n"); |
| 529 | return; |
| 530 | } |
| 531 | destLogBuffer.append(destFileBuffer); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 532 | } |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 533 | } else if (Path_Exists(uncompressedLog)) { |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 534 | std::ifstream uncompressedIfs(uncompressedLog.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 535 | std::stringstream uncompressedSS; |
| 536 | uncompressedSS << uncompressedIfs.rdbuf(); |
| 537 | uncompressedIfs.close(); |
| 538 | std::string uncompressedLogBuffer(uncompressedSS.str()); |
| 539 | destLogBuffer.append(uncompressedLogBuffer); |
| 540 | std::remove(uncompressedLog.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 541 | } |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 542 | |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 543 | std::ifstream ifs(Source.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 544 | std::stringstream ss; |
| 545 | ss << ifs.rdbuf(); |
| 546 | std::string srcLogBuffer(ss.str()); |
| 547 | ifs.close(); |
| 548 | |
| 549 | if (pipe(logPipe) < 0) { |
| 550 | LOGINFO("Unable to open pipe to write to persistent log file: %s\n", Destination.c_str()); |
| 551 | } |
| 552 | |
| 553 | destination_fd = open(Destination.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 554 | |
| 555 | pigz_pid = fork(); |
| 556 | if (pigz_pid < 0) { |
| 557 | LOGINFO("fork() failed\n"); |
| 558 | close(destination_fd); |
| 559 | close(logPipe[0]); |
| 560 | close(logPipe[1]); |
| 561 | } else if (pigz_pid == 0) { |
| 562 | close(logPipe[1]); |
| 563 | dup2(logPipe[0], fileno(stdin)); |
| 564 | dup2(destination_fd, fileno(stdout)); |
| 565 | if (execlp("pigz", "pigz", "-", NULL) < 0) { |
| 566 | close(destination_fd); |
| 567 | close(logPipe[0]); |
| 568 | _exit(-1); |
| 569 | } |
| 570 | } else { |
| 571 | close(logPipe[0]); |
| 572 | if (write(logPipe[1], destLogBuffer.c_str(), destLogBuffer.size()) < 0) { |
| 573 | LOGINFO("Unable to append to persistent log: %s\n", Destination.c_str()); |
| 574 | close(logPipe[1]); |
| 575 | close(destination_fd); |
| 576 | return; |
| 577 | } |
| 578 | if (write(logPipe[1], srcLogBuffer.c_str(), srcLogBuffer.size()) < 0) { |
| 579 | LOGINFO("Unable to append to persistent log: %s\n", Destination.c_str()); |
| 580 | close(logPipe[1]); |
| 581 | close(destination_fd); |
| 582 | return; |
| 583 | } |
| 584 | close(logPipe[1]); |
| 585 | } |
| 586 | close(destination_fd); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 587 | } |
| 588 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 589 | void TWFunc::Update_Log_File(void) { |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 590 | std::string recoveryDir = get_log_dir() + "recovery/"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 591 | |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 592 | if (get_log_dir() == CACHE_LOGS_DIR) { |
| 593 | if (!PartitionManager.Mount_By_Path(CACHE_LOGS_DIR, false)) { |
| 594 | LOGINFO("Failed to mount %s for TWFunc::Update_Log_File\n", CACHE_LOGS_DIR); |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 595 | } |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 596 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 597 | |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 598 | if (!TWFunc::Path_Exists(recoveryDir)) { |
| 599 | LOGINFO("Recreating %s folder.\n", recoveryDir.c_str()); |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 600 | if (!Create_Dir_Recursive(recoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, 0, 0)) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 601 | LOGINFO("Unable to create %s folder.\n", recoveryDir.c_str()); |
| 602 | } |
| 603 | } |
| 604 | |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 605 | std::string logCopy = recoveryDir + "log.gz"; |
| 606 | std::string lastLogCopy = recoveryDir + "last_log.gz"; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 607 | copy_file(logCopy, lastLogCopy, 600); |
| 608 | Copy_Log(TMP_LOG_FILE, logCopy); |
| 609 | chown(logCopy.c_str(), 1000, 1000); |
| 610 | chmod(logCopy.c_str(), 0600); |
| 611 | chmod(lastLogCopy.c_str(), 0640); |
| 612 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 613 | // Reset bootloader message |
| 614 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 615 | if (Part != NULL) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 616 | std::string err; |
| 617 | if (!clear_bootloader_message((void*)&err)) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 618 | if (err == "no misc device set") { |
| 619 | LOGINFO("%s\n", err.c_str()); |
| 620 | } else { |
| 621 | LOGERR("%s\n", err.c_str()); |
| 622 | } |
| 623 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 624 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 625 | |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 626 | if (get_log_dir() == CACHE_LOGS_DIR) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 627 | if (PartitionManager.Mount_By_Path("/cache", false)) { |
| 628 | if (unlink("/cache/recovery/command") && errno != ENOENT) { |
| 629 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
| 630 | } |
Dees Troy | 9d7fdf5 | 2013-09-19 20:49:25 +0000 | [diff] [blame] | 631 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 632 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 633 | sync(); |
| 634 | } |
| 635 | |
| 636 | void TWFunc::Update_Intent_File(string Intent) { |
| 637 | if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 638 | TWFunc::write_to_file("/cache/recovery/intent", Intent); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 639 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 643 | int TWFunc::tw_reboot(RebootCommand command) |
| 644 | { |
Ethan Yonker | fe91611 | 2016-03-14 14:54:37 -0500 | [diff] [blame] | 645 | DataManager::Flush(); |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 646 | Update_Log_File(); |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 647 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 648 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 649 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 650 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 651 | switch (command) { |
| 652 | case rb_current: |
| 653 | case rb_system: |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 654 | Update_Intent_File("s"); |
| 655 | sync(); |
| 656 | check_and_run_script("/sbin/rebootsystem.sh", "reboot system"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 657 | #ifdef ANDROID_RB_PROPERTY |
| 658 | return property_set(ANDROID_RB_PROPERTY, "reboot,"); |
| 659 | #elif defined(ANDROID_RB_RESTART) |
| 660 | return android_reboot(ANDROID_RB_RESTART, 0, 0); |
| 661 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 662 | return reboot(RB_AUTOBOOT); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 663 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 664 | case rb_recovery: |
| 665 | check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 666 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 667 | return property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 668 | #else |
| 669 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery"); |
| 670 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 671 | case rb_bootloader: |
| 672 | check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 673 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 674 | return property_set(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 675 | #else |
| 676 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); |
| 677 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 678 | case rb_poweroff: |
| 679 | check_and_run_script("/sbin/poweroff.sh", "power off"); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 680 | #ifdef ANDROID_RB_PROPERTY |
| 681 | return property_set(ANDROID_RB_PROPERTY, "shutdown,"); |
| 682 | #elif defined(ANDROID_RB_POWEROFF) |
| 683 | return android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
| 684 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 685 | return reboot(RB_POWER_OFF); |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 686 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 687 | case rb_download: |
| 688 | check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 689 | #ifdef ANDROID_RB_PROPERTY |
Ethan Yonker | bc2bc6b | 2015-03-24 21:37:52 -0500 | [diff] [blame] | 690 | return property_set(ANDROID_RB_PROPERTY, "reboot,download"); |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 691 | #else |
| 692 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download"); |
| 693 | #endif |
mauronofrio | e9a49ef | 2018-10-03 13:38:16 +0200 | [diff] [blame] | 694 | case rb_edl: |
| 695 | check_and_run_script("/sbin/rebootedl.sh", "reboot edl"); |
| 696 | #ifdef ANDROID_RB_PROPERTY |
| 697 | return property_set(ANDROID_RB_PROPERTY, "reboot,edl"); |
| 698 | #else |
| 699 | return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "edl"); |
| 700 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 701 | default: |
| 702 | return -1; |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 703 | } |
| 704 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void TWFunc::check_and_run_script(const char* script_file, const char* display_name) |
| 708 | { |
| 709 | // Check for and run startup script if script exists |
| 710 | struct stat st; |
| 711 | if (stat(script_file, &st) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 712 | gui_msg(Msg("run_script=Running {1} script...")(display_name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 713 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 714 | TWFunc::Exec_Cmd(script_file); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 715 | gui_msg("done=Done."); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 716 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 717 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 718 | |
| 719 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 720 | DIR *d = opendir(path.c_str()); |
| 721 | int r = 0; |
| 722 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 723 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 724 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 725 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(path)(strerror(errno))); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 726 | return -1; |
| 727 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 728 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 729 | if (d) { |
| 730 | struct dirent *p; |
| 731 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 732 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 733 | continue; |
| 734 | new_path = path + "/"; |
| 735 | new_path.append(p->d_name); |
| 736 | if (p->d_type == DT_DIR) { |
| 737 | r = removeDir(new_path, true); |
| 738 | if (!r) { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 739 | if (p->d_type == DT_DIR) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 740 | r = rmdir(new_path.c_str()); |
| 741 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 742 | LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 743 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 744 | } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 745 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 746 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 747 | LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 748 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 752 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 753 | if (!r) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 754 | if (skipParent) |
| 755 | return 0; |
| 756 | else |
| 757 | r = rmdir(path.c_str()); |
| 758 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 759 | } |
| 760 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | int TWFunc::copy_file(string src, string dst, int mode) { |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 764 | PartitionManager.Mount_By_Path(src, false); |
| 765 | PartitionManager.Mount_By_Path(dst, false); |
| 766 | if (!Path_Exists(src)) { |
bigbiff | ace7eec | 2020-08-30 11:18:39 -0400 | [diff] [blame] | 767 | LOGINFO("Path %s does not exist. Unable to copy %s\n", src.c_str(), dst.c_str()); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 768 | return -1; |
| 769 | } |
bigbiff | d331705 | 2019-12-22 16:05:12 -0500 | [diff] [blame] | 770 | std::ifstream srcfile(src.c_str(), ios::binary); |
| 771 | std::ofstream dstfile(dst.c_str(), ios::binary); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 772 | dstfile << srcfile.rdbuf(); |
bigbiff bigbiff | e3ad534 | 2019-09-18 19:05:59 -0400 | [diff] [blame] | 773 | if (!dstfile.bad()) { |
| 774 | LOGINFO("Copied file %s to %s\n", src.c_str(), dst.c_str()); |
| 775 | } |
| 776 | else { |
| 777 | LOGINFO("Unable to copy file %s to %s\n", src.c_str(), dst.c_str()); |
| 778 | return -1; |
| 779 | } |
| 780 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 781 | srcfile.close(); |
| 782 | dstfile.close(); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 783 | if (chmod(dst.c_str(), mode) != 0) |
| 784 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 785 | return 0; |
| 786 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 787 | |
| 788 | unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { |
| 789 | struct stat st; |
| 790 | |
| 791 | stat(Path.c_str(), &st); |
| 792 | if (st.st_mode & S_IFDIR) |
| 793 | return DT_DIR; |
| 794 | else if (st.st_mode & S_IFBLK) |
| 795 | return DT_BLK; |
| 796 | else if (st.st_mode & S_IFCHR) |
| 797 | return DT_CHR; |
| 798 | else if (st.st_mode & S_IFIFO) |
| 799 | return DT_FIFO; |
| 800 | else if (st.st_mode & S_IFLNK) |
| 801 | return DT_LNK; |
| 802 | else if (st.st_mode & S_IFREG) |
| 803 | return DT_REG; |
| 804 | else if (st.st_mode & S_IFSOCK) |
| 805 | return DT_SOCK; |
| 806 | return DT_UNKNOWN; |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 807 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 808 | |
| 809 | int TWFunc::read_file(string fn, string& results) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 810 | ifstream file; |
| 811 | file.open(fn.c_str(), ios::in); |
| 812 | |
| 813 | if (file.is_open()) { |
| 814 | file >> results; |
| 815 | file.close(); |
| 816 | return 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 817 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 818 | |
| 819 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 820 | return -1; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 824 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 825 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 826 | file.open(fn.c_str(), ios::in); |
| 827 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 828 | while (getline(file, line)) |
| 829 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 830 | file.close(); |
| 831 | return 0; |
| 832 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 833 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 834 | return -1; |
| 835 | } |
| 836 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 837 | int TWFunc::read_file(string fn, uint64_t& results) { |
| 838 | ifstream file; |
| 839 | file.open(fn.c_str(), ios::in); |
| 840 | |
| 841 | if (file.is_open()) { |
| 842 | file >> results; |
| 843 | file.close(); |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
| 848 | return -1; |
| 849 | } |
| 850 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 851 | int TWFunc::write_to_file(const string& fn, const string& line) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 852 | FILE *file; |
| 853 | file = fopen(fn.c_str(), "w"); |
| 854 | if (file != NULL) { |
| 855 | fwrite(line.c_str(), line.size(), 1, file); |
| 856 | fclose(file); |
| 857 | return 0; |
| 858 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 859 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 863 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 864 | DIR* d; |
| 865 | |
| 866 | string Filename; |
| 867 | Restore_Path += "/"; |
| 868 | d = opendir(Restore_Path.c_str()); |
| 869 | if (d == NULL) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 870 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Path)(strerror(errno))); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 871 | return false; |
| 872 | } |
| 873 | |
| 874 | struct dirent* de; |
| 875 | while ((de = readdir(d)) != NULL) { |
| 876 | Filename = Restore_Path; |
| 877 | Filename += de->d_name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 878 | if (TWFunc::Get_File_Type(Filename) == ENCRYPTED) { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 879 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 880 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 881 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 882 | closedir(d); |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | closedir(d); |
| 888 | return true; |
| 889 | } |
| 890 | |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 891 | string TWFunc::Get_Current_Date() { |
| 892 | string Current_Date; |
| 893 | time_t seconds = time(0); |
| 894 | struct tm *t = localtime(&seconds); |
| 895 | char timestamp[255]; |
| 896 | sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec); |
| 897 | Current_Date = timestamp; |
| 898 | return Current_Date; |
| 899 | } |
| 900 | |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 901 | string TWFunc::System_Property_Get(string Prop_Name) { |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 902 | return System_Property_Get(Prop_Name, PartitionManager, PartitionManager.Get_Android_Root_Path()); |
| 903 | } |
| 904 | |
| 905 | string TWFunc::System_Property_Get(string Prop_Name, TWPartitionManager &PartitionManager, string Mount_Point) { |
| 906 | bool mount_state = PartitionManager.Is_Mounted_By_Path(Mount_Point); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 907 | std::vector<string> buildprop; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 908 | string propvalue; |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 909 | if (!PartitionManager.Mount_By_Path(Mount_Point, true)) |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 910 | return propvalue; |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 911 | string prop_file = Mount_Point + "/build.prop"; |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 912 | if (!TWFunc::Path_Exists(prop_file)) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 913 | prop_file = Mount_Point + "/system/build.prop"; // for devices with system as a root file system (e.g. Pixel) |
Ethan Yonker | b4bff5e | 2016-12-16 07:47:58 -0600 | [diff] [blame] | 914 | if (TWFunc::read_file(prop_file, buildprop) != 0) { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 915 | LOGINFO("Unable to open build.prop for getting '%s'.\n", Prop_Name.c_str()); |
Vojtech Bocek | a2e7016 | 2013-09-17 17:05:10 +0200 | [diff] [blame] | 916 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 917 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 918 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 919 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 920 | } |
| 921 | int line_count = buildprop.size(); |
| 922 | int index; |
| 923 | size_t start_pos = 0, end_pos; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 924 | string propname; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 925 | for (index = 0; index < line_count; index++) { |
| 926 | end_pos = buildprop.at(index).find("=", start_pos); |
| 927 | propname = buildprop.at(index).substr(start_pos, end_pos); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 928 | if (propname == Prop_Name) { |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 929 | propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size()); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 930 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 931 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 932 | return propvalue; |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 935 | if (!mount_state) |
Chaosmaster | 65633a4 | 2020-02-05 15:24:09 +0100 | [diff] [blame] | 936 | PartitionManager.UnMount_By_Path(Mount_Point, false); |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 937 | return propvalue; |
| 938 | } |
| 939 | |
| 940 | void TWFunc::Auto_Generate_Backup_Name() { |
| 941 | string propvalue = System_Property_Get("ro.build.display.id"); |
| 942 | if (propvalue.empty()) { |
| 943 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
| 944 | return; |
| 945 | } |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 946 | else { |
| 947 | //remove periods from build display so it doesn't confuse the extension code |
| 948 | propvalue.erase(remove(propvalue.begin(), propvalue.end(), '.'), propvalue.end()); |
| 949 | } |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 950 | string Backup_Name = Get_Current_Date(); |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 951 | Backup_Name += "_" + propvalue; |
Ethan Yonker | b555789 | 2014-02-07 21:43:20 -0600 | [diff] [blame] | 952 | if (Backup_Name.size() > MAX_BACKUP_NAME_LEN) |
| 953 | Backup_Name.resize(MAX_BACKUP_NAME_LEN); |
| 954 | // Trailing spaces cause problems on some file systems, so remove them |
| 955 | string space_check, space = " "; |
| 956 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 957 | while (space_check == space) { |
| 958 | Backup_Name.resize(Backup_Name.size() - 1); |
| 959 | space_check = Backup_Name.substr(Backup_Name.size() - 1, 1); |
| 960 | } |
bigbiff | 74a6d0d | 2015-02-14 20:49:44 -0500 | [diff] [blame] | 961 | replace(Backup_Name.begin(), Backup_Name.end(), ' ', '_'); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 962 | if (PartitionManager.Check_Backup_Name(Backup_Name, false, true) != 0) { |
| 963 | LOGINFO("Auto generated backup name '%s' is not valid, using date instead.\n", Backup_Name.c_str()); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 964 | DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date()); |
Ethan Yonker | 53796e7 | 2019-01-11 22:49:52 -0600 | [diff] [blame] | 965 | } else { |
| 966 | DataManager::SetValue(TW_BACKUP_NAME, Backup_Name); |
Ethan Yonker | 92d48e0 | 2014-02-26 12:05:55 -0600 | [diff] [blame] | 967 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 968 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 969 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 970 | void TWFunc::Fixup_Time_On_Boot(const string& time_paths /* = "" */) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 971 | { |
| 972 | #ifdef QCOM_RTC_FIX |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 973 | static bool fixed = false; |
| 974 | if (fixed) |
| 975 | return; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 976 | |
| 977 | LOGINFO("TWFunc::Fixup_Time: Pre-fix date and time: %s\n", TWFunc::Get_Current_Date().c_str()); |
| 978 | |
| 979 | struct timeval tv; |
| 980 | uint64_t offset = 0; |
| 981 | std::string sepoch = "/sys/class/rtc/rtc0/since_epoch"; |
| 982 | |
| 983 | if (TWFunc::read_file(sepoch, offset) == 0) { |
| 984 | |
| 985 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s\n", sepoch.c_str()); |
| 986 | |
| 987 | tv.tv_sec = offset; |
| 988 | tv.tv_usec = 0; |
| 989 | settimeofday(&tv, NULL); |
| 990 | |
| 991 | gettimeofday(&tv, NULL); |
| 992 | |
Phoenix591 | 46b05f2 | 2018-02-03 06:41:08 +0000 | [diff] [blame] | 993 | if (tv.tv_sec > 1517600000) { // Anything older then 2 Feb 2018 19:33:20 GMT will do nicely thank you ;) |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 994 | |
| 995 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 996 | fixed = true; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 997 | return; |
| 998 | |
| 999 | } |
| 1000 | |
| 1001 | } else { |
| 1002 | |
| 1003 | LOGINFO("TWFunc::Fixup_Time: opening %s failed\n", sepoch.c_str()); |
| 1004 | |
| 1005 | } |
| 1006 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1007 | LOGINFO("TWFunc::Fixup_Time: will attempt to use the ats files now.\n"); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1008 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1009 | // Devices with Qualcomm Snapdragon 800 do some shenanigans with RTC. |
| 1010 | // They never set it, it just ticks forward from 1970-01-01 00:00, |
| 1011 | // and then they have files /data/system/time/ats_* with 64bit offset |
| 1012 | // in miliseconds which, when added to the RTC, gives the correct time. |
| 1013 | // So, the time is: (offset_from_ats + value_from_RTC) |
| 1014 | // There are multiple ats files, they are for different systems? Bases? |
| 1015 | // Like, ats_1 is for modem and ats_2 is for TOD (time of day?). |
| 1016 | // Look at file time_genoff.h in CodeAurora, qcom-opensource/time-services |
| 1017 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1018 | std::vector<std::string> paths; // space separated list of paths |
| 1019 | if (time_paths.empty()) { |
Mauronofrio Matarrese | 2dab70d | 2019-03-05 02:22:26 +0100 | [diff] [blame] | 1020 | paths = Split_String("/data/system/time/ /data/time/ /data/vendor/time/", " "); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1021 | if (!PartitionManager.Mount_By_Path("/data", false)) |
| 1022 | return; |
| 1023 | } else { |
| 1024 | // When specific path(s) are used, Fixup_Time needs those |
| 1025 | // partitions to already be mounted! |
| 1026 | paths = Split_String(time_paths, " "); |
| 1027 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1028 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1029 | FILE *f; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1030 | offset = 0; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1031 | struct dirent *dt; |
| 1032 | std::string ats_path; |
| 1033 | |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1034 | // Prefer ats_2, it seems to be the one we want according to logcat on hammerhead |
| 1035 | // - it is the one for ATS_TOD (time of day?). |
| 1036 | // However, I never saw a device where the offset differs between ats files. |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1037 | for (size_t i = 0; i < paths.size(); ++i) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1038 | { |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1039 | DIR *d = opendir(paths[i].c_str()); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1040 | if (!d) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1041 | continue; |
| 1042 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1043 | while ((dt = readdir(d))) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1044 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1045 | if (dt->d_type != DT_REG || strncmp(dt->d_name, "ats_", 4) != 0) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1046 | continue; |
| 1047 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1048 | if (ats_path.empty() || strcmp(dt->d_name, "ats_2") == 0) |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1049 | ats_path = paths[i] + dt->d_name; |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | closedir(d); |
| 1053 | } |
| 1054 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1055 | if (ats_path.empty()) { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1056 | LOGINFO("TWFunc::Fixup_Time: no ats files found, leaving untouched!\n"); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1057 | } else if ((f = fopen(ats_path.c_str(), "r")) == NULL) { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1058 | LOGINFO("TWFunc::Fixup_Time: failed to open file %s\n", ats_path.c_str()); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1059 | } else if (fread(&offset, sizeof(offset), 1, f) != 1) { |
Dees Troy | 3e254b9 | 2014-03-06 20:24:54 +0000 | [diff] [blame] | 1060 | LOGINFO("TWFunc::Fixup_Time: failed load uint64 from file %s\n", ats_path.c_str()); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1061 | fclose(f); |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1062 | } else { |
| 1063 | fclose(f); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1064 | |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 1065 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from file %s, offset %llu\n", ats_path.c_str(), (unsigned long long) offset); |
| 1066 | DataManager::SetValue("tw_qcom_ats_offset", (unsigned long long) offset, 1); |
| 1067 | fixed = true; |
| 1068 | } |
| 1069 | |
| 1070 | if (!fixed) { |
| 1071 | // Failed to get offset from ats file, check twrp settings |
| 1072 | unsigned long long value; |
| 1073 | if (DataManager::GetValue("tw_qcom_ats_offset", value) < 0) { |
| 1074 | return; |
| 1075 | } else { |
| 1076 | offset = (uint64_t) value; |
| 1077 | LOGINFO("TWFunc::Fixup_Time: Setting time offset from twrp setting file, offset %llu\n", (unsigned long long) offset); |
| 1078 | // Do not consider the settings file as a definitive answer, keep fixed=false so next run will try ats files again |
| 1079 | } |
| 1080 | } |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1081 | |
| 1082 | gettimeofday(&tv, NULL); |
| 1083 | |
| 1084 | tv.tv_sec += offset/1000; |
Phoenix591 | e444d11 | 2018-02-03 07:23:54 +0000 | [diff] [blame] | 1085 | #ifdef TW_CLOCK_OFFSET |
| 1086 | // Some devices are even quirkier and have ats files that are offset from the actual time |
| 1087 | tv.tv_sec = tv.tv_sec + TW_CLOCK_OFFSET; |
| 1088 | #endif |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1089 | tv.tv_usec += (offset%1000)*1000; |
| 1090 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1091 | while (tv.tv_usec >= 1000000) |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1092 | { |
| 1093 | ++tv.tv_sec; |
| 1094 | tv.tv_usec -= 1000000; |
| 1095 | } |
| 1096 | |
| 1097 | settimeofday(&tv, NULL); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1098 | |
| 1099 | LOGINFO("TWFunc::Fixup_Time: Date and time corrected: %s\n", TWFunc::Get_Current_Date().c_str()); |
Vojtech Bocek | d0e38bc | 2014-02-03 23:36:57 +0100 | [diff] [blame] | 1100 | #endif |
| 1101 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1102 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1103 | std::vector<std::string> TWFunc::Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty) |
| 1104 | { |
| 1105 | std::vector<std::string> res; |
| 1106 | size_t idx = 0, idx_last = 0; |
| 1107 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1108 | while (idx < str.size()) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1109 | { |
| 1110 | idx = str.find_first_of(delimiter, idx_last); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1111 | if (idx == std::string::npos) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1112 | idx = str.size(); |
| 1113 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1114 | if (idx-idx_last != 0 || !removeEmpty) |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 1115 | res.push_back(str.substr(idx_last, idx-idx_last)); |
| 1116 | |
| 1117 | idx_last = idx + delimiter.size(); |
| 1118 | } |
| 1119 | |
| 1120 | return res; |
| 1121 | } |
| 1122 | |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1123 | bool TWFunc::Create_Dir_Recursive(const std::string& path, mode_t mode, uid_t uid, gid_t gid) |
| 1124 | { |
| 1125 | std::vector<std::string> parts = Split_String(path, "/"); |
| 1126 | std::string cur_path; |
| 1127 | struct stat info; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1128 | for (size_t i = 0; i < parts.size(); ++i) |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1129 | { |
| 1130 | cur_path += "/" + parts[i]; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1131 | if (stat(cur_path.c_str(), &info) < 0 || !S_ISDIR(info.st_mode)) |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1132 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 1133 | if (mkdir(cur_path.c_str(), mode) < 0) |
Vojtech Bocek | 03fd6c5 | 2014-03-13 18:46:34 +0100 | [diff] [blame] | 1134 | return false; |
| 1135 | chown(cur_path.c_str(), uid, gid); |
| 1136 | } |
| 1137 | } |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1141 | int TWFunc::Set_Brightness(std::string brightness_value) |
| 1142 | { |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1143 | int result = -1; |
| 1144 | std::string secondary_brightness_file; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1145 | |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1146 | if (DataManager::GetIntValue("tw_has_brightnesss_file")) { |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1147 | LOGINFO("TWFunc::Set_Brightness: Setting brightness control to %s\n", brightness_value.c_str()); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1148 | result = TWFunc::write_to_file(DataManager::GetStrValue("tw_brightness_file"), brightness_value); |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1149 | DataManager::GetValue("tw_secondary_brightness_file", secondary_brightness_file); |
| 1150 | if (!secondary_brightness_file.empty()) { |
| 1151 | LOGINFO("TWFunc::Set_Brightness: Setting secondary brightness control to %s\n", brightness_value.c_str()); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1152 | TWFunc::write_to_file(secondary_brightness_file, brightness_value); |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1153 | } |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1154 | } |
Tatsuyuki Ishi | 548a175 | 2015-12-28 10:08:58 +0900 | [diff] [blame] | 1155 | return result; |
xNUTx | e85f02d | 2014-07-18 01:30:58 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1158 | bool TWFunc::Toggle_MTP(bool enable) { |
| 1159 | #ifdef TW_HAS_MTP |
| 1160 | static int was_enabled = false; |
| 1161 | |
| 1162 | if (enable && was_enabled) { |
| 1163 | if (!PartitionManager.Enable_MTP()) |
| 1164 | PartitionManager.Disable_MTP(); |
| 1165 | } else { |
| 1166 | was_enabled = DataManager::GetIntValue("tw_mtp_enabled"); |
| 1167 | PartitionManager.Disable_MTP(); |
| 1168 | usleep(500); |
| 1169 | } |
| 1170 | return was_enabled; |
| 1171 | #else |
| 1172 | return false; |
| 1173 | #endif |
| 1174 | } |
| 1175 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 1176 | void TWFunc::SetPerformanceMode(bool mode) { |
| 1177 | if (mode) { |
| 1178 | property_set("recovery.perf.mode", "1"); |
| 1179 | } else { |
| 1180 | property_set("recovery.perf.mode", "0"); |
| 1181 | } |
| 1182 | // Some time for events to catch up to init handlers |
| 1183 | usleep(500000); |
| 1184 | } |
| 1185 | |
Jenkins | 1710bf2 | 2014-10-02 20:22:21 -0400 | [diff] [blame] | 1186 | std::string TWFunc::to_string(unsigned long value) { |
| 1187 | std::ostringstream os; |
| 1188 | os << value; |
| 1189 | return os.str(); |
| 1190 | } |
| 1191 | |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1192 | void TWFunc::Disable_Stock_Recovery_Replace(void) { |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1193 | if (PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false)) { |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1194 | // Disable flashing of stock recovery |
| 1195 | if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) { |
| 1196 | rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1197 | gui_msg("rename_stock=Renamed stock recovery file in /system to prevent the stock ROM from replacing TWRP."); |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1198 | sync(); |
| 1199 | } |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1200 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
Ethan Yonker | 9132d91 | 2015-02-02 10:32:49 -0600 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1204 | unsigned long long TWFunc::IOCTL_Get_Block_Size(const char* block_device) { |
| 1205 | unsigned long block_device_size; |
| 1206 | int ret = 0; |
| 1207 | |
| 1208 | int fd = open(block_device, O_RDONLY); |
| 1209 | if (fd < 0) { |
| 1210 | LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", block_device, strerror(errno)); |
| 1211 | } else { |
| 1212 | ret = ioctl(fd, BLKGETSIZE, &block_device_size); |
| 1213 | close(fd); |
| 1214 | if (ret) { |
| 1215 | LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno)); |
| 1216 | } else { |
| 1217 | return (unsigned long long)(block_device_size) * 512LLU; |
| 1218 | } |
| 1219 | } |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 1223 | void TWFunc::copy_kernel_log(string curr_storage) { |
| 1224 | std::string dmesgDst = curr_storage + "/dmesg.log"; |
| 1225 | std::string dmesgCmd = "/sbin/dmesg"; |
| 1226 | |
| 1227 | std::string result; |
bigbiff | 56b02eb | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 1228 | Exec_Cmd(dmesgCmd, result, false); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1229 | write_to_file(dmesgDst, result); |
bigbiff bigbiff | bad332a | 2016-07-29 21:18:13 -0400 | [diff] [blame] | 1230 | gui_msg(Msg("copy_kernel_log=Copied kernel log to {1}")(dmesgDst)); |
| 1231 | tw_set_default_metadata(dmesgDst.c_str()); |
| 1232 | } |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 1233 | |
| 1234 | bool TWFunc::isNumber(string strtocheck) { |
| 1235 | int num = 0; |
| 1236 | std::istringstream iss(strtocheck); |
| 1237 | |
| 1238 | if (!(iss >> num).fail()) |
| 1239 | return true; |
| 1240 | else |
| 1241 | return false; |
| 1242 | } |
| 1243 | |
| 1244 | int TWFunc::stream_adb_backup(string &Restore_Name) { |
| 1245 | string cmd = "/sbin/bu --twrp stream " + Restore_Name; |
| 1246 | LOGINFO("stream_adb_backup: %s\n", cmd.c_str()); |
| 1247 | int ret = TWFunc::Exec_Cmd(cmd); |
| 1248 | if (ret != 0) |
| 1249 | return -1; |
| 1250 | return ret; |
| 1251 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1252 | |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1253 | std::string TWFunc::get_log_dir() { |
| 1254 | if (PartitionManager.Find_Partition_By_Path(CACHE_LOGS_DIR) == NULL) { |
| 1255 | if (PartitionManager.Find_Partition_By_Path(DATA_LOGS_DIR) == NULL) { |
bigbiff | ace7eec | 2020-08-30 11:18:39 -0400 | [diff] [blame] | 1256 | LOGINFO("Unable to find a directory to store TWRP logs."); |
| 1257 | return ""; |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 1258 | } else { |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1259 | return DATA_LOGS_DIR; |
bigbiff bigbiff | e4bdb15 | 2019-03-23 18:33:17 -0400 | [diff] [blame] | 1260 | } |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1261 | } |
| 1262 | else { |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1263 | return CACHE_LOGS_DIR; |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | void TWFunc::check_selinux_support() { |
| 1268 | if (TWFunc::Path_Exists("/prebuilt_file_contexts")) { |
| 1269 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 1270 | printf("Renaming regular /file_contexts -> /file_contexts.bak\n"); |
| 1271 | rename("/file_contexts", "/file_contexts.bak"); |
| 1272 | } |
| 1273 | printf("Moving /prebuilt_file_contexts -> /file_contexts\n"); |
| 1274 | rename("/prebuilt_file_contexts", "/file_contexts"); |
| 1275 | } |
| 1276 | struct selinux_opt selinux_options[] = { |
| 1277 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 1278 | }; |
| 1279 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
| 1280 | if (!selinux_handle) |
| 1281 | printf("No file contexts for SELinux\n"); |
| 1282 | else |
| 1283 | printf("SELinux contexts loaded from /file_contexts\n"); |
| 1284 | { // Check to ensure SELinux can be supported by the kernel |
| 1285 | char *contexts = NULL; |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1286 | std::string cacheDir = TWFunc::get_log_dir(); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1287 | std::string se_context_check = cacheDir + "recovery/"; |
| 1288 | int ret = 0; |
| 1289 | |
bigbiff | 349ea55 | 2020-06-19 16:07:38 -0400 | [diff] [blame] | 1290 | if (cacheDir == CACHE_LOGS_DIR) { |
| 1291 | PartitionManager.Mount_By_Path(CACHE_LOGS_DIR, false); |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1292 | } |
| 1293 | if (TWFunc::Path_Exists(se_context_check)) { |
| 1294 | ret = lgetfilecon(se_context_check.c_str(), &contexts); |
Makornthawat Emery | abc299c | 2019-03-29 13:45:22 +0000 | [diff] [blame] | 1295 | if (ret < 0) { |
bigbiff bigbiff | 19874f1 | 2019-01-08 20:06:57 -0500 | [diff] [blame] | 1296 | LOGINFO("Could not check %s SELinux contexts, using /sbin/teamwin instead which may be inaccurate.\n", se_context_check.c_str()); |
| 1297 | lgetfilecon("/sbin/teamwin", &contexts); |
| 1298 | } |
| 1299 | } |
| 1300 | if (ret < 0) { |
| 1301 | gui_warn("no_kernel_selinux=Kernel does not have support for reading SELinux contexts."); |
| 1302 | } else { |
| 1303 | free(contexts); |
| 1304 | gui_msg("full_selinux=Full SELinux support is present."); |
| 1305 | } |
| 1306 | } |
| 1307 | } |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1308 | |
| 1309 | bool TWFunc::Is_TWRP_App_In_System() { |
Captain Throwback | cc9746b | 2020-12-21 11:28:34 -0500 | [diff] [blame] | 1310 | bool is_system_mounted = true; |
| 1311 | if(!PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path())) { |
| 1312 | is_system_mounted = false; |
| 1313 | PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1314 | } |
Captain Throwback | cc9746b | 2020-12-21 11:28:34 -0500 | [diff] [blame] | 1315 | string base_path = PartitionManager.Get_Android_Root_Path(); |
| 1316 | if (TWFunc::Path_Exists(PartitionManager.Get_Android_Root_Path() + "/system")) |
| 1317 | base_path += "/system"; // For devices with system as a root file system (e.g. Pixel) |
| 1318 | string install_path = base_path + "/priv-app"; |
| 1319 | if (!TWFunc::Path_Exists(install_path)) |
| 1320 | install_path = base_path + "/app"; |
| 1321 | install_path += "/twrpapp"; |
| 1322 | if (TWFunc::Path_Exists(install_path)) { |
| 1323 | LOGINFO("App found at '%s'\n", install_path.c_str()); |
| 1324 | DataManager::SetValue("tw_app_installed_in_system", 1); |
| 1325 | return true; |
| 1326 | } |
| 1327 | if (!is_system_mounted) |
| 1328 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
Ethan Yonker | 76bbd3a | 2019-05-10 10:50:04 -0500 | [diff] [blame] | 1329 | DataManager::SetValue("tw_app_installed_in_system", 0); |
| 1330 | return false; |
| 1331 | } |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 1332 | |
epicX | 9d930a2 | 2020-12-29 00:39:36 +0530 | [diff] [blame] | 1333 | void TWFunc::checkforapp(){ |
| 1334 | |
| 1335 | string sdkverstr = System_Property_Get("ro.build.version.sdk"); |
| 1336 | int sdkver = 0; |
| 1337 | if (!sdkverstr.empty()) { |
| 1338 | sdkver = atoi(sdkverstr.c_str()); |
| 1339 | } |
| 1340 | if (sdkver <= 13) { |
| 1341 | if (sdkver == 0) |
| 1342 | LOGINFO("Unable to read sdk version from build prop\n"); |
| 1343 | else |
| 1344 | LOGINFO("SDK version too low for TWRP app (%i < 14)\n", sdkver); |
| 1345 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1346 | goto exit; |
| 1347 | } |
| 1348 | if (Is_TWRP_App_In_System()) { |
| 1349 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1350 | goto exit; |
| 1351 | } |
| 1352 | if (PartitionManager.Mount_By_Path("/data", false)) { |
| 1353 | const char parent_path[] = "/data/app"; |
| 1354 | const char app_prefix[] = "me.twrp.twrpapp-"; |
| 1355 | DIR *d = opendir(parent_path); |
| 1356 | if (d) { |
| 1357 | struct dirent *p; |
| 1358 | while ((p = readdir(d))) { |
| 1359 | if (p->d_type != DT_DIR || strlen(p->d_name) < strlen(app_prefix) || strncmp(p->d_name, app_prefix, strlen(app_prefix))) |
| 1360 | continue; |
| 1361 | closedir(d); |
| 1362 | LOGINFO("App found at '%s/%s'\n", parent_path, p->d_name); |
| 1363 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1364 | goto exit; |
| 1365 | } |
| 1366 | closedir(d); |
| 1367 | } |
| 1368 | } else { |
| 1369 | LOGINFO("Data partition cannot be mounted during app check\n"); |
| 1370 | DataManager::SetValue("tw_app_install_status", 2); // 0 = no status, 1 = not installed, 2 = already installed or do not install |
| 1371 | } |
| 1372 | |
| 1373 | LOGINFO("App not installed\n"); |
| 1374 | DataManager::SetValue("tw_app_install_status", 1); // 0 = no status, 1 = not installed, 2 = already installed |
| 1375 | exit: |
| 1376 | return; |
| 1377 | |
| 1378 | } |
| 1379 | |
Chaosmaster | 461e39f | 2020-02-07 01:48:13 +0100 | [diff] [blame] | 1380 | int TWFunc::Property_Override(string Prop_Name, string Prop_Value) { |
| 1381 | #ifdef TW_INCLUDE_LIBRESETPROP |
| 1382 | return setprop(Prop_Name.c_str(), Prop_Value.c_str(), false); |
| 1383 | #else |
| 1384 | return -2; |
| 1385 | #endif |
| 1386 | } |
| 1387 | |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 1388 | bool TWFunc::Get_Encryption_Policy(ext4_encryption_policy &policy, std::string path) { |
bigbiff | f62d249 | 2020-05-20 10:15:34 -0400 | [diff] [blame] | 1389 | #ifdef TW_INCLUDE_FBE |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 1390 | if (!TWFunc::Path_Exists(path)) { |
| 1391 | LOGERR("Unable to find %s to get policy\n", path.c_str()); |
| 1392 | return false; |
| 1393 | } |
| 1394 | if (!e4crypt_policy_get_struct(path.c_str(), &policy)) { |
| 1395 | LOGERR("No policy set for path %s\n", path.c_str()); |
| 1396 | return false; |
| 1397 | } |
bigbiff | f62d249 | 2020-05-20 10:15:34 -0400 | [diff] [blame] | 1398 | #endif |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | bool TWFunc::Set_Encryption_Policy(std::string path, const ext4_encryption_policy &policy) { |
bigbiff | f62d249 | 2020-05-20 10:15:34 -0400 | [diff] [blame] | 1403 | #ifdef TW_INCLUDE_FBE |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 1404 | if (!TWFunc::Path_Exists(path)) { |
| 1405 | LOGERR("unable to find %s to set policy\n", path.c_str()); |
| 1406 | return false; |
| 1407 | } |
| 1408 | char binary_policy[EXT4_KEY_DESCRIPTOR_SIZE]; |
| 1409 | char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX]; |
| 1410 | policy_to_hex(binary_policy, policy_hex); |
| 1411 | if (!e4crypt_policy_set_struct(path.c_str(), &policy)) { |
| 1412 | LOGERR("unable to set policy for path: %s\n", path.c_str()); |
| 1413 | return false; |
| 1414 | } |
bigbiff | f62d249 | 2020-05-20 10:15:34 -0400 | [diff] [blame] | 1415 | #endif |
bigbiff | a2bd7b7 | 2020-05-07 21:45:34 -0400 | [diff] [blame] | 1416 | return true; |
| 1417 | } |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1418 | |
| 1419 | string TWFunc::Check_For_TwrpFolder(){ |
| 1420 | string oldFolder = ""; |
| 1421 | vector<string> customTWRPFolders; |
| 1422 | string mainPath = DataManager::GetCurrentStoragePath(); |
| 1423 | DIR* d; |
| 1424 | struct dirent* de; |
| 1425 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1426 | if (DataManager::GetIntValue(TW_IS_ENCRYPTED)) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1427 | goto exit; |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | d = opendir(mainPath.c_str()); |
| 1432 | if (d == NULL) { |
| 1433 | goto exit; |
| 1434 | } |
| 1435 | |
| 1436 | while ((de = readdir(d)) != NULL) { |
| 1437 | string name = de->d_name; |
| 1438 | string fullPath = mainPath + '/' + name; |
| 1439 | unsigned char type = de->d_type; |
| 1440 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1441 | if (name == "." || name == "..") continue; |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1442 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1443 | if (type == DT_UNKNOWN) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1444 | type = Get_D_Type_From_Stat(fullPath); |
| 1445 | } |
| 1446 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1447 | if (type == DT_DIR && Path_Exists(fullPath + '/' + TW_SETTINGS_FILE)) { |
| 1448 | if ('/' + name == TW_DEFAULT_RECOVERY_FOLDER) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1449 | oldFolder = name; |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1450 | } else { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1451 | customTWRPFolders.push_back(name); |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | closedir(d); |
| 1457 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1458 | if (oldFolder == "" && customTWRPFolders.empty()) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1459 | LOGINFO("No recovery folder found. Using default folder.\n"); |
| 1460 | goto exit; |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1461 | } else if (customTWRPFolders.empty()) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1462 | LOGINFO("No custom recovery folder found. Using TWRP as default.\n"); |
| 1463 | goto exit; |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1464 | } else { |
| 1465 | if (customTWRPFolders.size() > 1) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1466 | LOGINFO("More than one custom recovery folder found. Using first one from the list.\n"); |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1467 | } else { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1468 | LOGINFO("One custom recovery folder found.\n"); |
| 1469 | } |
| 1470 | string customPath = '/' + customTWRPFolders.at(0); |
| 1471 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1472 | if (Path_Exists(mainPath + TW_DEFAULT_RECOVERY_FOLDER)) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1473 | string oldBackupFolder = mainPath + TW_DEFAULT_RECOVERY_FOLDER + "/BACKUPS/" + DataManager::GetStrValue("device_id"); |
| 1474 | string newBackupFolder = mainPath + customPath + "/BACKUPS/" + DataManager::GetStrValue("device_id"); |
| 1475 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1476 | if (Path_Exists(oldBackupFolder)) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1477 | vector<string> backups; |
| 1478 | d = opendir(oldBackupFolder.c_str()); |
| 1479 | |
| 1480 | if (d != NULL) { |
| 1481 | while ((de = readdir(d)) != NULL) { |
| 1482 | string name = de->d_name; |
| 1483 | unsigned char type = de->d_type; |
| 1484 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1485 | if (name == "." || name == "..") continue; |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1486 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1487 | if (type == DT_UNKNOWN) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1488 | type = Get_D_Type_From_Stat(mainPath + '/' + name); |
| 1489 | } |
| 1490 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1491 | if (type == DT_DIR) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1492 | backups.push_back(name); |
| 1493 | } |
| 1494 | } |
| 1495 | closedir(d); |
| 1496 | } |
| 1497 | |
epicX | fb61038 | 2021-03-01 00:02:57 +0530 | [diff] [blame] | 1498 | for (auto it = backups.begin(); it != backups.end(); it++) { |
epicX | 98053c3 | 2021-01-04 13:01:31 +0530 | [diff] [blame] | 1499 | Exec_Cmd("mv -f \"" + oldBackupFolder + '/' + *it + "\" \"" + newBackupFolder + '/' + *it + (Path_Exists(newBackupFolder + '/' + *it) ? "_new\"" : "\"")); |
| 1500 | } |
| 1501 | } |
| 1502 | Exec_Cmd("rm -rf \"" + mainPath + TW_DEFAULT_RECOVERY_FOLDER + '\"'); |
| 1503 | } |
| 1504 | |
| 1505 | return customPath; |
| 1506 | } |
| 1507 | |
| 1508 | exit: |
| 1509 | return TW_DEFAULT_RECOVERY_FOLDER; |
| 1510 | } |
Ethan Yonker | af2897c | 2014-02-10 13:07:14 -0600 | [diff] [blame] | 1511 | #endif // ndef BUILD_TWRPTAR_MAIN |