This adds a 60 second screen timeout for TWRP. Might consider making this configurable in the future.
Will also set overlay to lockscreen so we don't have inadvetent screen selections.
Touching the screen will bring the display back up.
add back check script for poweroff
move diff time function to twrp-functions.cpp
make sure we chmod after copy_file
add read_file and write_file functions to twrp-functions.cpp
make single thread
try to force update screen
add forceRender
drop caches after tar processing

Change-Id: I3c5c509dd39dbb05451bbfe5d8b56d53c90d8d1b
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index fa2110a..85ce727 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 @@
         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 @@
 	dstfile << srcfile.rdbuf();
 	srcfile.close();
 	dstfile.close();
+	if (chmod(dst.c_str(), mode) != 0)
+		return -1;
 	return 0;
 }
 
@@ -472,3 +476,48 @@
 		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;
+}