在Android 系统Udate时,需要删除系统原先的全部数据。这里提供一个方法,在系统Update后,恢复个人自己的私有文件或数据。

1 修改 /bootable/recovery/recovery.c 

2 提供一个特定目录保存自己的文件/recovery-bak/data-bak.zip,也可以自定义备份文件夹;要与上面的代码相对应。

 

 

 
  

  1.  
  2. /bootable/recovery/recovery.c main函数: 
  3. ..... 
  4.  
  5.     } else if (wipe_data) { 
  6.         printf("wipe_data..., default INSTALL_SUCCESS value = %d\n", status); 
  7.         if (device_wipe_data()) status = INSTALL_ERROR; 
  8.         if (erase_volume("/data")) status = INSTALL_ERROR; 
  9.         if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; 
  10.         if (copy_bak()) status = INSTALL_ERROR; 
  11.         if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n"); 
  12.  
  13. 添加函数 
  14. static int 
  15. copy_bak(void) { 
  16.     if (ensure_path_mounted("/data") !=0) { 
  17.         LOGE("ensure_path_mounted failed to mount \"%s\"\n""/data"); 
  18.         return -1; 
  19.     } 
  20.  
  21.     if (ensure_path_mounted("/recovery-bak") !=0) { 
  22.         LOGE("ensure_path_mounted failed to mount \"%s\"\n""/recovery-bak"); 
  23.         return -1; 
  24.     } 
  25.  
  26.     ZipArchive zip_bak; 
  27.     int err = mzOpenZipArchive("/recovery-bak/data-bak.zip", &zip_bak); 
  28.     if (err != 0) { 
  29.         LOGI("Can't open %s\n""data-bak.zip"); 
  30.         return -1; 
  31.     } 
  32.     else { 
  33.         LOGI("start copy bak\n"); 
  34.         // To create a consistent system p_w_picpath, never use the clock for timestamps. 
  35.         struct utimbuf timestamp = { 1217592000, 1217592000 };  // 8/1/2008 default 
  36.         bool success = mzExtractRecursive(&zip_bak, "data""/data"
  37.                 MZ_EXTRACT_FILES_ONLY, &timestamp, 
  38.                 NULL, NULL); 
  39.         mzCloseZipArchive(&zip_bak); 
  40.         LOGI("copy bak %s\n",((success==true)?"success":"failed")); 
  41.         dirSetHierarchyPermissions("/data/app", 1000, 1000, 0771, 0644); 
  42.     } 
  43.  
  44.     if (ensure_path_unmounted("/data") !=0) { 
  45.         LOGE("ensure_path_unmounted failed to unmount \"%s\"\n""/data"); 
  46.         return -1; 
  47.     } 
  48.  
  49.     if (ensure_path_unmounted("/recovery-bak") !=0) { 
  50.         LOGE("ensure_path_unmounted failed to unmount \"%s\"\n""/recovery-bak"); 
  51.         return -1; 
  52.     } 
  53.  
  54.     return 0;