summaryrefslogtreecommitdiffstats
path: root/twrp-functions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'twrp-functions.cpp')
-rw-r--r--twrp-functions.cpp89
1 files changed, 30 insertions, 59 deletions
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index 463522664..e54e760ab 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -36,52 +36,21 @@ extern "C" {
/* Execute a command */
int TWFunc::Exec_Cmd(string cmd, string &result) {
- int fd[2];
- int ret = -1;
- if(pipe(fd) < 0)
- return -1;
-
- pid_t pid = fork();
- if (pid < 0)
- {
- close(fd[0]);
- close(fd[1]);
- return -1;
- }
-
- if(pid == 0) // child
- {
- close(fd[0]);
- dup2(fd[1], 1); // send stdout to the pipe
- dup2(fd[1], 2); // send stderr to the pipe
- close(fd[1]);
-
- ret = system(cmd.c_str());
- if(ret != -1)
- ret = WEXITSTATUS(ret);
- else
- LOGERR("Exec_Cmd: system() failed with -1 (%d)!\n", errno);
- exit(ret);
- }
- else
- {
- close(fd[1]);
-
- int len;
- char buffer[128];
- buffer[sizeof(buffer)-1] = 0;
- while ((len = read(fd[0], buffer, sizeof(buffer)-1)) > 0)
- {
- buffer[len] = 0;
- buffer[sizeof(buffer)-2] = '\n';
- LOGINFO("%s", buffer);
+ FILE* exec;
+ char buffer[130];
+ int ret = 0;
+ exec = __popen(cmd.c_str(), "r");
+ if (!exec) return -1;
+ while(!feof(exec)) {
+ memset(&buffer, 0, sizeof(buffer));
+ if (fgets(buffer, 128, exec) != NULL) {
+ buffer[128] = '\n';
+ buffer[129] = NULL;
result += buffer;
}
-
- waitpid(pid, &ret, 0);
- return WEXITSTATUS(ret);
}
- return -1;
+ ret = __pclose(exec);
+ return ret;
}
// Returns "file.name" from a full /path/to/file.name
@@ -216,7 +185,7 @@ unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Erro
while ((de = readdir(d)) != NULL)
{
- if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
+ if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, "lost+found") != 0)
{
dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error);
dusize += dutemp;
@@ -455,15 +424,17 @@ unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
}
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;
+ ifstream file;
+ file.open(fn.c_str(), ios::in);
+
+ if (file.is_open()) {
+ file >> results;
+ file.close();
+ return 0;
}
- LOGINFO("Cannot find file %s\n", fn.c_str());
- return -1;
+
+ LOGINFO("Cannot find file %s\n", fn.c_str());
+ return -1;
}
int TWFunc::read_file(string fn, vector<string>& results) {
@@ -529,7 +500,7 @@ timespec TWFunc::timespec_diff(timespec& start, timespec& end)
return temp;
}
- int TWFunc::drop_caches(void) {
+int TWFunc::drop_caches(void) {
string file = "/proc/sys/vm/drop_caches";
string value = "3";
if (write_file(file, value) != 0)
@@ -620,7 +591,8 @@ bool TWFunc::Fix_su_Perms(void) {
int TWFunc::tw_chmod(string fn, string mode) {
long mask = 0;
- for ( std::string::size_type n = 0; n < mode.length(); ++n) {
+ std::string::size_type n = (mode.length() == 3) ? 1 : 0;
+ for (; n < mode.length(); ++n) {
if (n == 0) {
if (mode[n] == '0')
continue;
@@ -744,7 +716,7 @@ int TWFunc::Get_File_Type(string fn) {
string::size_type i = 0;
int firstbyte = 0, secondbyte = 0;
char header[3];
-
+
ifstream f;
f.open(fn.c_str(), ios::in | ios::binary);
f.get(header, 3);
@@ -752,13 +724,12 @@ int TWFunc::Get_File_Type(string fn) {
firstbyte = header[i] & 0xff;
secondbyte = header[++i] & 0xff;
- if (firstbyte == 0x1f && secondbyte == 0x8b) {
+ if (firstbyte == 0x1f && secondbyte == 0x8b)
return 1; // Compressed
- } else if (firstbyte == 0x4f && secondbyte == 0x41) {
+ else if (firstbyte == 0x4f && secondbyte == 0x41)
return 2; // Encrypted
- } else {
+ else
return 0; // Unknown
- }
return 0;
}