Skip to content

Commit

Permalink
fix bug that nandroid_advanced_restore did not work ,since no md5 fil…
Browse files Browse the repository at this point in the history
…e exist, and bacup to backup operation , file_scan is not used
  • Loading branch information
yanhao committed Oct 26, 2012
1 parent 8ceeda9 commit 9b3a3bc
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 17 deletions.
122 changes: 111 additions & 11 deletions src/miui/src/main/backup_ui.c
Expand Up @@ -53,16 +53,116 @@ static STATUS backup_restore(char* path)
return RET_OK;
}

int file_backup(char *file_name, int file_len, void *data)
static STATUS _backup_dir_show(char *path)
{
return_val_if_fail(file_name != NULL, RET_FAIL);
return_val_if_fail(strlen(file_name) <= file_len, RET_INVALID_ARG);
/*
*nandroid_restore(backup_path, restore_boot, system, data, chache , sdext, wimax)
*/
backup_restore(file_name);
//to up layer
return -1;
DIR* d;
struct dirent* de;
d = opendir(path);
return_val_if_fail(d != NULL, RET_FAIL);

int d_size = 0;
int d_alloc = 10;
return_val_if_fail(backup_menu != NULL, RET_FAIL);
char** dirs = malloc(d_alloc * sizeof(char*));
char** dirs_desc = malloc(d_alloc * sizeof(char*));
return_val_if_fail(dirs != NULL, RET_FAIL);
return_val_if_fail(dirs_desc != NULL, RET_FAIL);
int z_size = 1;
int z_alloc = 10;
char** zips = malloc(z_alloc * sizeof(char*));
char** zips_desc=malloc(z_alloc * sizeof(char*));
return_val_if_fail(zips != NULL, RET_FAIL);
return_val_if_fail(zips_desc != NULL, RET_FAIL);
zips[0] = strdup("../");
zips_desc[0]=strdup("../");

while ((de = readdir(d)) != NULL) {
int name_len = strlen(de->d_name);
char de_path[PATH_MAX];
snprintf(de_path, PATH_MAX, "%s/%s", path, de->d_name);
struct stat st ;
assert_if_fail(stat(de_path, &st) == 0);
if (de->d_type == DT_DIR) {
//skip "." and ".." entries
if (name_len == 1 && de->d_name[0] == '.') continue;
if (name_len == 2 && de->d_name[0] == '.' &&
de->d_name[1] == '.') continue;
if (d_size >= d_alloc) {
d_alloc *= 2;
dirs = realloc(dirs, d_alloc * sizeof(char*));
dirs_desc = realloc(dirs_desc, d_alloc * sizeof(char*));
}
dirs[d_size] = malloc(name_len + 2);
dirs_desc[d_size] = malloc(64);
strcpy(dirs[d_size], de->d_name);
dirs[d_size][name_len ] = '\0';
snprintf(dirs_desc[d_size], 64, "%s" ,ctime(&st.st_mtime));
++d_size;
} else if (de->d_type == DT_REG && name_len >= 4 &&
strncasecmp(de->d_name + (name_len - 4), ".zip", 4) == 0) {
if (z_size >= z_alloc) {
z_alloc *= 2;
zips = realloc(zips, z_alloc * sizeof(char*));
zips_desc = realloc(zips_desc, z_alloc * sizeof(char*));
}
zips[z_size] = strdup(de->d_name);
zips_desc[z_size] = malloc(64);
snprintf(zips_desc[z_size], 64, "%s %lldbytes" ,ctime(&st.st_mtime), st.st_size);
z_size++;
}
}
closedir(d);


// append dirs to the zips list
if (d_size + z_size + 1 > z_alloc) {
z_alloc = d_size + z_size + 1;
zips = realloc(zips, z_alloc * sizeof(char*));
zips_desc = realloc(zips_desc, z_alloc * sizeof(char*));
}
memcpy(zips + z_size, dirs, d_size * sizeof(char *));
memcpy(zips_desc + z_size, dirs_desc, d_size * sizeof(char*));
free(dirs);
z_size += d_size;
zips[z_size] = NULL;
zips_desc[z_size] = NULL;

int result;
int chosen_item = 0;
do {
chosen_item = miui_sdmenu(backup_menu->name, zips, zips_desc, z_size);
return_val_if_fail(chosen_item >= 0, RET_FAIL);
char * item = zips[chosen_item];
int item_len = strlen(item);
if ( chosen_item == 0) {
//go up but continue browsing
result = -1;
break;
} else {
// select a zipfile
// the status to the caller
char new_path[PATH_MAX];
strlcpy(new_path, path, PATH_MAX);
strlcat(new_path, "/", PATH_MAX);
strlcat(new_path, item, PATH_MAX);
/*
*nandroid_restore(backup_path, restore_boot, system, data, chache , sdext, wimax)
*/
if (p_current != NULL && RET_YES == miui_confirm(3, p_current->name, p_current->desc, p_current->icon)) {
backup_restore(new_path);
}
break;
}
} while(1);

int i;
for (i = 0; i < z_size; ++i)
{
free(zips[i]);
free(zips_desc[i]);
}
free(zips);
return result;
}

