/* Made my own mount, because aosp api "ensure_path_mounted" relies on recovery.fstab
** and we needed one that can mount based on what the file system really is (from blkid), 
** and not what's in fstab or recovery.fstab
*/
int tw_mount(struct dInfo mMnt)
{
	if (strcmp(mMnt.mnt,"system") == 0 || strcmp(mMnt.mnt,"data") == 0 ||
		strcmp(mMnt.mnt,"cache") == 0 || strcmp(mMnt.mnt,"sd-ext") == 0 || 
		strcmp(mMnt.mnt,"efs") == 0) { // if any of the mount points match these
		FILE *fp;
		char mCommand[255];
		char mOutput[50];
		LOGI("=> Checking if /%s is mounted.\n",mMnt.mnt); // check it mounted
		sprintf(mCommand,"cat /proc/mounts | grep %s | awk '{ print $1 }'",mMnt.blk); // by checking for block in proc mounts
		fp = __popen(mCommand, "r"); // run above command
		if (fscanf(fp,"%s",mOutput) != 1) { // if we get a match
			__pclose(fp);
			LOGI("=> /%s is not mounted. Mounting...\n",mMnt.mnt);
			sprintf(mCommand,"mount -t %s %s /%s",mMnt.fst,mMnt.blk,mMnt.mnt); // mount using filesystem stored in struct mMnt.fst
			fp = __popen(mCommand, "r");
			fgets(mOutput,sizeof(mOutput),fp); // get output
			__pclose(fp);
			if (mOutput[0] == 'm') { // if output starts with m, it's an error
				ui_print("-- Error: %s",mOutput);
				return 1;
			} else {
				LOGI("=> Mounted /%s.\n",mMnt.mnt); // output should be nothing, so it's succesful
			}
		} else {
			__pclose(fp);
			LOGI("=> /%s is already mounted.\n",mMnt.mnt);
		}
	}
	return 0;
}
int tw_unmount(struct dInfo uMnt)
{
	if (strcmp(uMnt.mnt,"system") == 0 || strcmp(uMnt.mnt,"data") == 0 ||
		strcmp(uMnt.mnt,"cache") == 0 || strcmp(uMnt.mnt,"sd-ext") == 0 || 
		strcmp(uMnt.mnt,"efs") == 0) {
		FILE *fp;
		char uCommand[255];
		char uOutput[50];
		char exe[50];
		LOGI("=> Checking if /%s is mounted.\n",uMnt.mnt);
		sprintf(uCommand,"cat /proc/mounts | grep %s | awk '{ print $1 }'",uMnt.blk);
		fp = __popen(uCommand, "r");
		if (fscanf(fp,"%s",uOutput) == 1) {
			__pclose(fp);
			sprintf(exe,"umount /%s",uMnt.mnt);
			fp = __popen(exe, "r");
			fgets(uOutput,sizeof(uOutput),fp);
			__pclose(fp);
			if (uOutput[0] == 'u') {
				ui_print("-- Error: %s",uOutput);
				return 1;
			} else {
				LOGI("=> Unmounted /%s\n",uMnt.mnt);
			}
		} else {
			__pclose(fp);
			LOGI("=> /%s is not mounted.\n\n",uMnt.mnt);
		}
	}
	return 0;
}
int twrp_tar_extract_wrapper(const char* popen_command, const char* backup_path, int callback) {
    char tmp[PATH_MAX];

    strcpy(tmp, popen_command);
    set_perf_mode(1);
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        set_perf_mode(0);
        return -1;
    }

    int nand_starts = 1;
    last_size_update = 0;
    while (fgets(tmp, PATH_MAX, fp) != NULL) {
#ifdef PHILZ_TOUCH_RECOVERY
        if (user_cancel_nandroid(&fp, NULL, 0, &nand_starts)) {
            set_perf_mode(0);
            return -1;
        }
#endif
        tmp[PATH_MAX - 1] = '\0';
        if (callback) {
            update_size_progress(backup_path);
            nandroid_callback(tmp);
        }
    }

#ifdef PHILZ_TOUCH_RECOVERY
    ui_print_preset_colors(0, NULL);
