blob: 203638a6309fb8fb06ea88970ecc14fa3ce0b1d9 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
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_Troy6ef66352013-02-21 08:26:57 -060019#ifndef _TWRPFUNCTIONS_HPP
20#define _TWRPFUNCTIONS_HPP
21
22#include <string>
Dees_Troy2673cec2013-04-02 20:22:16 +000023#include <vector>
Dees_Troy6ef66352013-02-21 08:26:57 -060024
bigbiff bigbiff56cf5642016-08-19 17:43:45 -040025#include "twrpDigest/twrpDigest.hpp"
26
Chaosmaster65633a42020-02-05 15:24:09 +010027#ifndef BUILD_TWRPTAR_MAIN
28#include "partitions.hpp"
29#endif
30
Dees_Troy6ef66352013-02-21 08:26:57 -060031using namespace std;
32
bigbiff22851b92021-09-01 16:46:57 -040033#define CACHE_LOGS_DIR "/cache/" // For devices with a dedicated cache partition
34#define DATA_LOGS_DIR "/data/" // For devices that do not have a dedicated cache partition
bigbiff bigbiff19874f12019-01-08 20:06:57 -050035
Dees_Troy6ef66352013-02-21 08:26:57 -060036typedef enum
37{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020038 rb_current = 0,
39 rb_system,
40 rb_recovery,
41 rb_poweroff,
bigbiffdf8436b2020-08-30 16:22:34 -040042 rb_bootloader,
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020043 rb_download,
mauronofrioe9a49ef2018-10-03 13:38:16 +020044 rb_edl,
bigbiffdf8436b2020-08-30 16:22:34 -040045 rb_fastboot
Dees_Troy6ef66352013-02-21 08:26:57 -060046} RebootCommand;
47
bigbiffce8f83c2015-12-12 18:30:21 -050048enum Archive_Type {
49 UNCOMPRESSED = 0,
50 COMPRESSED,
51 ENCRYPTED,
52 COMPRESSED_ENCRYPTED
53};
54
Dees_Troy6ef66352013-02-21 08:26:57 -060055// Partition class
56class TWFunc
57{
58public:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060059 static string Get_Root_Path(const string& Path); // Trims any trailing folders or filenames from the path, also adds a leading / if not present
60 static string Get_Path(const string& Path); // Trims everything after the last / in the string
61 static string Get_Filename(const string& Path); // Trims the path off of a filename
Dees_Troy6ef66352013-02-21 08:26:57 -060062
bigbiffad58e1b2020-07-06 20:24:34 -040063 static int Exec_Cmd(const string& cmd, string &result, bool combine_stderr); //execute a command and return the result as a string by reference, set combined_stderror to add stderr
64 static int Exec_Cmd(const string& cmd, bool Show_Errors = true); //execute a command, displays an error to the GUI if Show_Errors is true, Show_Errors is true by default
Ethan Yonker7e941582019-03-22 08:18:21 -050065 static int Wait_For_Child(pid_t pid, int *status, string Child_Name, bool Show_Errors = true); // Waits for pid to exit and checks exit status, displays an error to the GUI if Show_Errors is true which is the default
Ethan Yonkerddb63e22017-01-19 14:01:57 -060066 static int Wait_For_Child_Timeout(pid_t pid, int *status, const string& Child_Name, int timeout); // Waits for a pid to exit until the timeout is hit. If timeout is hit, kill the chilld.
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060067 static bool Path_Exists(string Path); // Returns true if the path exists
bigbiffce8f83c2015-12-12 18:30:21 -050068 static Archive_Type Get_File_Type(string fn); // Determines file type, 0 for unknown, 1 for gzip, 2 for OAES encrypted
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060069 static int Try_Decrypting_File(string fn, string password); // -1 for some error, 0 for failed to decrypt, 1 for decrypted, 3 for decrypted and found gzip format
bigbiffee7b7ff2020-03-23 15:08:27 -040070 static unsigned long Get_File_Size(const string& Path); // Returns the size of a file
bigbiffad58e1b2020-07-06 20:24:34 -040071 static std::string Remove_Beginning_Slash(const std::string& path); // Remove the beginning slash of a path
Vojtech Bocek05f87d62014-03-11 22:08:23 +010072 static std::string Remove_Trailing_Slashes(const std::string& path, bool leaveLast = false); // Normalizes the path, e.g /data//media/ -> /data/media
Matt Mower2416a502016-04-12 19:54:46 -050073 static void Strip_Quotes(char* &str); // Remove leading & trailing double-quotes from a string
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050074 static vector<string> split_string(const string &in, char del, bool skip_empty);
Ethan Yonker472f5062016-02-25 13:47:30 -060075 static timespec timespec_diff(timespec& start, timespec& end); // Return a diff for 2 times
76 static int32_t timespec_diff_ms(timespec& start, timespec& end); // Returns diff in ms
Adithya Ra327aa72021-12-19 00:49:54 +053077 static bool Wait_For_File(const string& path, std::chrono::nanoseconds timeout); // Wait For File, True is success, False is timeout;
Ethan Yonkeraf2897c2014-02-10 13:07:14 -060078
79#ifndef BUILD_TWRPTAR_MAIN
Dees_Troy6ef66352013-02-21 08:26:57 -060080 static int Recursive_Mkdir(string Path); // Recursively makes the entire path
Dees_Troy6ef66352013-02-21 08:26:57 -060081 static void GUI_Operation_Text(string Read_Value, string Default_Text); // Updates text for display in the GUI, e.g. Backing up %partition name%
82 static void GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text); // Same as above but includes partition name
Dees_Troy2673cec2013-04-02 20:22:16 +000083 static void Update_Log_File(void); // Writes the log to last_log
84 static void Update_Intent_File(string Intent); // Updates intent file
Dees_Troy6ef66352013-02-21 08:26:57 -060085 static int tw_reboot(RebootCommand command); // Prepares the device for rebooting
86 static void check_and_run_script(const char* script_file, const char* display_name); // checks for the existence of a script, chmods it to 755, then runs it
Dees_Troy6ef66352013-02-21 08:26:57 -060087 static int removeDir(const string path, bool removeParent); //recursively remove a directory
bigbiff22851b92021-09-01 16:46:57 -040088 static int copy_file(string src, string dst, int mode, bool mount_paths=true); //copy file from src to dst with mode permissions
Dees_Troy6ef66352013-02-21 08:26:57 -060089 static unsigned int Get_D_Type_From_Stat(string Path); // Returns a dirent dt_type value using stat instead of dirent
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050090 static int read_file(string fn, vector<string>& results); //read from file
Dees_Troy6ef66352013-02-21 08:26:57 -060091 static int read_file(string fn, string& results); //read from file
xNUTxe85f02d2014-07-18 01:30:58 +020092 static int read_file(string fn, uint64_t& results); //read from file
bigbiff22851b92021-09-01 16:46:57 -040093 static bool write_to_file(const string& fn, const string& line); //write single line to file with no newline
94 static bool write_to_file(const string& fn, const std::vector<string> lines); // write vector of strings line by line with newlines
Dees_Troy83bd4832013-05-04 12:39:56 +000095 static bool Try_Decrypting_Backup(string Restore_Path, string Password); // true for success, false for failed to decrypt
Ethan Yonkerb5557892014-02-07 21:43:20 -060096 static string System_Property_Get(string Prop_Name); // Returns value of Prop_Name from reading /system/build.prop
Captain Throwback1483a8e2022-01-03 09:52:06 -050097 static string Partition_Property_Get(string Prop_Name, TWPartitionManager &PartitionManager, string Mount_Point, string prop_file_name); // Returns value of Prop_Name from reading provided prop file
Dees Troyb21cc642013-09-10 17:36:41 +000098 static string Get_Current_Date(void); // Returns the current date in ccyy-m-dd--hh-nn-ss format
99 static void Auto_Generate_Backup_Name(); // Populates TW_BACKUP_NAME with a backup name based on current date and ro.build.display.id from /system/build.prop
nkk7198fc3992017-12-16 16:26:42 +0200100 static void Fixup_Time_On_Boot(const string& time_paths = ""); // Fixes time on devices which need it (time_paths is a space separated list of paths to check for ats_* files)
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100101 static std::vector<std::string> Split_String(const std::string& str, const std::string& delimiter, bool removeEmpty = true); // Splits string by delimiter
Vojtech Bocek03fd6c52014-03-13 18:46:34 +0100102 static bool Create_Dir_Recursive(const std::string& path, mode_t mode = 0755, uid_t uid = -1, gid_t gid = -1); // Create directory and it's parents, if they don't exist. mode, uid and gid are set to all _newly_ created folders. If whole path exists, do nothing.
xNUTxe85f02d2014-07-18 01:30:58 +0200103 static int Set_Brightness(std::string brightness_value); // Well, you can read, it does what it says, passing return int from TWFunc::Write_File ;)
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400104 static bool Toggle_MTP(bool enable); // Disables MTP if enable is false and re-enables MTP if enable is true and it was enabled the last time it was toggled off
Jenkins1710bf22014-10-02 20:22:21 -0400105 static std::string to_string(unsigned long value); //convert ul to string
Tom Hite5a926722014-09-15 01:31:03 +0000106 static void SetPerformanceMode(bool mode); // support recovery.perf.mode
Ethan Yonker9132d912015-02-02 10:32:49 -0600107 static void Disable_Stock_Recovery_Replace(); // Disable stock ROMs from replacing TWRP with stock recovery
Ethan Yonker483e9f42016-01-11 22:21:18 -0600108 static unsigned long long IOCTL_Get_Block_Size(const char* block_device);
bigbiff bigbiffbad332a2016-07-29 21:18:13 -0400109 static void copy_kernel_log(string curr_storage); // Copy Kernel Log to Current Storage (PSTORE/KMSG)
Captain Throwback52978932021-09-05 15:11:07 -0400110 static void copy_logcat(string curr_storage); // Copy Logcat to Current Storage
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400111 static bool isNumber(string strtocheck); // return true if number, false if not a number
112 static int stream_adb_backup(string &Restore_Name); // Tell ADB Backup to Stream to TWRP from GUI selection
bigbiff25d25b92020-06-19 16:07:38 -0400113 static std::string get_log_dir(); // return recovery log storage directory
bigbiff bigbiff19874f12019-01-08 20:06:57 -0500114 static void check_selinux_support(); // print whether selinux support is enabled to console
Ethan Yonker76bbd3a2019-05-10 10:50:04 -0500115 static bool Is_TWRP_App_In_System(); // Check if the TWRP app is installed in the system partition
epicX271bb3a2020-12-30 01:03:18 +0530116 static void checkforapp();
Chaosmaster461e39f2020-02-07 01:48:13 +0100117 static int Property_Override(string Prop_Name, string Prop_Value); // Override properties (including ro. properties)
Mohd Faraz446ccbf2023-01-24 19:58:24 +0100118 static int Delete_Property(string Prop_Name); // Delete properties (non-persistent properties only)
bigbiff22851b92021-09-01 16:46:57 -0400119 static void List_Mounts(); // List current mounts by the kernel
120 static void Clear_Bootloader_Message(); // Removes the bootloader message from misc for next boot
121 static string Check_For_TwrpFolder(); // Gets user defined path on storage where backups should be stored
Darth98f775972022-08-05 09:39:03 +0100122 static bool Check_Xml_Format(const std::string filename); // Return whether a xml is in plain xml or ABX format
Mohd Faraz446ccbf2023-01-24 19:58:24 +0100123 static bool Find_Fstab(string &fstab);
Mohd Faraza57874b2023-09-30 00:57:58 +0530124 static bool Get_Service_From_Manifest(std::string basepath, std::string service, std::string &ret);
Tom Hite5a926722014-09-15 01:31:03 +0000125
Darth98f775972022-08-05 09:39:03 +0100126 static bool abx_to_xml(const std::string path, std::string &result); // could we convert abx to xml (if so, return the full path to the converted file)
Dees_Troy6ef66352013-02-21 08:26:57 -0600127private:
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 static void Copy_Log(string Source, string Destination);
Dees_Troy6ef66352013-02-21 08:26:57 -0600129
130};
131
Dees_Troy2673cec2013-04-02 20:22:16 +0000132extern int Log_Offset;
Ethan Yonkeraf2897c2014-02-10 13:07:14 -0600133#else
134};
135#endif // ndef BUILD_TWRPTAR_MAIN
Dees_Troy2673cec2013-04-02 20:22:16 +0000136
Dees_Troy6ef66352013-02-21 08:26:57 -0600137#endif // _TWRPFUNCTIONS_HPP