static STATUS restore_child_show(menuUnit* p)
Expand Down Expand Up @@ -91,7 +191,7 @@ static STATUS restore_child_show(menuUnit* p)
miui_error("p->resulte %d should not be the value\n", p->result);
return MENU_BACK;
}
file_scan(path_name, PATH_MAX, p->name, strlen(p->name), &file_backup, NULL, NULL);
_backup_dir_show(path_name);
return MENU_BACK;
}

Expand All @@ -101,7 +201,7 @@ static STATUS backup_child_show(menuUnit* p)
miuiIntent_send(INTENT_MOUNT, 1, "/sdcard");
char path_name[PATH_MAX];
static time_t timep;
static struct tm *time_tm = NULL;
static struct tm *time_tm;
time(&timep);
time_tm = gmtime(&timep);
return_val_if_fail(miuiIntent_result_get_int() == 0, MENU_BACK);
Expand Down
53 changes: 49 additions & 4 deletions src/nandroid.c
Expand Up @@ -190,7 +190,7 @@ static nandroid_backup_handler get_backup_handler(const char *backup_path) {
}


int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
static int nandroid_backup_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
int ret = 0;
char* name = basename(mount_point);

Expand Down Expand Up @@ -229,7 +229,7 @@ int nandroid_backup_partition_extended(const char* backup_path, const char* moun
return 0;
}

int nandroid_backup_partition(const char* backup_path, const char* root) {
static int nandroid_backup_partition(const char* backup_path, const char* root) {
Volume *vol = volume_for_path(root);
// make sure the volume exists before attempting anything...
if (vol == NULL || vol->fs_type == NULL)
Expand All @@ -256,6 +256,51 @@ int nandroid_backup_partition(const char* backup_path, const char* root) {
return nandroid_backup_partition_extended(backup_path, root, 1);
}

int nandroid_advanced_backup(const char* backup_path, const char *root)
{
if (ensure_path_mounted(backup_path) != 0) {
return print_and_error("Can't mount backup path.\n");
}

Volume* volume = volume_for_path(backup_path);
if (NULL == volume) {
if (strstr(backup_path, "/sdcard") == backup_path && is_data_media())
volume = volume_for_path("/data");
else
return print_and_error("Unable to find volume for backup path.\n");
}
int ret;
struct statfs s;
if (NULL != volume) {
if (0 != (ret = statfs(volume->mount_point, &s)))
return print_and_error("Unable to stat backup path.\n");
uint64_t bavail = s.f_bavail;
uint64_t bsize = s.f_bsize;
uint64_t sdcard_free = bavail * bsize;
uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
if (sdcard_free_mb < 150)
ui_print("There may not be enough free space to complete backup... continuing...\n");
}
char tmp[PATH_MAX];
sprintf(tmp, "mkdir -p %s", backup_path);
__system(tmp);

if (0 != (ret = nandroid_backup_partition(backup_path, root)))
return ret;
ui_print("Generating md5 sum...\n");
sprintf(tmp, "nandroid-md5.sh %s", backup_path);
if (0 != (ret = __system(tmp))) {
ui_print("Error while generating md5 sum!\n");
return ret;
}

sync();
ui_print("\nBackup complete!\n");
return 0;

}

int nandroid_backup(const char* backup_path)
{
ui_set_background(BACKGROUND_ICON_INSTALLING);
Expand Down Expand Up @@ -426,7 +471,7 @@ static nandroid_restore_handler get_restore_handler(const char *backup_path) {
return tar_extract_wrapper;
}

int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
static int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
int ret = 0;
char* name = basename(mount_point);

Expand Down Expand Up @@ -529,7 +574,7 @@ int nandroid_restore_partition_extended(const char* backup_path, const char* mou
return 0;
}

int nandroid_restore_partition(const char* backup_path, const char* root) {
static int nandroid_restore_partition(const char* backup_path, const char* root) {
Volume *vol = volume_for_path(root);
// make sure the volume exists...
if (vol == NULL || vol->fs_type == NULL)
Expand Down
2 changes: 1 addition & 1 deletion src/nandroid.h
Expand Up @@ -5,5 +5,5 @@ void nandroid_generate_timestamp_path(char* backup_path);
int nandroid_backup(const char* backup_path);
int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext, int restore_wimax);

int nandroid_backup_partition(const char* backup_path, const char* root) ;
int nandroid_advanced_backup(const char* backup_path, const char *root);
#endif
2 changes: 1 addition & 1 deletion src/recovery.c
Expand Up @@ -509,7 +509,7 @@ static intentResult* intent_advanced_backup(int argc, char* argv[])
{
return_intent_result_if_fail(argc == 2);
return_intent_result_if_fail(argv != NULL);
int result = nandroid_backup_partition(argv[0], argv[1]);
int result = nandroid_advanced_backup(argv[0], argv[1]);
assert_ui_if_fail(result == 0);
return miuiIntent_result_set(result, NULL);
}
Expand Down

0 comments on commit 9b3a3bc

Please sign in to comment.