diff options
Diffstat (limited to 'twrp-functions.cpp')
-rw-r--r-- | twrp-functions.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/twrp-functions.cpp b/twrp-functions.cpp index fa2110aa0..85ce7274a 100644 --- a/twrp-functions.cpp +++ b/twrp-functions.cpp @@ -12,6 +12,7 @@ #include <sys/sendfile.h> #include <sys/stat.h> #include <sys/vfs.h> +#include "cutils/android_reboot.h" #include <iostream> #include <fstream> #include "twrp-functions.hpp" @@ -374,6 +375,7 @@ int TWFunc::tw_reboot(RebootCommand command) return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader"); case rb_poweroff: check_and_run_script("/sbin/poweroff.sh", "power off"); + android_reboot(ANDROID_RB_POWEROFF, 0, 0); return reboot(RB_POWER_OFF); case rb_download: check_and_run_script("/sbin/rebootdownload.sh", "reboot download"); @@ -449,6 +451,8 @@ int TWFunc::copy_file(string src, string dst, int mode) { dstfile << srcfile.rdbuf(); srcfile.close(); dstfile.close(); + if (chmod(dst.c_str(), mode) != 0) + return -1; return 0; } @@ -472,3 +476,48 @@ unsigned int TWFunc::Get_D_Type_From_Stat(string Path) { return DT_SOCK; return DT_UNKNOWN; } + +int TWFunc::read_file(string fn, string& results) { + ifstream file; + file.open(fn.c_str(), ios::in); + if (file.is_open()) { + file >> results; + file.close(); + return 0; + } + LOGI("Cannot find file %s\n", fn.c_str()); + return -1; +} + +int TWFunc::write_file(string fn, string& line) { + FILE *file; + file = fopen(fn.c_str(), "w"); + if (file != NULL) { + fwrite(line.c_str(), line.size(), 1, file); + fclose(file); + return 0; + } + LOGI("Cannot find file %s\n", fn.c_str()); + return -1; +} + +timespec TWFunc::timespec_diff(timespec& start, timespec& end) +{ + timespec temp; + if ((end.tv_nsec-start.tv_nsec)<0) { + temp.tv_sec = end.tv_sec-start.tv_sec-1; + temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; + } else { + temp.tv_sec = end.tv_sec-start.tv_sec; + temp.tv_nsec = end.tv_nsec-start.tv_nsec; + } + return temp; +} + + int TWFunc::drop_caches(void) { + string file = "/proc/sys/vm/drop_caches"; + string value = "3"; + if (write_file(file, value) != 0) + return -1; + return 0; +} |