Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { char* result = NULL; if (argc != 1) { return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); } char *path; if (ReadArgs(state, argv, 1, &path) < 0) { return NULL; } ui_print("Formatting %s...\n", path); if (0 != format_volume(path)) { free(path); return StringValue(strdup("")); } if (strcmp(path, "/data") == 0 && has_datadata()) { ui_print("Formatting /datadata...\n", path); if (0 != format_volume("/datadata")) { free(path); return StringValue(strdup("")); } if (0 != format_volume(get_android_secure_path())) { free(path); return StringValue(strdup("")); } } done: return StringValue(strdup(path)); }
int maybe_install_firmware_update(const char *send_intent) { if (update_data == NULL || update_length == 0) return 0; /* We destroy the cache partition to pass the update image to the * bootloader, so all we can really do afterwards is wipe cache and reboot. * Set up this instruction now, in case we're interrupted while writing. */ struct bootloader_message boot; memset(&boot, 0, sizeof(boot)); strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); strlcpy(boot.recovery, "recovery\n--wipe_cache\n", sizeof(boot.command)); if (send_intent != NULL) { strlcat(boot.recovery, "--send_intent=", sizeof(boot.recovery)); strlcat(boot.recovery, send_intent, sizeof(boot.recovery)); strlcat(boot.recovery, "\n", sizeof(boot.recovery)); } if (set_bootloader_message(&boot)) return -1; int width = 0, height = 0, bpp = 0; char *busy_image = ui_copy_image( BACKGROUND_ICON_FIRMWARE_INSTALLING, &width, &height, &bpp); char *fail_image = ui_copy_image( BACKGROUND_ICON_FIRMWARE_ERROR, &width, &height, &bpp); ui_print("Writing %s image...\n", update_type); if (write_update_for_bootloader( update_data, update_length, width, height, bpp, busy_image, fail_image)) { LOGE("Can't write %s image\n(%s)\n", update_type, strerror(errno)); format_volume("/cache"); // Attempt to clean cache up, at least. return -1; } free(busy_image); free(fail_image); /* The update image is fully written, so now we can instruct the bootloader * to install it. (After doing so, it will come back here, and we will * wipe the cache and reboot into the system.) */ snprintf(boot.command, sizeof(boot.command), "update-%s", update_type); if (set_bootloader_message(&boot)) { format_volume("/cache"); return -1; } reboot(RB_AUTOBOOT); // Can't reboot? WTF? LOGE("Can't reboot\n"); return -1; }
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) return 0; // see if we need a raw restore (mtd) char tmp[PATH_MAX]; if (strcmp(vol->fs_type, "mtd") == 0 || strcmp(vol->fs_type, "bml") == 0 || strcmp(vol->fs_type, "emmc") == 0) { int ret; const char* name = basename(root); ui_print("Erasing %s before restore...\n", name); if (0 != (ret = format_volume(root))) { ui_print("Error while erasing %s image!", name); return ret; } sprintf(tmp, "%s%s.img", backup_path, root); ui_print("Restoring %s image...\n", name); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) { ui_print("Error while flashing %s image!", name); return ret; } return 0; } return nandroid_restore_partition_extended(backup_path, root, 1); }
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) return 0; // see if we need a raw restore (mtd) char tmp[PATH_MAX]; if (strcmp(vol->fs_type, "mtd") == 0 || strcmp(vol->fs_type, "bml") == 0 || strcmp(vol->fs_type, "emmc") == 0) { int ret; const char* name = basename(root); ui_print("恢复之前先清除 %s \n", name); if (0 != (ret = format_volume(root))) { ui_print("清除 %s 镜像失败...", name); return ret; } if (strcmp(backup_path, "-") == 0) strcpy(tmp, backup_path); else sprintf(tmp, "%s%s.img", backup_path, root); ui_print("正在恢复 %s 镜像...\n", name); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) { ui_print("刷写 %s 镜像失败!", name); return ret; } return 0; } return nandroid_restore_partition_extended(backup_path, root, 1); }
//INTENT_WIPE ,format "/data" | "/cache" | "/system" "/sdcard" static intentResult* intent_format(int argc, char *argv[]) { return_intent_result_if_fail(argc == 1); return_intent_result_if_fail(argv != NULL); int result = format_volume(argv[0]); assert_ui_if_fail(result == 0); return miuiIntent_result_set(result, "ok"); }
Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { char* result = NULL; if (argc != 1) { return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); } char *path; if (ReadArgs(state, argv, 1, &path) < 0) { return NULL; } ui_print("Formatting %s...\n", path); if (0 != format_volume(path)) { free(path); return StringValue(strdup("")); } if (strcmp(path, "/data") == 0 && has_datadata()) { ui_print("Formatting /datadata...\n", path); if (0 != format_volume("/datadata")) { free(path); return StringValue(strdup("")); } ensure_path_mounted("/sdcard"); ensure_path_mounted("/external_sd"); if( access( "/sdcard/clockworkmod/.is_as_external", F_OK ) != -1) { if (0 != format_volume("/external_sd/.android_secure")) { free(path); return StringValue(strdup("")); } } else { if (0 != format_volume("/sdcard/.android_secure")) { free(path); return StringValue(strdup("")); } } } done: return StringValue(strdup(path)); }
int erase_volume(const char *volume) { ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); ui_print("%s %s...\n", edifyformatting, volume); if (strcmp(volume, "/cache") == 0) { // Any part of the log we'd copied to cache is now gone. // Reset the pointer so we copy from the beginning of the temp // log. tmplog_offset = 0; } // Finally run format_volume from within roots return format_volume(volume); }
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); char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); struct stat file_info; if (0 != (ret = statfs(tmp, &file_info))) { ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); return 0; } ensure_directory(mount_point); int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; ui_print("Restoring %s...\n", name); /* if (0 != (ret = ensure_root_path_unmounted(root))) { ui_print("Can't unmount %s!\n", mount_point); return ret; } */ if (0 != (ret = format_volume(mount_point))) { ui_print("Error while formatting %s!\n", mount_point); return ret; } if (0 != (ret = ensure_path_mounted(mount_point))) { ui_print("Can't mount %s!\n", mount_point); return ret; } nandroid_restore_handler restore_handler = get_restore_handler(mount_point); if (restore_handler == NULL) { ui_print("Error finding an appropriate restore handler.\n"); return -2; } if (0 != (ret = restore_handler(tmp, mount_point, callback))) { ui_print("Error while restoring %s!\n", mount_point); return ret; } if (umount_when_finished) { ensure_path_unmounted(mount_point); } return 0; }
static int erase_volume(const char *volume) { ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); ui_print("正在格式化 %s \n", volume); if (strcmp(volume, "/cache") == 0) { // Any part of the log we'd copied to cache is now gone. // Reset the pointer so we copy from the beginning of the temp // log. tmplog_offset = 0; } return format_volume(volume); }
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); char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); struct stat file_info; if (0 != (ret = statfs(tmp, &file_info))) { ui_print("%s.img没有发现. 放弃还原 %s.\n", name, mount_point); return 0; } ensure_directory(mount_point); unyaffs_callback callback = NULL; if (0 != stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info)) { callback = yaffs_callback; } ui_print("还原 %s...\n", name); /* if (0 != (ret = ensure_root_path_unmounted(root))) { ui_print("Can't unmount %s!\n", mount_point); return ret; } */ if (0 != (ret = format_volume(mount_point))) { ui_print("格式化时出错 %s!\n", mount_point); return ret; } if (0 != (ret = ensure_path_mounted(mount_point))) { ui_print("不能挂载 %s!\n", mount_point); return ret; } if (0 != (ret = unyaffs(tmp, mount_point, callback))) { ui_print("还原时出错 %s!\n", mount_point); return ret; } if (umount_when_finished) { ensure_path_unmounted(mount_point); } return 0; }
static int erase_volume(const char *volume) { ui->SetBackground(RecoveryUI::INSTALLING); ui->SetProgressType(RecoveryUI::INDETERMINATE); ui->Print("Formatting %s...\n", volume); ensure_path_unmounted(volume); if (strcmp(volume, "/cache") == 0) { // Any part of the log we'd copied to cache is now gone. // Reset the pointer so we copy from the beginning of the temp // log. tmplog_offset = 0; } return format_volume(volume); }
void device_toggle_truedualboot(void) { char confirm[PATH_MAX]; int enable = dualboot_is_tdb_enabled(); #ifndef PHILZ_TOUCH_RECOVERY ui_setMenuTextColor(MENU_TEXT_COLOR_RED); #endif sprintf(confirm, "Yes - %s TrueDualBoot", enable?"DISABLE":"ENABLE"); if (confirm_selection("This will WIPE DATA. Confirm?", confirm)) { // unmount /data if(ensure_path_unmounted("/data")!=0) { LOGE("Error unmounting /data!\n"); return; } // format /data ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); ui_print("Formatting /data...\n"); set_force_raw_format_enabled(1); if(format_volume("/data")!=0) { ui_print("Error formatting /data!\n"); ui_reset_progress(); return; } ui_reset_progress(); set_force_raw_format_enabled(0); ui_print("Done.\n"); // toggle dualboot_set_tdb_enabled(!enable); } #ifndef PHILZ_TOUCH_RECOVERY ui_setMenuTextColor(MENU_TEXT_COLOR); #endif return; }
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) { ui_print("Volume not found! Skipping restore of %s...\n", root); return 0; } // see if we need a raw restore (mtd) char tmp[PATH_MAX]; if (strcmp(vol->fs_type, "mtd") == 0 || strcmp(vol->fs_type, "bml") == 0 || strcmp(vol->fs_type, "emmc") == 0) { int ret; const char* name = basename(root); //fix partition could be formatted when no image to restore (exp: if md5 check disabled and empty backup folder) struct stat file_check; sprintf(tmp, "%s%s.img", backup_path, root); if (0 != stat(tmp, &file_check)) { ui_print("%s.img not found. Skipping restore of %s\n", name, root); return 0; } ui_print("Erasing %s before restore...\n", name); if (0 != (ret = format_volume(root))) { ui_print("Error while erasing %s image!\n", name); return ret; } //sprintf(tmp, "%s%s.img", backup_path, root); ui_print("Restoring %s image...\n", name); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) { ui_print("Error while flashing %s image!\n", name); return ret; } return 0; } return nandroid_restore_partition_extended(backup_path, root, 1); }
armor_inventory_preset( const std::string &color_in ) : color( color_in ) { append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->get_encumber() ); }, _( "ENCUMBRANCE" ) ); append_cell( [ this ]( const item_location & loc ) { return loc->get_storage() > 0 ? string_format( "<%s>%s</color>", color, format_volume( loc->get_storage() ) ) : std::string(); }, _( "STORAGE" ) ); append_cell( [ this ]( const item_location & loc ) { return string_format( "<%s>%d%%</color>", color, loc->get_coverage() ); }, _( "COVERAGE" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->get_warmth() ); }, _( "WARMTH" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->bash_resist() ); }, _( "BASH" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->cut_resist() ); }, _( "CUT" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->acid_resist() ); }, _( "ACID" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->fire_resist() ); }, _( "FIRE" ) ); append_cell( [ this ]( const item_location & loc ) { return get_number_string( loc->get_env_resist() ); }, _( "ENV" ) ); }
static int erase_volume(const char *volume) { ui->SetBackground(RecoveryUI::ERASING); ui->SetProgressType(RecoveryUI::INDETERMINATE); /* add by cjcheng start... */ if (strcmp(volume, "/cache") == 0) { ui->SetTipTitle(RecoveryUI::TIP_TITLE_WIPE_CACHE); } else if (strcmp(volume, "/data") == 0) { ui->SetTipTitle(RecoveryUI::TIP_TITLE_WIPE_DATA); } /* add by cjcheng end... */ ui->Print("Formatting %s...\n", volume); ensure_path_unmounted(volume); if (strcmp(volume, "/cache") == 0) { // Any part of the log we'd copied to cache is now gone. // Reset the pointer so we copy from the beginning of the temp // log. tmplog_offset = 0; } return format_volume(volume); }
int oem_erase_partition(int argc, char **argv) { int retval = -1; char mnt_point[MOUNT_POINT_SIZE] = ""; if (argc != 2) { /* Should not pass here ! */ error("oem erase called with wrong parameter!"); goto end; } if (get_mountpoint(argv[1], mnt_point)) goto end; print("CMD '%s %s'...\n", argv[0], mnt_point); print("ERASE step 1/2...\n"); retval = nuke_volume(mnt_point, BUFFER_SIZE); if (retval != 0) { error("format_volume failed: %s\n", mnt_point); goto end; } else { print("format_volume succeeds: %s\n", mnt_point); } print("ERASE step 2/2...\n"); retval = format_volume(mnt_point); if (retval != 0) { error("format_volume failed: %s\n", mnt_point); } else { print("format_volume succeeds: %s\n", mnt_point); } end: return retval; }
static int erase_volume(const char *volume) { bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); ui->SetBackground(RecoveryUI::ERASING); ui->SetProgressType(RecoveryUI::INDETERMINATE); saved_log_file* head = NULL; if (is_cache) { // If we're reformatting /cache, we load any // "/cache/recovery/last*" files into memory, so we can restore // them after the reformat. ensure_path_mounted(volume); DIR* d; struct dirent* de; d = opendir(CACHE_LOG_DIR); if (d) { char path[PATH_MAX]; strcpy(path, CACHE_LOG_DIR); strcat(path, "/"); int path_len = strlen(path); while ((de = readdir(d)) != NULL) { if (strncmp(de->d_name, "last", 4) == 0) { saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file)); strcpy(path+path_len, de->d_name); p->name = strdup(path); if (stat(path, &(p->st)) == 0) { // truncate files to 512kb if (p->st.st_size > (1 << 19)) { p->st.st_size = 1 << 19; } p->data = (unsigned char*) malloc(p->st.st_size); FILE* f = fopen(path, "rb"); fread(p->data, 1, p->st.st_size, f); fclose(f); p->next = head; head = p; } else { free(p); } } } closedir(d); } else { if (errno != ENOENT) { printf("opendir failed: %s\n", strerror(errno)); } } } ui->Print("Formatting %s...\n", volume); ensure_path_unmounted(volume); int result = format_volume(volume); if (is_cache) { while (head) { FILE* f = fopen_path(head->name, "wb"); if (f) { fwrite(head->data, 1, head->st.st_size, f); fclose(f); chmod(head->name, head->st.st_mode); chown(head->name, head->st.st_uid, head->st.st_gid); } free(head->name); free(head->data); saved_log_file* temp = head->next; free(head); head = temp; } // Any part of the log we'd copied to cache is now gone. // Reset the pointer so we copy from the beginning of the temp // log. tmplog_offset = 0; copy_logs(); } return result; }
void show_partition_menu() { static char* headers[] = { "Mounts and Storage Menu", "", NULL }; static MountMenuEntry* mount_menu = NULL; static FormatMenuEntry* format_menu = NULL; typedef char* string; int i, mountable_volumes, formatable_volumes; int num_volumes; Volume* device_volumes; num_volumes = get_num_volumes(); device_volumes = get_device_volumes(); string options[255]; if(!device_volumes) return; mountable_volumes = 0; formatable_volumes = 0; mount_menu = malloc(num_volumes * sizeof(MountMenuEntry)); format_menu = malloc(num_volumes * sizeof(FormatMenuEntry)); for (i = 0; i < num_volumes; ++i) { Volume* v = &device_volumes[i]; if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0) { if (strcmp("datamedia", v->fs_type) != 0) { sprintf(&mount_menu[mountable_volumes].mount, "mount %s", v->mount_point); sprintf(&mount_menu[mountable_volumes].unmount, "unmount %s", v->mount_point); mount_menu[mountable_volumes].v = &device_volumes[i]; ++mountable_volumes; } if (is_safe_to_format(v->mount_point)) { sprintf(&format_menu[formatable_volumes].txt, "format %s", v->mount_point); format_menu[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point)) { sprintf(&format_menu[formatable_volumes].txt, "format %s", v->mount_point); format_menu[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } static char* confirm_format = "Confirm format?"; static char* confirm = "Yes - Format"; char confirm_string[255]; for (;;) { for (i = 0; i < mountable_volumes; i++) { MountMenuEntry* e = &mount_menu[i]; Volume* v = e->v; if(is_path_mounted(v->mount_point)) options[i] = e->unmount; else options[i] = e->mount; } for (i = 0; i < formatable_volumes; i++) { FormatMenuEntry* e = &format_menu[i]; options[mountable_volumes+i] = e->txt; } if (!is_data_media()) { options[mountable_volumes + formatable_volumes] = "mount USB storage"; options[mountable_volumes + formatable_volumes + 1] = NULL; } else { options[mountable_volumes + formatable_volumes] = "format /data and /data/media (/sdcard)"; options[mountable_volumes + formatable_volumes + 1] = NULL; } int chosen_item = get_menu_selection(headers, &options, 0, 0); if (chosen_item == GO_BACK) break; if (chosen_item == (mountable_volumes+formatable_volumes)) { if (!is_data_media()) { show_mount_usb_storage_menu(); } else { if (!confirm_selection("format /data and /data/media (/sdcard)", confirm)) continue; ignore_data_media_workaround(1); ui_print("Formatting /data...\n"); if (0 != format_volume("/data")) ui_print("Error formatting /data!\n"); else ui_print("Done.\n"); ignore_data_media_workaround(0); } } else if (chosen_item < mountable_volumes) { MountMenuEntry* e = &mount_menu[chosen_item]; Volume* v = e->v; if (is_path_mounted(v->mount_point)) { if (0 != ensure_path_unmounted(v->mount_point)) ui_print("Error unmounting %s!\n", v->mount_point); } else { if (0 != ensure_path_mounted(v->mount_point)) ui_print("Error mounting %s!\n", v->mount_point); } } else if (chosen_item < (mountable_volumes + formatable_volumes)) { chosen_item = chosen_item - mountable_volumes; FormatMenuEntry* e = &format_menu[chosen_item]; Volume* v = e->v; sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format); if (!confirm_selection(confirm_string, confirm)) continue; ui_print("Formatting %s...\n", v->mount_point); if (0 != format_volume(v->mount_point)) ui_print("Error formatting %s!\n", v->mount_point); else ui_print("Done.\n"); } } free(mount_menu); free(format_menu); }
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) { ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); if (ensure_path_mounted(backup_path) != 0) return print_and_error("Can't mount backup path\n"); char tmp[PATH_MAX]; #ifdef PHILZ_TOUCH_RECOVERY if (enable_md5sum) #endif { ui_print("Checking MD5 sums...\n"); sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); if (0 != __system(tmp)) return print_and_error("MD5 mismatch!\n"); } int ret; if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) return ret; if (backup_recovery && 0 != (ret = nandroid_restore_partition(backup_path, "/recovery"))) return ret; struct stat s; Volume *vol = volume_for_path("/wimax"); if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) { char serialno[PROPERTY_VALUE_MAX]; serialno[0] = 0; property_get("ro.serialno", serialno, ""); sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); struct stat st; if (0 != stat(tmp, &st)) { ui_print("WARNING: WiMAX partition exists, but nandroid\n"); ui_print(" backup does not contain WiMAX image.\n"); ui_print(" You should create a new backup to\n"); ui_print(" protect your WiMAX keys.\n"); } else { ui_print("Erasing WiMAX before restore...\n"); if (0 != (ret = format_volume("/wimax"))) return print_and_error("Error while formatting wimax!\n"); ui_print("Restoring WiMAX image...\n"); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) return ret; } } // restore of raw efs image files (efs_time-stamp.img) is done elsewhere // as it needs to pass in a filename (instead of a folder) as backup_path // this could be done here since efs is processed alone, but must be done before md5 checksum! if (backup_efs == RESTORE_EFS_TAR && 0 != (ret = nandroid_restore_partition(backup_path, "/efs"))) return ret; if (backup_modem == RAW_IMG_FILE && 0 != (ret = nandroid_restore_partition(backup_path, "/modem"))) return ret; else if (backup_modem == RAW_BIN_FILE) { sprintf(tmp, "%s/modem.bin", backup_path); custom_restore_raw_handler(tmp, "/modem"); } if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) return ret; if (is_custom_backup && backup_preload) { if (0 != (ret = nandroid_restore_partition(backup_path, "/preload"))) { ui_print("Failed to restore /preload!\n"); return ret; } } else if (!is_custom_backup #ifdef PHILZ_TOUCH_RECOVERY && nandroid_add_preload #endif ) { if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/preload"))) { ui_print("Failed to restore preload! Try to disable it.\n"); ui_print("Skipping /preload...\n"); //return ret; } } if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) return ret; if (has_datadata()) { if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) return ret; } if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0))) return ret; if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0))) return ret; if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) return ret; sync(); ui_set_background(BACKGROUND_ICON_NONE); ui_reset_progress(); ui_print("\nRestore complete!\n"); return 0; }
void show_partition_menu() { static char* headers[] = { "分区挂载和U盘模式", "", NULL }; static MountMenuEntry* mount_menu = NULL; static FormatMenuEntry* format_menu = NULL; typedef char* string; int i, mountable_volumes, formatable_volumes; int num_volumes; Volume* device_volumes; num_volumes = get_num_volumes(); device_volumes = get_device_volumes(); string options[255]; if(!device_volumes) return; mountable_volumes = 0; formatable_volumes = 0; mount_menu = malloc(num_volumes * sizeof(MountMenuEntry)); format_menu = malloc(num_volumes * sizeof(FormatMenuEntry)); for (i = 0; i < num_volumes; ++i) { Volume* v = &device_volumes[i]; if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0) { if (strcmp("datamedia", v->fs_type) != 0) { sprintf(&mount_menu[mountable_volumes].mount, "挂载 %s", v->mount_point); sprintf(&mount_menu[mountable_volumes].unmount, "卸载 %s", v->mount_point); mount_menu[mountable_volumes].v = &device_volumes[i]; ++mountable_volumes; } if (is_safe_to_format(v->mount_point)) { sprintf(&format_menu[formatable_volumes].txt, "格式化 %s", v->mount_point); format_menu[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point)) { sprintf(&format_menu[formatable_volumes].txt, "格式化 %s", v->mount_point); format_menu[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } static char* confirm_format = "确定格式化?"; static char* confirm = "是 - 格式化该分区"; char confirm_string[255]; for (;;) { for (i = 0; i < mountable_volumes; i++) { MountMenuEntry* e = &mount_menu[i]; Volume* v = e->v; if(is_path_mounted(v->mount_point)) options[i] = e->unmount; else options[i] = e->mount; } for (i = 0; i < formatable_volumes; i++) { FormatMenuEntry* e = &format_menu[i]; options[mountable_volumes+i] = e->txt; } if (!is_data_media()) { options[mountable_volumes + formatable_volumes] = "挂载U盘模式"; options[mountable_volumes + formatable_volumes + 1] = NULL; } else { options[mountable_volumes + formatable_volumes] = "格式化 /data and /data/media (/sdcard)"; options[mountable_volumes + formatable_volumes + 1] = NULL; } int chosen_item = get_menu_selection(headers, &options, 0, 0); if (chosen_item == GO_BACK) break; if (chosen_item == (mountable_volumes+formatable_volumes)) { if (!is_data_media()) { show_mount_usb_storage_menu(); } else { if (!confirm_selection("格式化 /data 和 /data/media (/sdcard)", confirm)) continue; handle_data_media_format(1); ui_print("格式化DATA分区(/data)...\n"); if (0 != format_volume("/data")) ui_print("格式化DATA(/data)分区失败!\n"); else ui_print("格式化完成!\n"); handle_data_media_format(0); } } else if (chosen_item < mountable_volumes) { MountMenuEntry* e = &mount_menu[chosen_item]; Volume* v = e->v; if (is_path_mounted(v->mount_point)) { if (0 != ensure_path_unmounted(v->mount_point)) ui_print("卸载%s失败!\n", v->mount_point); } else { if (0 != ensure_path_mounted(v->mount_point)) ui_print("挂载%s失败!\n", v->mount_point); } } else if (chosen_item < (mountable_volumes + formatable_volumes)) { chosen_item = chosen_item - mountable_volumes; FormatMenuEntry* e = &format_menu[chosen_item]; Volume* v = e->v; sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format); if (!confirm_selection(confirm_string, confirm)) continue; ui_print("正在格式化%s...\n", v->mount_point); if (0 != format_volume(v->mount_point)) ui_print("格式化%s失败!\n", v->mount_point); else ui_print("格式化完成!\n"); } } free(mount_menu); free(format_menu); }
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); nandroid_restore_handler restore_handler = NULL; const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL }; const char* backup_filesystem = NULL; Volume *vol = volume_for_path(mount_point); const char *device = NULL; if (vol != NULL) device = vol->device; char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); struct stat file_info; if (0 != (ret = statfs(tmp, &file_info))) { // can't find the backup, it may be the new backup format? // iterate through the backup types printf("couldn't find old .img format\n"); char *filesystem; int i = 0; while ((filesystem = filesystems[i]) != NULL) { sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = unyaffs_wrapper; break; } sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = tar_extract_wrapper; break; } sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = dedupe_extract_wrapper; break; } i++; } if (backup_filesystem == NULL || restore_handler == NULL) { //ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); ui_print("No %s backup found(img, tar, dup). Skipping restore of %s.\n", name, mount_point); return 0; } else { printf("Found new backup image: %s\n", tmp); } // If the fs_type of this volume is "auto" or mount_point is /data // and is_data_media, let's revert // to using a rm -rf, rather than trying to do a // ext3/ext4/whatever format. // This is because some phones (like DroidX) will freak out if you // reformat the /system or /data partitions, and not boot due to // a locked bootloader. // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1 // have a /sdcard symlinked to /data/media. // Or of volume does not exist (.android_secure), just rm -rf. if (vol == NULL || 0 == strcmp(vol->fs_type, "auto")) backup_filesystem = NULL; if (0 == strcmp(vol->mount_point, "/data") && is_data_media()) backup_filesystem = NULL; } ensure_directory(mount_point); int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; compute_archive_stats(tmp); ui_print("Restoring %s...\n", name); if (backup_filesystem == NULL) { if (0 != (ret = format_volume(mount_point))) { ui_print("Error while formatting %s!\n", mount_point); return ret; } } else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) { ui_print("Error while formatting %s!\n", mount_point); return ret; } if (0 != (ret = ensure_path_mounted(mount_point))) { ui_print("Can't mount %s!\n", mount_point); return ret; } if (restore_handler == NULL) restore_handler = get_restore_handler(mount_point); if (restore_handler == NULL) { ui_print("Error finding an appropriate restore handler.\n"); return -2; } if (0 != (ret = restore_handler(tmp, mount_point, callback))) { ui_print("Error while restoring %s!\n", mount_point); return ret; } if (umount_when_finished) { ensure_path_unmounted(mount_point); } return 0; }
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) { ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); nandroid_files_total = 0; if (ensure_path_mounted(backup_path) != 0) return print_and_error("挂载分区失败\n"); char tmp[PATH_MAX]; ui_print("检查MD5 校验值...\n"); sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); if (0 != __system(tmp)) return print_and_error("MD5不合法\n"); int ret; if (restore_boot) { if (NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) return ret; if (NULL != volume_for_path("/uboot") && 0 != (ret = nandroid_restore_partition(backup_path, "/uboot"))) return ret; if (NULL != volume_for_path("/recovery") && 0 != (ret = nandroid_restore_partition(backup_path, "/recovery"))) return ret; } struct stat s; Volume *vol = volume_for_path("/wimax"); if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) { char serialno[PROPERTY_VALUE_MAX]; serialno[0] = 0; property_get("ro.serialno", serialno, ""); sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); struct stat st; if (0 != stat(tmp, &st)) { ui_print("警告: WiMAX分区存在,但是\n"); ui_print(" 不存在可恢复的WiMAX 镜像.\n"); ui_print(" 亦需要重新做一个备份 \n"); ui_print(" 来保护你的WiMAX keys.\n"); } else { ui_print("恢复之前先清除WiMAX...\n"); if (0 != (ret = format_volume("/wimax"))) return print_and_error("抹掉wimax失败!\n"); ui_print("恢复WiMAX 镜像...\n"); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) return ret; } } if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) return ret; if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) return ret; if (has_datadata()) { if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) return ret; } if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0))) return ret; if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0))) return ret; if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) return ret; sync(); ui_set_background(BACKGROUND_ICON_NONE); ui_reset_progress(); ui_print("\n恢复完成!\n"); return 0; }
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); nandroid_restore_handler restore_handler = NULL; const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL }; const char* backup_filesystem = NULL; Volume *vol = volume_for_path(mount_point); const char *device = NULL; if (vol != NULL) device = vol->device; char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); struct stat file_info; if (strcmp(backup_path, "-") == 0) { if (vol) backup_filesystem = vol->fs_type; restore_handler = tar_extract_wrapper; strcpy(tmp, "/proc/self/fd/0"); } else if (0 != (ret = statfs(tmp, &file_info))) { // can't find the backup, it may be the new backup format? // iterate through the backup types printf("找不到默认\n"); char *filesystem; int i = 0; while ((filesystem = filesystems[i]) != NULL) { sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = unyaffs_wrapper; break; } sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = tar_extract_wrapper; break; } sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = dedupe_extract_wrapper; break; } i++; } if (backup_filesystem == NULL || restore_handler == NULL) { ui_print("%s.镜像没找到,因此跳过该处恢复. %s.\n", name, mount_point); return 0; } else { printf("找到新的可恢复镜像: %s\n", tmp); } } // If the fs_type of this volume is "auto" or mount_point is /data // and is_data_media, let's revert // to using a rm -rf, rather than trying to do a // ext3/ext4/whatever format. // This is because some phones (like DroidX) will freak out if you // reformat the /system or /data partitions, and not boot due to // a locked bootloader. // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1 // have a /sdcard symlinked to /data/media. // Or of volume does not exist (.android_secure), just rm -rf. if (vol == NULL || 0 == strcmp(vol->fs_type, "auto")) backup_filesystem = NULL; if (0 == strcmp(vol->mount_point, "/data") && is_data_media()) backup_filesystem = NULL; ensure_directory(mount_point); int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; ui_print("正在恢复 %s...\n", name); if (backup_filesystem == NULL) { if (0 != (ret = format_volume(mount_point))) { ui_print("格式化下列分区失败 %s!\n", mount_point); return ret; } } else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) { ui_print("格式化下列分区失败 %s!\n", mount_point); return ret; } if (0 != (ret = ensure_path_mounted(mount_point))) { ui_print("挂载下列分区失败 %s!\n", mount_point); return ret; } if (restore_handler == NULL) restore_handler = get_restore_handler(mount_point); // override restore handler for undump if (strcmp(backup_path, "-") == 0) { restore_handler = tar_undump_wrapper; } if (restore_handler == NULL) { ui_print("找不到合适的方案\n"); return -2; } if (0 != (ret = restore_handler(tmp, mount_point, callback))) { ui_print("恢复失败: %s!\n", mount_point); return ret; } if (umount_when_finished) { ensure_path_unmounted(mount_point); } return 0; }
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) { ui_set_background(BACKGROUND_ICON_INSTALLING); ui_show_indeterminate_progress(); yaffs_files_total = 0; if (ensure_path_mounted("/sdcard") != 0) return print_and_error("Can't mount /sdcard\n"); char tmp[PATH_MAX]; ui_print("Checking MD5 sums...\n"); sprintf(tmp, "cd %s && md5sum -c nandroid.md5", backup_path); if (0 != __system(tmp)) return print_and_error("MD5 mismatch!\n"); int ret; if (restore_boot && NULL != volume_for_path("/boot") && 0 != (ret = nandroid_restore_partition(backup_path, "/boot"))) return ret; struct stat s; Volume *vol = volume_for_path("/wimax"); if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) { char serialno[PROPERTY_VALUE_MAX]; serialno[0] = 0; property_get("ro.serialno", serialno, ""); sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); struct stat st; if (0 != stat(tmp, &st)) { ui_print("WARNING: WiMAX partition exists, but nandroid\n"); ui_print(" backup does not contain WiMAX image.\n"); ui_print(" You should create a new backup to\n"); ui_print(" protect your WiMAX keys.\n"); } else { ui_print("Erasing WiMAX before restore...\n"); if (0 != (ret = format_volume("/wimax"))) return print_and_error("Error while formatting wimax!\n"); ui_print("Restoring WiMAX image...\n"); if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) return ret; } } if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "/system"))) return ret; if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/data"))) return ret; if (has_datadata()) { if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "/datadata"))) return ret; } if (restore_data && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/sdcard/.android_secure", 0))) return ret; if (restore_cache && 0 != (ret = nandroid_restore_partition_extended(backup_path, "/cache", 0))) return ret; #ifdef RECOVERY_HAVE_SD_EXT if (restore_sdext && 0 != (ret = nandroid_restore_partition(backup_path, "/sd-ext"))) return ret; #endif sync(); ui_set_background(BACKGROUND_ICON_NONE); ui_reset_progress(); ui_print("\nRestore complete!\n"); return 0; }
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); nandroid_restore_handler restore_handler = NULL; const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL }; const char* backup_filesystem = NULL; Volume *vol = volume_for_path(mount_point); const char *device = NULL; if (vol != NULL) device = vol->device; char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); struct stat file_info; if (0 != (ret = statfs(tmp, &file_info))) { // can't find the backup, it may be the new backup format? // iterate through the backup types printf("couldn't find default\n"); char *filesystem; int i = 0; while ((filesystem = filesystems[i]) != NULL) { sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = unyaffs_wrapper; break; } sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem); if (0 == (ret = statfs(tmp, &file_info))) { backup_filesystem = filesystem; restore_handler = tar_extract_wrapper; break; } i++; } if (backup_filesystem == NULL || restore_handler == NULL) { ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point); return 0; } else { printf("Found new backup image: %s\n", tmp); } // If the fs_type of this volume is "auto", let's revert to using a // rm -rf, rather than trying to do a ext3/ext4/whatever format. // This is because some phones (like DroidX) will freak out if you // reformat the /system or /data partitions, and not boot due to // a locked bootloader. // The "auto" fs type preserves the file system, and does not // trigger that lock. // Or of volume does not exist (.android_secure), just rm -rf. if (vol == NULL || 0 == strcmp(vol->fs_type, "auto")) backup_filesystem = NULL; } else { // Force legacy tar extraction to old CWM2 backup files restore_handler = tar_extract_wrapper_legacy; } ensure_directory(mount_point); int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0; ui_print("Restoring %s...\n", name); if (backup_filesystem == NULL) { if (0 != (ret = format_volume(mount_point))) { ui_print("Error while formatting %s!\n", mount_point); return ret; } } else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) { ui_print("Error while formatting %s!\n", mount_point); return ret; } if (0 != (ret = ensure_path_mounted(mount_point))) { ui_print("Can't mount %s!\n", mount_point); return ret; } if (restore_handler == NULL) restore_handler = get_restore_handler(mount_point); if (restore_handler == NULL) { ui_print("Error finding an appropriate restore handler.\n"); return -2; } if (0 != (ret = restore_handler(tmp, mount_point, callback))) { ui_print("Error while restoring %s!\n", mount_point); return ret; } if (umount_when_finished) { ensure_path_unmounted(mount_point); } return 0; }
/** * Prints a list of all parts to the screen inside of a boxed window, possibly * highlighting a selected one. * @param win The window to draw in. * @param y1 The y-coordinate to start drawing at. * @param max_y Draw no further than this y-coordinate. * @param width The width of the window. * @param p The index of the part being examined. * @param hl The index of the part to highlight (if any). */ int vehicle::print_part_list( const catacurses::window &win, int y1, const int max_y, int width, int p, int hl /*= -1*/ ) const { if( p < 0 || p >= ( int )parts.size() ) { return y1; } std::vector<int> pl = this->parts_at_relative( parts[p].mount.x, parts[p].mount.y ); int y = y1; for( size_t i = 0; i < pl.size(); i++ ) { if( y >= max_y ) { mvwprintz( win, y, 1, c_yellow, _( "More parts here..." ) ); ++y; break; } const vehicle_part &vp = parts[ pl [ i ] ]; nc_color col_cond = vp.is_broken() ? c_dark_gray : vp.base.damage_color(); std::string partname = vp.name(); if( vp.is_fuel_store() && vp.ammo_current() != "null" ) { partname += string_format( " (%s)", item::nname( vp.ammo_current() ).c_str() ); } if( part_flag( pl[i], "CARGO" ) ) { //~ used/total volume of a cargo vehicle part partname += string_format( _( " (vol: %s/%s %s)" ), format_volume( stored_volume( pl[i] ) ).c_str(), format_volume( max_volume( pl[i] ) ).c_str(), volume_units_abbr() ); } bool armor = part_flag( pl[i], "ARMOR" ); std::string left_sym; std::string right_sym; if( armor ) { left_sym = "("; right_sym = ")"; } else if( part_info( pl[i] ).location == part_location_structure ) { left_sym = "["; right_sym = "]"; } else { left_sym = "-"; right_sym = "-"; } nc_color sym_color = ( int )i == hl ? hilite( c_light_gray ) : c_light_gray; mvwprintz( win, y, 1, sym_color, left_sym ); trim_and_print( win, y, 2, getmaxx( win ) - 4, ( int )i == hl ? hilite( col_cond ) : col_cond, partname ); wprintz( win, sym_color, right_sym ); if( i == 0 && vpart_position( const_cast<vehicle &>( *this ), pl[i] ).is_inside() ) { //~ indicates that a vehicle part is inside mvwprintz( win, y, width - 2 - utf8_width( _( "Interior" ) ), c_light_gray, _( "Interior" ) ); } else if( i == 0 ) { //~ indicates that a vehicle part is outside mvwprintz( win, y, width - 2 - utf8_width( _( "Exterior" ) ), c_light_gray, _( "Exterior" ) ); } y++; } // print the label for this location const cata::optional<std::string> label = vpart_position( const_cast<vehicle &>( *this ), p ).get_label(); if( label && y <= max_y ) { mvwprintz( win, y++, 1, c_light_red, _( "Label: %s" ), label->c_str() ); } return y; }
void show_partition_menu() { static char* headers[3]; headers[0] = showpartitionheader; headers[1] = "\n"; headers[2] = NULL; static MountMenuEntry* mount_menue = NULL; static FormatMenuEntry* format_menue = NULL; typedef char* string; int i, mountable_volumes, formatable_volumes; int num_volumes; Volume* device_volumes; num_volumes = get_num_volumes(); device_volumes = get_device_volumes(); string options[255]; if(!device_volumes) return; mountable_volumes = 0; formatable_volumes = 0; mount_menue = malloc(num_volumes * sizeof(MountMenuEntry)); format_menue = malloc(num_volumes * sizeof(FormatMenuEntry)); for (i = 0; i < num_volumes; ++i) { Volume* v = &device_volumes[i]; if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0) { if (is_safe_to_mount(v->mount_point)) { sprintf(&mount_menue[mountable_volumes].mount, "Mount %s", v->mount_point); sprintf(&mount_menue[mountable_volumes].unmount, "Unmount %s", v->mount_point); mount_menue[mountable_volumes].v = &device_volumes[i]; ++mountable_volumes; } if (is_safe_to_format(v->mount_point)) { sprintf(&format_menue[formatable_volumes].txt, "Erase %s", v->mount_point); format_menue[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point)) { sprintf(&format_menue[formatable_volumes].txt, "Erase %s", v->mount_point); format_menue[formatable_volumes].v = &device_volumes[i]; ++formatable_volumes; } } static char* confirm_format = "Confirm format?"; static char* confirm = "Yes - Format"; char confirm_string[255]; for (;;) { for (i = 0; i < mountable_volumes; i++) { MountMenuEntry* e = &mount_menue[i]; Volume* v = e->v; if(is_path_mounted(v->mount_point)) options[i] = e->unmount; else options[i] = e->mount; } for (i = 0; i < formatable_volumes; i++) { FormatMenuEntry* e = &format_menue[i]; options[mountable_volumes+i] = e->txt; } options[mountable_volumes+formatable_volumes] = "Erase dalvik-cache"; options[mountable_volumes+formatable_volumes + 1] = NULL; int chosen_item = get_menu_selection(headers, &options, 0, 0); if (chosen_item == GO_BACK) break; if (chosen_item == (mountable_volumes+formatable_volumes)) { erase_dalvik_cache(0); } else if (chosen_item < mountable_volumes) { MountMenuEntry* e = &mount_menue[chosen_item]; Volume* v = e->v; if (is_path_mounted(v->mount_point)) { if (0 != ensure_path_unmounted(v->mount_point)) ui_print("%s %s!\n", unmounterror, v->mount_point); } else { if (0 != ensure_path_mounted(v->mount_point)) ui_print("%s %s!\n", mounterror, v->mount_point); } } else if (chosen_item < (mountable_volumes + formatable_volumes)) { chosen_item = chosen_item - mountable_volumes; FormatMenuEntry* e = &format_menue[chosen_item]; Volume* v = e->v; sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format); if (!confirm_selection(confirm_string, confirm)) continue; ui_print("Formatting %s...\n", v->mount_point); if (0 != format_volume(v->mount_point)) ui_print("Error formatting %s!\n", v->mount_point); else ui_print("%s\n", done); } } free(mount_menue); free(format_menue); }
// Pick up items at (pos). void Pickup::pick_up( const tripoint &pos, int min ) { int cargo_part = -1; const optional_vpart_position vp = g->m.veh_at( pos ); vehicle *const veh = veh_pointer_or_null( vp ); bool from_vehicle = false; if( min != -1 ) { switch( interact_with_vehicle( veh, pos, vp ? vp->part_index() : -1 ) ) { case DONE: return; case ITEMS_FROM_CARGO: { const cata::optional<vpart_reference> carg = vp.part_with_feature( "CARGO", false ); cargo_part = carg ? carg->part_index() : -1; } from_vehicle = cargo_part >= 0; break; case ITEMS_FROM_GROUND: // Nothing to change, default is to pick from ground anyway. if( g->m.has_flag( "SEALED", pos ) ) { return; } break; } } if( !from_vehicle ) { bool isEmpty = ( g->m.i_at( pos ).empty() ); // Hide the pickup window if this is a toilet and there's nothing here // but water. if( ( !isEmpty ) && g->m.furn( pos ) == f_toilet ) { isEmpty = true; for( auto maybe_water : g->m.i_at( pos ) ) { if( maybe_water.typeId() != "water" ) { isEmpty = false; break; } } } if( isEmpty && ( min != -1 || !get_option<bool>( "AUTO_PICKUP_ADJACENT" ) ) ) { return; } } // which items are we grabbing? std::vector<item> here; if( from_vehicle ) { auto vehitems = veh->get_items( cargo_part ); here.resize( vehitems.size() ); std::copy( vehitems.begin(), vehitems.end(), here.begin() ); } else { auto mapitems = g->m.i_at( pos ); here.resize( mapitems.size() ); std::copy( mapitems.begin(), mapitems.end(), here.begin() ); } if( min == -1 ) { // Recursively pick up adjacent items if that option is on. if( get_option<bool>( "AUTO_PICKUP_ADJACENT" ) && g->u.pos() == pos ) { //Autopickup adjacent direction adjacentDir[8] = {NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST}; for( auto &elem : adjacentDir ) { tripoint apos = tripoint( direction_XY( elem ), 0 ); apos += pos; pick_up( apos, min ); } } // Bail out if this square cannot be auto-picked-up if( g->check_zone( zone_type_id( "NO_AUTO_PICKUP" ), pos ) ) { return; } else if( g->m.has_flag( "SEALED", pos ) ) { return; } } // Not many items, just grab them if( ( int )here.size() <= min && min != -1 ) { g->u.assign_activity( activity_id( "ACT_PICKUP" ) ); g->u.activity.placement = pos - g->u.pos(); g->u.activity.values.push_back( from_vehicle ); // Only one item means index is 0. g->u.activity.values.push_back( 0 ); // auto-pickup means pick up all. g->u.activity.values.push_back( 0 ); return; } std::vector<std::list<item_idx>> stacked_here; for( size_t i = 0; i < here.size(); i++ ) { item &it = here[i]; bool found_stack = false; for( auto &stack : stacked_here ) { if( stack.begin()->_item.stacks_with( it ) ) { item_idx el = { it, i }; stack.push_back( el ); found_stack = true; break; } } if( !found_stack ) { std::list<item_idx> newstack; newstack.push_back( { it, i } ); stacked_here.push_back( newstack ); } } std::reverse( stacked_here.begin(), stacked_here.end() ); if( min != -1 ) { // don't bother if we're just autopickuping g->temp_exit_fullscreen(); } bool sideStyle = use_narrow_sidebar(); // Otherwise, we have Autopickup, 2 or more items and should list them, etc. int maxmaxitems = sideStyle ? TERMY : getmaxy( g->w_messages ) - 3; int itemsH = std::min( 25, TERMY / 2 ); int pickupBorderRows = 3; // The pickup list may consume the entire terminal, minus space needed for its // header/footer and the item info window. int minleftover = itemsH + pickupBorderRows; if( maxmaxitems > TERMY - minleftover ) { maxmaxitems = TERMY - minleftover; } const int minmaxitems = sideStyle ? 6 : 9; std::vector<pickup_count> getitem( stacked_here.size() ); int maxitems = stacked_here.size(); maxitems = ( maxitems < minmaxitems ? minmaxitems : ( maxitems > maxmaxitems ? maxmaxitems : maxitems ) ); int itemcount = 0; if( min == -1 ) { //Auto Pickup, select matching items if( !select_autopickup_items( stacked_here, getitem ) ) { // If we didn't find anything, bail out now. return; } } else { int pickupH = maxitems + pickupBorderRows; int pickupW = getmaxx( g->w_messages ); int pickupY = VIEW_OFFSET_Y; int pickupX = getbegx( g->w_messages ); int itemsW = pickupW; int itemsY = sideStyle ? pickupY + pickupH : TERMY - itemsH; int itemsX = pickupX; catacurses::window w_pickup = catacurses::newwin( pickupH, pickupW, pickupY, pickupX ); catacurses::window w_item_info = catacurses::newwin( itemsH, itemsW, itemsY, itemsX ); std::string action; long raw_input_char = ' '; input_context ctxt( "PICKUP" ); ctxt.register_action( "UP" ); ctxt.register_action( "DOWN" ); ctxt.register_action( "RIGHT" ); ctxt.register_action( "LEFT" ); ctxt.register_action( "NEXT_TAB", _( "Next page" ) ); ctxt.register_action( "PREV_TAB", _( "Previous page" ) ); ctxt.register_action( "SCROLL_UP" ); ctxt.register_action( "SCROLL_DOWN" ); ctxt.register_action( "CONFIRM" ); ctxt.register_action( "SELECT_ALL" ); ctxt.register_action( "QUIT", _( "Cancel" ) ); ctxt.register_action( "ANY_INPUT" ); ctxt.register_action( "HELP_KEYBINDINGS" ); ctxt.register_action( "FILTER" ); int start = 0; int cur_it = 0; bool update = true; mvwprintw( w_pickup, 0, 0, _( "PICK UP" ) ); int selected = 0; int iScrollPos = 0; std::string filter; std::string new_filter; std::vector<int> matches;//Indexes of items that match the filter bool filter_changed = true; if( g->was_fullscreen ) { g->draw_ter(); } // Now print the two lists; those on the ground and about to be added to inv // Continue until we hit return or space do { const std::string pickup_chars = ctxt.get_available_single_char_hotkeys( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:;" ); int idx = -1; for( int i = 1; i < pickupH; i++ ) { mvwprintw( w_pickup, i, 0, " " ); } if( action == "ANY_INPUT" && raw_input_char >= '0' && raw_input_char <= '9' ) { int raw_input_char_value = ( char )raw_input_char - '0'; itemcount *= 10; itemcount += raw_input_char_value; if( itemcount < 0 ) { itemcount = 0; } } else if( action == "SCROLL_UP" ) { iScrollPos--; } else if( action == "SCROLL_DOWN" ) { iScrollPos++; } else if( action == "PREV_TAB" ) { if( start > 0 ) { start -= maxitems; } else { start = ( int )( ( matches.size() - 1 ) / maxitems ) * maxitems; } selected = start; mvwprintw( w_pickup, maxitems + 2, 0, " " ); } else if( action == "NEXT_TAB" ) { if( start + maxitems < ( int )matches.size() ) { start += maxitems; } else { start = 0; } iScrollPos = 0; selected = start; mvwprintw( w_pickup, maxitems + 2, pickupH, " " ); } else if( action == "UP" ) { selected--; iScrollPos = 0; if( selected < 0 ) { selected = matches.size() - 1; start = ( int )( matches.size() / maxitems ) * maxitems; if( start >= ( int )matches.size() ) { start -= maxitems; } } else if( selected < start ) { start -= maxitems; } } else if( action == "DOWN" ) { selected++; iScrollPos = 0; if( selected >= ( int )matches.size() ) { selected = 0; start = 0; } else if( selected >= start + maxitems ) { start += maxitems; } } else if( selected >= 0 && selected < int( matches.size() ) && ( ( action == "RIGHT" && !getitem[matches[selected]].pick ) || ( action == "LEFT" && getitem[matches[selected]].pick ) ) ) { idx = selected; } else if( action == "FILTER" ) { new_filter = filter; string_input_popup popup; popup .title( _( "Set filter" ) ) .width( 30 ) .edit( new_filter ); if( !popup.canceled() ) { filter_changed = true; } else { wrefresh( g->w_terrain ); } } else if( action == "ANY_INPUT" && raw_input_char == '`' ) { std::string ext = string_input_popup() .title( _( "Enter 2 letters (case sensitive):" ) ) .width( 3 ) .max_length( 2 ) .query_string(); if( ext.size() == 2 ) { int p1 = pickup_chars.find( ext.at( 0 ) ); int p2 = pickup_chars.find( ext.at( 1 ) ); if( p1 != -1 && p2 != -1 ) { idx = pickup_chars.size() + ( p1 * pickup_chars.size() ) + p2; } } } else if( action == "ANY_INPUT" ) { idx = ( raw_input_char <= 127 ) ? pickup_chars.find( raw_input_char ) : -1; iScrollPos = 0; } if( idx >= 0 && idx < ( int )matches.size() ) { size_t true_idx = matches[idx]; if( itemcount != 0 || getitem[true_idx].count == 0 ) { item &temp = stacked_here[true_idx].begin()->_item; int amount_available = temp.count_by_charges() ? temp.charges : stacked_here[true_idx].size(); if( itemcount >= amount_available ) { itemcount = 0; } getitem[true_idx].count = itemcount; itemcount = 0; } // Note: this might not change the value of getitem[idx] at all! getitem[true_idx].pick = ( action == "RIGHT" ? true : ( action == "LEFT" ? false : !getitem[true_idx].pick ) ); if( action != "RIGHT" && action != "LEFT" ) { selected = idx; start = ( int )( idx / maxitems ) * maxitems; } if( !getitem[true_idx].pick ) { getitem[true_idx].count = 0; } update = true; } if( filter_changed ) { matches.clear(); while( matches.empty() ) { auto filter_func = item_filter_from_string( new_filter ); for( size_t index = 0; index < stacked_here.size(); index++ ) { if( filter_func( stacked_here[index].begin()->_item ) ) { matches.push_back( index ); } } if( matches.empty() ) { popup( _( "Your filter returned no results" ) ); wrefresh( g->w_terrain ); // The filter must have results, or simply be emptied or canceled, // as this screen can't be reached without there being // items available string_input_popup popup; popup .title( _( "Set filter" ) ) .width( 30 ) .edit( new_filter ); if( popup.canceled() ) { new_filter = filter; filter_changed = false; } } } if( filter_changed ) { filter = new_filter; filter_changed = false; selected = 0; start = 0; iScrollPos = 0; } wrefresh( g->w_terrain ); } item &selected_item = stacked_here[matches[selected]].begin()->_item; werase( w_item_info ); if( selected >= 0 && selected <= ( int )stacked_here.size() - 1 ) { std::vector<iteminfo> vThisItem; std::vector<iteminfo> vDummy; selected_item.info( true, vThisItem ); draw_item_info( w_item_info, "", "", vThisItem, vDummy, iScrollPos, true, true ); } draw_custom_border( w_item_info, false ); mvwprintw( w_item_info, 0, 2, "< " ); trim_and_print( w_item_info, 0, 4, itemsW - 8, c_white, "%s >", selected_item.display_name().c_str() ); wrefresh( w_item_info ); if( action == "SELECT_ALL" ) { int count = 0; for( auto i : matches ) { if( getitem[i].pick ) { count++; } getitem[i].pick = true; } if( count == ( int )stacked_here.size() ) { for( size_t i = 0; i < stacked_here.size(); i++ ) { getitem[i].pick = false; } } update = true; } for( cur_it = start; cur_it < start + maxitems; cur_it++ ) { mvwprintw( w_pickup, 1 + ( cur_it % maxitems ), 0, " " ); if( cur_it < ( int )matches.size() ) { int true_it = matches[cur_it]; item &this_item = stacked_here[ true_it ].begin()->_item; nc_color icolor = this_item.color_in_inventory(); if( cur_it == selected ) { icolor = hilite( icolor ); } if( cur_it < ( int )pickup_chars.size() ) { mvwputch( w_pickup, 1 + ( cur_it % maxitems ), 0, icolor, char( pickup_chars[cur_it] ) ); } else if( cur_it < ( int )pickup_chars.size() + ( int )pickup_chars.size() * ( int )pickup_chars.size() ) { int p = cur_it - pickup_chars.size(); int p1 = p / pickup_chars.size(); int p2 = p % pickup_chars.size(); mvwprintz( w_pickup, 1 + ( cur_it % maxitems ), 0, icolor, "`%c%c", char( pickup_chars[p1] ), char( pickup_chars[p2] ) ); } else { mvwputch( w_pickup, 1 + ( cur_it % maxitems ), 0, icolor, ' ' ); } if( getitem[true_it].pick ) { if( getitem[true_it].count == 0 ) { wprintz( w_pickup, c_light_blue, " + " ); } else { wprintz( w_pickup, c_light_blue, " # " ); } } else { wprintw( w_pickup, " - " ); } std::string item_name; if( stacked_here[true_it].begin()->_item.ammo_type() == "money" ) { //Count charges //TODO: transition to the item_location system used for the inventory unsigned long charges_total = 0; for( const auto item : stacked_here[true_it] ) { charges_total += item._item.charges; } //Picking up none or all the cards in a stack if( !getitem[true_it].pick || getitem[true_it].count == 0 ) { item_name = stacked_here[true_it].begin()->_item.display_money( stacked_here[true_it].size(), charges_total ); } else { unsigned long charges = 0; int c = getitem[true_it].count; for( auto it = stacked_here[true_it].begin(); it != stacked_here[true_it].end() && c > 0; ++it, --c ) { charges += it->_item.charges; } item_name = string_format( _( "%s of %s" ), stacked_here[true_it].begin()->_item.display_money( getitem[true_it].count, charges ), format_money( charges_total ) ); } } else { item_name = this_item.display_name( stacked_here[true_it].size() ); } if( stacked_here[true_it].size() > 1 ) { item_name = string_format( "%d %s", stacked_here[true_it].size(), item_name.c_str() ); } if( get_option<bool>( "ITEM_SYMBOLS" ) ) { item_name = string_format( "%s %s", this_item.symbol().c_str(), item_name.c_str() ); } trim_and_print( w_pickup, 1 + ( cur_it % maxitems ), 6, pickupW - 4, icolor, item_name ); } } mvwprintw( w_pickup, maxitems + 1, 0, _( "[%s] Unmark" ), ctxt.get_desc( "LEFT", 1 ).c_str() ); center_print( w_pickup, maxitems + 1, c_light_gray, string_format( _( "[%s] Help" ), ctxt.get_desc( "HELP_KEYBINDINGS", 1 ).c_str() ) ); right_print( w_pickup, maxitems + 1, 0, c_light_gray, string_format( _( "[%s] Mark" ), ctxt.get_desc( "RIGHT", 1 ).c_str() ) ); mvwprintw( w_pickup, maxitems + 2, 0, _( "[%s] Prev" ), ctxt.get_desc( "PREV_TAB", 1 ).c_str() ); center_print( w_pickup, maxitems + 2, c_light_gray, string_format( _( "[%s] All" ), ctxt.get_desc( "SELECT_ALL", 1 ).c_str() ) ); right_print( w_pickup, maxitems + 2, 0, c_light_gray, string_format( _( "[%s] Next" ), ctxt.get_desc( "NEXT_TAB", 1 ).c_str() ) ); if( update ) { // Update weight & volume information update = false; for( int i = 9; i < pickupW; ++i ) { mvwaddch( w_pickup, 0, i, ' ' ); } units::mass weight_picked_up = 0; units::volume volume_picked_up = 0; for( size_t i = 0; i < getitem.size(); i++ ) { if( getitem[i].pick ) { item temp = stacked_here[i].begin()->_item; if( temp.count_by_charges() && getitem[i].count < temp.charges && getitem[i].count != 0 ) { temp.charges = getitem[i].count; } int num_picked = std::min( stacked_here[i].size(), getitem[i].count == 0 ? stacked_here[i].size() : getitem[i].count ); weight_picked_up += temp.weight() * num_picked; volume_picked_up += temp.volume() * num_picked; } } auto weight_predict = g->u.weight_carried() + weight_picked_up; auto volume_predict = g->u.volume_carried() + volume_picked_up; mvwprintz( w_pickup, 0, 9, weight_predict > g->u.weight_capacity() ? c_red : c_white, _( "Wgt %.1f" ), round_up( convert_weight( weight_predict ), 1 ) ); wprintz( w_pickup, c_white, "/%.1f", round_up( convert_weight( g->u.weight_capacity() ), 1 ) ); std::string fmted_volume_predict = format_volume( volume_predict ); mvwprintz( w_pickup, 0, 24, volume_predict > g->u.volume_capacity() ? c_red : c_white, _( "Vol %s" ), fmted_volume_predict.c_str() ); std::string fmted_volume_capacity = format_volume( g->u.volume_capacity() ); wprintz( w_pickup, c_white, "/%s", fmted_volume_capacity.c_str() ); }; wrefresh( w_pickup ); action = ctxt.handle_input(); raw_input_char = ctxt.get_raw_input().get_first_input(); } while( action != "QUIT" && action != "CONFIRM" ); bool item_selected = false; // Check if we have selected an item. for( auto selection : getitem ) { if( selection.pick ) { item_selected = true; } } if( action != "CONFIRM" || !item_selected ) { w_pickup = catacurses::window(); w_item_info = catacurses::window(); add_msg( _( "Never mind." ) ); g->reenter_fullscreen(); g->refresh_all(); return; } } // At this point we've selected our items, register an activity to pick them up. g->u.assign_activity( activity_id( "ACT_PICKUP" ) ); g->u.activity.placement = pos - g->u.pos(); g->u.activity.values.push_back( from_vehicle ); if( min == -1 ) { // Auto pickup will need to auto resume since there can be several of them on the stack. g->u.activity.auto_resume = true; } std::vector<std::pair<int, int>> pick_values; for( size_t i = 0; i < stacked_here.size(); i++ ) { const auto &selection = getitem[i]; if( !selection.pick ) { continue; } const auto &stack = stacked_here[i]; // Note: items can be both charged and stacked // For robustness, let's assume they can be both in the same stack bool pick_all = selection.count == 0; size_t count = selection.count; for( const item_idx &it : stack ) { if( !pick_all && count == 0 ) { break; } if( it._item.count_by_charges() ) { size_t num_picked = std::min( ( size_t )it._item.charges, count ); pick_values.push_back( { static_cast<int>( it.idx ), static_cast<int>( num_picked ) } ); count -= num_picked; } else { size_t num_picked = 1; pick_values.push_back( { static_cast<int>( it.idx ), 0 } ); count -= num_picked; } } } // The pickup activity picks up items last-to-first from its values list, so make sure the // higher indices are at the end. std::sort( pick_values.begin(), pick_values.end() ); for( auto &it : pick_values ) { g->u.activity.values.push_back( it.first ); g->u.activity.values.push_back( it.second ); } g->reenter_fullscreen(); }