在Android 系統Udate時,需要刪除系統原先的全部數據。這裏提供一個方法,在系統Update後,恢復個人自己的私有文件或數據。
1 修改 /bootable/recovery/recovery.c
2 提供一個特定目錄保存自己的文件/recovery-bak/data-bak.zip,也可以自定義備份文件夾;要與上面的代碼相對應。
- /bootable/recovery/recovery.c main函數:
- .....
- } else if (wipe_data) {
- printf("wipe_data..., default INSTALL_SUCCESS value = %d\n", status);
- if (device_wipe_data()) status = INSTALL_ERROR;
- if (erase_volume("/data")) status = INSTALL_ERROR;
- if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
- if (copy_bak()) status = INSTALL_ERROR;
- if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
- 添加函數
- static int
- copy_bak(void) {
- if (ensure_path_mounted("/data") !=0) {
- LOGE("ensure_path_mounted failed to mount \"%s\"\n", "/data");
- return -1;
- }
- if (ensure_path_mounted("/recovery-bak") !=0) {
- LOGE("ensure_path_mounted failed to mount \"%s\"\n", "/recovery-bak");
- return -1;
- }
- ZipArchive zip_bak;
- int err = mzOpenZipArchive("/recovery-bak/data-bak.zip", &zip_bak);
- if (err != 0) {
- LOGI("Can't open %s\n", "data-bak.zip");
- return -1;
- }
- else {
- LOGI("start copy bak\n");
- // To create a consistent system image, never use the clock for timestamps.
- struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
- bool success = mzExtractRecursive(&zip_bak, "data", "/data",
- MZ_EXTRACT_FILES_ONLY, ×tamp,
- NULL, NULL);
- mzCloseZipArchive(&zip_bak);
- LOGI("copy bak %s\n",((success==true)?"success":"failed"));
- dirSetHierarchyPermissions("/data/app", 1000, 1000, 0771, 0644);
- }
- if (ensure_path_unmounted("/data") !=0) {
- LOGE("ensure_path_unmounted failed to unmount \"%s\"\n", "/data");
- return -1;
- }
- if (ensure_path_unmounted("/recovery-bak") !=0) {
- LOGE("ensure_path_unmounted failed to unmount \"%s\"\n", "/recovery-bak");
- return -1;
- }
- return 0;
- }