#endif
    set_perf_mode(0);
    return __pclose(fp);
}
static int dedupe_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
    char tmp[PATH_MAX];
    char blob_dir[PATH_MAX];
    strcpy(blob_dir, backup_file_image);
    char *d = dirname(blob_dir);
    strcpy(blob_dir, d);
    d = dirname(blob_dir);
    strcpy(blob_dir, d);
    d = dirname(blob_dir);
    strcpy(blob_dir, d);
    strcat(blob_dir, "/blobs");
    ensure_directory(blob_dir);

    if (!(nandroid_backup_bitfield & NANDROID_FIELD_DEDUPE_CLEARED_SPACE)) {
        nandroid_backup_bitfield |= NANDROID_FIELD_DEDUPE_CLEARED_SPACE;
        nandroid_dedupe_gc(blob_dir);
    }

    sprintf(tmp, "dedupe c %s %s %s.dup %s", backup_path, blob_dir, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "./media" : "");

    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute dedupe.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
Exemple #5
0
int checkMD5(char *imgDir, const char *imgFile)
{
	int bool = 0;
	if (ensure_path_mounted("/sdcard") != 0) {
		LOGI("=> Can not mount sdcard.\n");
	} else {
		FILE *fp;
		char tmpString[255];
		char tmpAnswer[10];
		sprintf(tmpString,"cd %s && md5sum -c %s.md5",imgDir,imgFile);
		fp = __popen(tmpString, "r");
		if (fp == NULL)
		{
			LOGI("=> Can not open pipe.\n");
		} else {
			fgets(tmpString, 255, fp);
			sscanf(tmpString,"%*s %s",tmpAnswer);
			if (strcmp(tmpAnswer,"OK") == 0)
			{
				bool = 1;
				ui_print("....MD5 Check: %s\n", tmpAnswer);
			} else {
				ui_print("....MD5 Error: %s\n", tmpString);
			}
		}
static int dedupe_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
    char tmp[PATH_MAX];
    char blob_dir[PATH_MAX];
    strcpy(blob_dir, backup_file_image);
    char *bd = dirname(blob_dir);
    strcpy(blob_dir, bd);
    bd = dirname(blob_dir);
    strcpy(blob_dir, bd);
    bd = dirname(blob_dir);
    sprintf(tmp, "dedupe x %s %s/blobs %s; exit $?", backup_file_image, bd, backup_path);

    char path[PATH_MAX];
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute dedupe.\n");
        return -1;
    }

    while (fgets(path, PATH_MAX, fp) != NULL) {
        if (callback)
            nandroid_callback(path);
    }

    return __pclose(fp);
}
Exemple #7
0
void msg_shell_cmd(struct client *clnt)
{
	struct message *msg = (struct message *)(clnt->msg_buf);

	clnt->msg_size = __popen(msg->data, msg->data, DATA_SIZE);

	clnt->msg_size += sizeof(msg->type);
	msg->type++;
	
	send(clnt->fd, msg, clnt->msg_size, 0);
}
Exemple #8
0
void msg_chdir(struct client *clnt)
{
	struct message *msg = (struct message *)(clnt->msg_buf);
	
	chdir(msg->data + 3);
	
	clnt->msg_size = __popen("pwd", msg->data, DATA_SIZE);
	
	clnt->msg_size += sizeof(msg->type);
	msg->type++;	
	send(clnt->fd, msg, clnt->msg_size, 0);
}
/* Execute a command */
int TWFunc::Exec_Cmd(const string& cmd, string &result) {
	FILE* exec;
	char buffer[130];
	int ret = 0;
	exec = __popen(cmd.c_str(), "r");
	if (!exec) return -1;
	while(!feof(exec)) {
		if (fgets(buffer, 128, exec) != NULL) {
			result += buffer;
		}
	}
	ret = __pclose(exec);
	return ret;
}
Exemple #10
0
static int unyaffs_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd %s ; unyaffs %s ; exit $?", backup_path, backup_file_image);
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute unyaffs.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
static int do_tar_compress(char* command, int callback) {
    char buf[PATH_MAX];

    FILE *fp = __popen(command, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar command!\n");
        return -1;
    }

    while (fgets(buf, PATH_MAX, fp) != NULL) {
        buf[PATH_MAX - 1] = '\0';
        if (callback)
            nandroid_callback(buf);
    }

    return __pclose(fp);
}
static int do_tar_extract(char* command, int callback) {
    char buf[PATH_MAX];

    FILE *fp = __popen(command, "r");
    if (fp == NULL) {
        ui_print("不能正确执行tar命令.\n");
        return -1;
    }

    while (fgets(buf, PATH_MAX, fp) != NULL) {
        buf[PATH_MAX - 1] = '\0';
        if (callback)
            nandroid_callback(buf);
    }

    return __pclose(fp);
}
Exemple #13
0
static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd $(dirname %s) ; cat %s* | tar xv ; exit $?", backup_path, backup_file_image);
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
unsigned long long getUsedSizeViaDu(const char* path)
{
    char cmd[512];
    sprintf(cmd, "du -sk %s | awk '{ print $1 }'", path);

    FILE *fp;
    fp = __popen(cmd, "r");
    
    char str[512];
    fgets(str, sizeof(str), fp);
    __pclose(fp);

    unsigned long long size = atol(str);
    size *= 1024ULL;

    return size;
}
/* Execute a command */
int TWFunc::Exec_Cmd(string cmd, string &result) {
	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;
		}
	}
	ret = __pclose(exec);
	return ret;
}
static int tar_extract_wrapper(const char* backup_file_image, const char* backup_path, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd $(dirname %s) ; tar xvf %s ; exit $?", backup_path, backup_file_image);

    char path[PATH_MAX];
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        return -1;
    }

    while (fgets(path, PATH_MAX, fp) != NULL) {
        if (callback)
            yaffs_callback(path);
    }

    return __pclose(fp);
}
Exemple #17
0
static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd $(dirname %s) ; touch %s.tar ; (tar cv %s $(basename %s) | split -a 1 -b 1000000000 /proc/self/fd/0 %s.tar.) 2> /proc/self/fd/1 ; exit $?", backup_path, backup_file_image, strcmp(backup_path, "/data") == 0 && is_data_media() ? "--exclude 'media'" : "", backup_path, backup_file_image);

    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd %s ; mkyaffs2image . %s.img ; exit $?", backup_path, backup_file_image);

    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("运行或者解析mkyaffs2image失败.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
Exemple #19
0
static int tar_extract_wrapper_legacy(const char* backup_file_image, const char* backup_path, int callback) {
    char tmp[PATH_MAX];
    // All subdirs are included in the tar archive
    sprintf(tmp, "cd / ; tar xvf %s ; exit $?", backup_file_image);

    char path[PATH_MAX];
    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        return -1;
    }

    while (fgets(path, PATH_MAX, fp) != NULL) {
        if (callback)
            yaffs_callback(path);
    }

    return __pclose(fp);
}
int getSizeViaDf(struct dInfo* mMnt)
{
	FILE* fp;
    char command[255], line[512];
    int ret = 0, include_block = 1;
	unsigned int min_len;

    min_len = strlen(mMnt->blk) + 2;
	sprintf(command, "df %s", mMnt->blk);
	fp = __popen(command, "r");
    if (fp == NULL)
        return -1;

    while (fgets(line, sizeof(line), fp) != NULL)
    {
        unsigned long blocks, used, available;
        char device[64];
        char tmpString[64];

        if (strncmp(line, "Filesystem", 10) == 0)
			continue;
		if (strlen(line) < min_len) {
			include_block = 0;
			continue;
		}
		if (include_block) {
			sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
		} else {
			// The device block string is so long that the df information is on the next line
			int space_count = 0;
			while (tmpString[space_count] == 32)
				space_count++;
			sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
		}
		
        // Adjust block size to byte size
        unsigned long long size = blocks * 1024ULL;
        sprintf(tmpString, "%s%s", tw_block, device);
        setLocationData(NULL, mMnt->blk, NULL, NULL, size);
    }
    fclose(fp);
    return 0;
}
Exemple #21
0
static int do_tar_extract(char* command, int callback) {
    char buf[PATH_MAX];

    set_perf_mode(1);
    FILE *fp = __popen(command, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar command.\n");
        set_perf_mode(0);
        return -1;
    }

    while (fgets(buf, PATH_MAX, fp) != NULL) {
        buf[PATH_MAX - 1] = '\0';
        if (callback)
            nandroid_callback(buf);
    }

    set_perf_mode(0);
    return __pclose(fp);
}
int TWFunc::Exec_Cmd_Show_Output(const string& cmd) {
	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;
			gui_print(buffer);
		}
	}
	ret = __pclose(exec);
	return ret;
}
Exemple #23
0
eFSType Utility::getDeviceFSType(string device, bool cached /* = true */)
{
    static map<string,string> cachedOutput;

    if (!cached || cachedOutput.size() == 0)
    {
        // This routine uses blkid
        FILE* fp = __popen("blkid", "r");
        if (!fp)                                    return fs_unknown;

        char blkOutput[256];
        while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
        {
            char* ptr = blkOutput;
            while (*ptr > 0 && *ptr != ':')     ptr++;
            if (*ptr == 0)  continue;
            *ptr = 0;

            ptr++;
            while (strlen(ptr) > 5)
            {
                if (memcmp(ptr, "TYPE=", 5) == 0)
                {
                    // Skip the open quote too
                    ptr += 6;
                    string type = ptr;
                    type = type.substr(0, type.size() - 2);

                    cachedOutput.insert(make_pair(blkOutput, type));
                    break;
                }
                ptr++;
            }
        }
        __pclose(fp);
    }

    map<string,string>::iterator iter = cachedOutput.find(device);
    if (iter == cachedOutput.end())   return fs_unknown;
    return getFsTypeFromStr(iter->second);
}
static int tar_compress_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
    char tmp[PATH_MAX];
    if (strcmp(backup_path, "/data") == 0 && volume_for_path("/sdcard") == NULL)
      sprintf(tmp, "cd $(dirname %s) ; tar cvf %s --exclude 'media' $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);
    else
      sprintf(tmp, "cd $(dirname %s) ; tar cvf %s $(basename %s) ; exit $?", backup_path, backup_file_image, backup_path);

    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
        ui_print("Unable to execute tar.\n");
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = NULL;
        if (callback)
            yaffs_callback(tmp);
    }

    return __pclose(fp);
}
Exemple #25
0
static int mkyaffs2image_wrapper(const char* backup_path, const char* backup_file_image, int callback) {
    char tmp[PATH_MAX];
    sprintf(tmp, "cd %s ; mkyaffs2image . %s.img ; exit $?", backup_path, backup_file_image);

    FILE *fp = __popen(tmp, "r");
    if (fp == NULL) {
#ifndef USE_CHINESE_FONT
        ui_print("Unable to execute mkyaffs2image.\n");
#else
        ui_print("无法执行 mkyaffs2image。\n");
#endif
        return -1;
    }

    while (fgets(tmp, PATH_MAX, fp) != NULL) {
        tmp[PATH_MAX - 1] = '\0';
        if (callback)
            nandroid_callback(tmp);
    }

    return __pclose(fp);
}
Exemple #26
0
static int do_tar_compress(char* command, int callback) {
    char buf[PATH_MAX];

    set_perf_mode(1);
    FILE *fp = __popen(command, "r");
    if (fp == NULL) {
#ifndef USE_CHINESE_FONT
        ui_print("Unable to execute tar command!\n");
#else
        ui_print("无法执行 tar 命令!\n");
#endif
        set_perf_mode(0);
        return -1;
    }

    while (fgets(buf, PATH_MAX, fp) != NULL) {
        buf[PATH_MAX - 1] = '\0';
        if (callback)
            nandroid_callback(buf);
    }

    set_perf_mode(0);
    return __pclose(fp);
}
/* New backup function
** Condensed all partitions into one function
*/
int tw_backup(struct dInfo bMnt, char *bDir)
{
	if (ensure_path_mounted(SDCARD_ROOT) != 0) {
		ui_print("-- Could not mount: %s.\n-- Aborting.\n",SDCARD_ROOT);
		return 1;
	}
	int bDiv;
	char bTarArg[10];
	if (DataManager_GetIntValue(TW_USE_COMPRESSION_VAR)) { // set compression or not
		strcpy(bTarArg,"-czvf");
		bDiv = 512;
	} else {
		strcpy(bTarArg,"-cvf");
		bDiv = 2048;
	}
	FILE *bFp;
	int bPartSize;
	char *bImage = malloc(sizeof(char)*50);
	char *bMount = malloc(sizeof(char)*50);
	char *bCommand = malloc(sizeof(char)*255);
	if (strcmp(bMnt.mnt,"system") == 0 || strcmp(bMnt.mnt,"data") == 0 || 
			strcmp(bMnt.mnt,"cache") == 0 || strcmp(bMnt.mnt,"sd-ext") == 0 || 
			strcmp(bMnt.mnt,"efs") == 0 || strcmp(bMnt.mnt,".android_secure") == 0) { // detect mountable partitions
		if (strcmp(bMnt.mnt,".android_secure") == 0) { // if it's android secure, add sdcard to prefix
			strcpy(bMount,"/sdcard/");
			strcat(bMount,bMnt.mnt);
			sprintf(bImage,"and-sec.%s.win",bMnt.fst); // set image name based on filesystem, should always be vfat for android_secure
		} else {
			strcpy(bMount,"/");
			strcat(bMount,bMnt.mnt);
			sprintf(bImage,"%s.%s.win",bMnt.mnt,bMnt.fst); // anything else that is mountable, will be partition.filesystem.win
			ui_print("\n-- Mounting %s, please wait...\n",bMount);
			if (tw_mount(bMnt)) {
				ui_print("-- Could not mount: %s\n-- Aborting.\n",bMount);
				free(bCommand);
				free(bMount);
				free(bImage);
				return 1;
			}
			ui_print("-- Done.\n\n",bMount);
		}
		sprintf(bCommand,"du -sk %s | awk '{ print $1 }'",bMount); // check for partition/folder size
		bFp = __popen(bCommand, "r");
		fscanf(bFp,"%d",&bPartSize);
		__pclose(bFp);
		sprintf(bCommand,"cd %s && tar %s %s%s ./*",bMount,bTarArg,bDir,bImage); // form backup command
	} else {
		strcpy(bMount,bMnt.mnt);
		bPartSize = bMnt.sze / 1024;
		sprintf(bImage,"%s.%s.win",bMnt.mnt,bMnt.fst); // non-mountable partitions such as boot/wimax/recovery
		if (strcmp(bMnt.fst,"mtd") == 0) {
			sprintf(bCommand,"dump_image %s %s%s",bMnt.mnt,bDir,bImage); // if it's mtd, we use dump image
		} else if (strcmp(bMnt.fst,"emmc") == 0) {
			sprintf(bCommand,"dd bs=%s if=%s of=%s%s",bs_size,bMnt.blk,bDir,bImage); // if it's emmc, use dd
		}
		ui_print("\n");
	}
	LOGI("=> Filename: %s\n",bImage);
	LOGI("=> Size of %s is %d KB.\n\n",bMount,bPartSize);
	int i;
	char bUppr[20];
	strcpy(bUppr,bMnt.mnt);
	for (i = 0; i < strlen(bUppr); i++) { // make uppercase of mount name
		bUppr[i] = toupper(bUppr[i]);
	}
	ui_print("[%s (%d MB)]\n",bUppr,bPartSize/1024); // show size in MB
	int bProgTime;
	time_t bStart, bStop;
	char bOutput[512];
	if (sdSpace > bPartSize) { // Do we have enough space on sdcard?
		time(&bStart); // start timer
		bProgTime = bPartSize / bDiv; // not very accurate but better than nothing progress time for progress bar
		ui_show_progress(1,bProgTime);
		ui_print("...Backing up %s partition.\n",bMount);
		bFp = __popen(bCommand, "r"); // sending backup command formed earlier above
		if(DataManager_GetIntValue(TW_SHOW_SPAM_VAR) == 2) { // if twrp spam is on, show all lines
			while (fgets(bOutput,sizeof(bOutput),bFp) != NULL) {
				ui_print_overwrite("%s",bOutput);
			}
		} else { // else just show single line
			while (fscanf(bFp,"%s",bOutput) == 1) {
				if(DataManager_GetIntValue(TW_SHOW_SPAM_VAR) == 1) ui_print_overwrite("%s",bOutput);
			}
		}
		ui_print_overwrite("....Done.\n");
		__pclose(bFp);
		int pFileSize;
		ui_print("...Double checking backup file size.\n");
		sprintf(bCommand,"ls -l %s%s | awk '{ print $5 }'",bDir,bImage); // double checking to make sure we backed up something
		bFp = __popen(bCommand, "r");
		fscanf(bFp,"%d",&pFileSize);
		__pclose(bFp);
		ui_print("....File size: %d bytes.\n",pFileSize); // file size
		if (pFileSize > 0) { // larger than 0 bytes?
			if (strcmp(bMnt.fst,"mtd") == 0 || strcmp(bMnt.fst,"emmc") == 0) { // if it's an unmountable partition, we can make sure
				LOGI("=> Expected size: %d Got: %d\n",bMnt.sze,pFileSize); // partition size matches file image size (they should match!)
				if (pFileSize != bMnt.sze) {
					ui_print("....File size is incorrect. Aborting...\n\n"); // they dont :(
					free(bCommand);
					free(bMount);
					free(bImage);
					return 1;
				} else {
					ui_print("....File size matches partition size.\n"); // they do, yay!
				}
			}
			ui_print("...Generating %s md5...\n", bMnt.mnt);
			makeMD5(bDir,bImage); // make md5 file
			ui_print("....Done.\n");
			ui_print("...Verifying %s md5...\n", bMnt.mnt);
			checkMD5(bDir,bImage); // test the md5 we just made, just in case
			ui_print("....Done.\n");
			time(&bStop); // stop timer
			ui_reset_progress(); // stop progress bar
			output_time(bUppr, "DONE", (int)difftime(bStop,bStart));
			tw_unmount(bMnt); // unmount partition we just restored to (if it's not a mountable partition, it will just bypass)
			sdSpace -= bPartSize; // minus from sd space number (not accurate but, it's more than the real count so it will suffice)
		} else {
			ui_print("...File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
			tw_unmount(bMnt);
			free(bCommand);
			free(bMount);
			free(bImage);
			return 1;
		}
	} else {
		ui_print("...Not enough space on /sdcard. Aborting.\n"); // oh noes! no space left on sdcard, abort! abort!
		tw_unmount(bMnt);
		free(bCommand);
		free(bMount);
		free(bImage);
		return 1;
	}
	free(bCommand);
	free(bMount);
	free(bImage);
	return 0;
}
void
set_restore_files()
{
	FILE *rfFp;
	char rfCommand[255];
	char rfOutput[20];
	tw_nan_system_x = -1; // set restore files as not there (default)
	tw_nan_data_x = -1;
	tw_nan_cache_x = -1;
	tw_nan_recovery_x = -1;
	tw_nan_wimax_x = -1;
	tw_nan_boot_x = -1;
	tw_nan_andsec_x = -1;
	tw_nan_sdext_x = -1;
	sprintf(rfCommand,"cd %s && ls -l *.win | awk '{ print $9 }'",nan_dir); // form scan for *.win files command
	rfFp = __popen(rfCommand, "r"); // open pipe with scan command
	while (fscanf(rfFp,"%s",rfOutput) == 1) { // while there is output
		if (strcmp(rfOutput,"system.yaffs2.win") == 0 ||
			strcmp(rfOutput,"system.ext2.win") == 0 ||
			strcmp(rfOutput,"system.ext3.win") == 0 ||
			strcmp(rfOutput,"system.ext4.win") == 0) { // if output matches any of these filenames
			strcpy(sys.fnm,rfOutput); // copy the filename and store in struct .fnm
			tw_nan_system_x = 1; // set restore option for system as visible
			tw_total++; // add to restore count
		}
		if (strcmp(rfOutput,"data.yaffs2.win") == 0 ||
			strcmp(rfOutput,"data.ext2.win") == 0 ||
			strcmp(rfOutput,"data.ext3.win") == 0 ||
			strcmp(rfOutput,"data.ext4.win") == 0) {
			strcpy(dat.fnm,rfOutput);
			tw_nan_data_x = 1;
			tw_total++;
		}
		if (strcmp(rfOutput,"cache.yaffs2.win") == 0 ||
			strcmp(rfOutput,"cache.ext2.win") == 0 ||
			strcmp(rfOutput,"cache.ext3.win") == 0 ||
			strcmp(rfOutput,"cache.ext4.win") == 0) {
			strcpy(cac.fnm,rfOutput);
			tw_nan_cache_x = 1;
			tw_total++;
		}
		if (strcmp(rfOutput,"sd-ext.ext2.win") == 0 ||
			strcmp(rfOutput,"sd-ext.ext3.win") == 0 ||
			strcmp(rfOutput,"sd-ext.ext4.win") == 0) {
			strcpy(sde.fnm,rfOutput);
			tw_nan_sdext_x = 1;
			tw_total++;
		}
		if (strcmp(rfOutput,"and-sec.vfat.win") == 0) {
			strcpy(ase.fnm,rfOutput);
			tw_nan_andsec_x = 1;
			tw_total++;
		}
		if (strcmp(rfOutput,"boot.mtd.win") == 0 ||
			strcmp(rfOutput,"boot.emmc.win") == 0) {
			strcpy(boo.fnm,rfOutput);
			tw_nan_boot_x = 1;
			tw_total++;
		}
		if (strcmp(rfOutput,"recovery.mtd.win") == 0 ||
			strcmp(rfOutput,"recovery.emmc.win") == 0) {
			strcpy(rec.fnm,rfOutput);
			tw_nan_recovery_x = 1;
			tw_total++;
		}
		if (strcmp(wim.mnt,"wimax") == 0 || strcmp(wim.mnt,"efs") == 0) {
			if (strcmp(rfOutput,"wimax.mtd.win") == 0 ||
				strcmp(rfOutput,"wimax.emmc.win") == 0) {
				strcpy(wim.fnm,rfOutput);
				tw_nan_wimax_x = 1;
				tw_total++;
			}
			if (strcmp(rfOutput,"efs.yaffs2.win") == 0) {
				strcpy(wim.fnm,rfOutput);
				tw_nan_wimax_x = 1;
				tw_total++;
			}
		}
	}
	__pclose(rfFp);
}
int
nandroid_back_exe()
{
	if (ensure_path_mounted(SDCARD_ROOT) != 0) {
		ui_print("-- Could not mount: %s.\n-- Aborting.\n",SDCARD_ROOT);
		return 1;
	}
    struct tm *t;
    char timestamp[15];
	char tw_image_dir[255];
	char exe[255];
	time_t start, stop;
	time_t seconds;
	seconds = time(0);
    t = localtime(&seconds);
    sprintf(timestamp,"%02d%02d%d%02d%02d%02d",t->tm_mon+1,t->tm_mday,t->tm_year+1900,t->tm_hour,t->tm_min,t->tm_sec); // make time stamp
	sprintf(tw_image_dir,"%s/%s/%s/",backup_folder,device_id,timestamp); // for backup folder
	sprintf(exe,"mkdir -p %s",tw_image_dir); // make the folder with timestamp
	if (__system(exe) != 0) {
		ui_print("-- Could not create: %s.\n-- Aborting.",tw_image_dir);
		return 1;
	} else {
		LOGI("=> Created folder: %s\n",tw_image_dir);
	}
	FILE *fp;
	char pOutput[25];
	int sdSpaceFinal;
	LOGI("=> Checking space on %s.\n",SDCARD_ROOT);
	fp = __popen("df -k /sdcard | grep sdcard | awk '{ print $4 \" \" $3 }'", "r"); // how much space left on sdcard?
	fgets(pOutput,25,fp);
	__pclose(fp);
	if(pOutput[2] == '%') { // oh o, crespo devices report diskspace on the 3rd argument.
		if (sscanf(pOutput,"%*s %d",&sdSpaceFinal) != 1) { // is it a number?
			ui_print("-- Could not determine free space on %s.",SDCARD_ROOT); // oh noes! Can't find sdcard's free space.
			return 1;
		}
	} else {
		if (sscanf(pOutput,"%d %*s",&sdSpaceFinal) != 1) { // is it a number?
			ui_print("-- Could not determine free space on %s.",SDCARD_ROOT); // oh noes! Can't find sdcard's free space.
			return 1;
		}
	}
	LOGI("=> %s",pOutput);
	sdSpace = sdSpaceFinal; // set starting and running count of sd space
	LOGI("=> /sdcard has %d MB free.\n",sdSpace/1024);
	time(&start);
	ui_print("\n[BACKUP STARTED]\n\n");
	ui_print("-- Verifying filesystems, please wait...\n");
	verifyFst();
	ui_print("-- Updating fstab.\n");
	createFstab();
	ui_print("-- Done.\n");
	// SYSTEM
	if (DataManager_GetIntValue(TW_NANDROID_SYSTEM_VAR)) { // was system backup enabled?
		if (tw_backup(sys,tw_image_dir) == 1) { // did the backup process return an error ? 0 = no error
			ui_print("-- Error occured, check recovery.log. Aborting.\n"); //oh noes! abort abort!
			return 1;
		}
	}
	// DATA
	if (DataManager_GetIntValue(TW_NANDROID_DATA_VAR)) {
		if (tw_backup(dat,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// BOOT
	if (DataManager_GetIntValue(TW_NANDROID_BOOT_VAR)) {
		if (tw_backup(boo,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// RECOVERY
	if (DataManager_GetIntValue(TW_NANDROID_RECOVERY_VAR)) {
		if (tw_backup(rec,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// CACHE
	if (DataManager_GetIntValue(TW_NANDROID_CACHE_VAR)) {
		if (tw_backup(cac,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// WIMAX
	if (DataManager_GetIntValue(TW_NANDROID_WIMAX_VAR)) {
		if (tw_backup(wim,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// ANDROID-SECURE
	if (DataManager_GetIntValue(TW_NANDROID_ANDSEC_VAR)) {
		if (tw_backup(ase,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	// SD-EXT
	if (DataManager_GetIntValue(TW_NANDROID_SDEXT_VAR)) {
		if (tw_backup(sde,tw_image_dir) == 1) {
			ui_print("-- Error occured, check recovery.log. Aborting.\n");
			return 1;
		}
	}
	LOGI("=> Checking /sdcard space again.\n\n");
	fp = __popen("df -k /sdcard| grep sdcard | awk '{ print $4 \" \" $3 }'", "r");
	fgets(pOutput,25,fp);
	__pclose(fp);
	if(pOutput[2] == '%') {
		if (sscanf(pOutput,"%*s %d",&sdSpace) != 1) { // is it a number?
			ui_print("-- Could not determine free space on %s.\n",SDCARD_ROOT); // oh noes! Can't find sdcard's free space.
			return 1;
		}
	} else {
		if (sscanf(pOutput,"%d %*s",&sdSpace) != 1) { // is it a number?
			ui_print("-- Could not determine free space on %s.\n",SDCARD_ROOT); // oh noes! Can't find sdcard's free space.
			return 1;
		}
	}
	time(&stop);
	ui_print("[%d MB TOTAL BACKED UP TO SDCARD]\n",(int)(sdSpaceFinal - sdSpace) / 1024);
	output_time("BACKUP", "COMPLETED", (int)difftime(stop, start));
	return 0;
}
int tw_restore(struct dInfo rMnt, char *rDir)
{
	int i;
	FILE *reFp;
	char rUppr[20];
	char rMount[30];
	char rFilesystem[10];
	char rFilename[255];
	char rCommand[255];
	char rOutput[255];
	time_t rStart, rStop;
	strcpy(rUppr,rMnt.mnt);
	for (i = 0; i < strlen(rUppr); i++) {
		rUppr[i] = toupper(rUppr[i]);
	}
	ui_print("[%s]\n",rUppr);
	ui_show_progress(1,150);
	time(&rStart);
	ui_print("...Verifying md5 hash for %s.\n",rMnt.mnt);
	if(checkMD5(rDir,rMnt.fnm) == 0) // verify md5, check if no error; 0 = no error.
	{
		strcpy(rFilename,rDir);
		strcat(rFilename,rMnt.fnm);
		sprintf(rCommand,"ls -l %s | awk -F'.' '{ print $2 }'",rFilename); // let's get the filesystem type from filename
		reFp = __popen(rCommand, "r");
		LOGI("=> Filename is: %s\n",rMnt.fnm);
		while (fscanf(reFp,"%s",rFilesystem) == 1) { // if we get a match, store filesystem type
			LOGI("=> Filesystem is: %s\n",rFilesystem); // show it off to the world!
		}
		__pclose(reFp);
		if (DataManager_GetIntValue(TW_RM_RF_VAR) == 1 && (strcmp(rMnt.mnt,"system") == 0 || strcmp(rMnt.mnt,"data") == 0 || strcmp(rMnt.mnt,"cache") == 0)) { // we'll use rm -rf instead of formatting for system, data and cache if the option is set
			ui_print("...using rm -rf to wipe %s\n", rMnt.mnt);
			tw_mount(rMnt); // mount the partition first
			sprintf(rCommand,"rm -rf %s%s/*", "/", rMnt.mnt);
			LOGI("rm rf commad: %s\n", rCommand);
			reFp = __popen(rCommand, "r");
			while (fscanf(reFp,"%s",rOutput) == 1) {
				ui_print_overwrite("%s",rOutput);
			}
			__pclose(reFp);
		} else {
			ui_print("...Formatting %s\n",rMnt.mnt);
			tw_format(rFilesystem,rMnt.blk); // let's format block, based on filesystem from filename above
		}
		ui_print("....Done.\n");
		if (strcmp(rFilesystem,"mtd") == 0) { // if filesystem is mtd, we use flash image
			sprintf(rCommand,"flash_image %s %s",rMnt.mnt,rFilename);
			strcpy(rMount,rMnt.mnt);
		} else if (strcmp(rFilesystem,"emmc") == 0) { // if filesystem is emmc, we use dd
			sprintf(rCommand,"dd bs=%s if=%s of=%s",bs_size,rFilename,rMnt.dev);
			strcpy(rMount,rMnt.mnt);
		} else {
			tw_mount(rMnt);
			strcpy(rMount,"/");
			if (strcmp(rMnt.mnt,".android_secure") == 0) { // if it's android_secure, we have add prefix
				strcat(rMount,"sdcard/");
			}
			strcat(rMount,rMnt.mnt);
			sprintf(rCommand,"cd %s && tar -xvf %s",rMount,rFilename); // formulate shell command to restore
		}
		ui_print("...Restoring %s\n",rMount);
		reFp = __popen(rCommand, "r");
		if(DataManager_GetIntValue(TW_SHOW_SPAM_VAR) == 2) { // twrp spam
			while (fgets(rOutput,sizeof(rOutput),reFp) != NULL) {
				ui_print_overwrite("%s",rOutput);
			}
		} else {
			while (fscanf(reFp,"%s",rOutput) == 1) {
				if(DataManager_GetIntValue(TW_SHOW_SPAM_VAR) == 1) ui_print_overwrite("%s",rOutput);
			}
		}
		__pclose(reFp);
		ui_print_overwrite("....Done.\n");
		if (strcmp(rMnt.mnt,".android_secure") != 0) { // any partition other than android secure,
			tw_unmount(rMnt); // let's unmount (unmountable partitions won't matter)
		}
	} else {
		ui_print("...Failed md5 check. Aborted.\n\n");
		return 1;
	}
	ui_reset_progress();
	time(&rStop);
	output_time(rUppr, "DONE", (int)difftime(rStop,rStart));
	return 0;
}