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