自己放某个文件到/cache分区,重启后发现文件消失了,那么是怎么消失的呢?【转】

自己放某个文件到/cache分区,重启后发现文件消失了,那么是怎么消失的呢?


Step 1. 

packages\providers\DownloadProvider\src\com\android\providers\downloads\StorageManager.java:


   
   
  1. /**
  2. * Removes files in the systemcache and downloads data dir without corresponding entries in
  3. * the downloads database.
  4. * This can occur if a delete is done on the database but the file is not removed from the
  5. * filesystem (due to sudden death of the process, for example).
  6. * This is not a very common occurrence. So, do this only once in a while.
  7. */
  8. private void removeSpuriousFiles() {
  9. if (true || Constants.LOGV) {
  10. Log.i(Constants.TAG, “in removeSpuriousFiles”);
  11. }
  12. // get a list of all files in system cache dir and downloads data dir
  13. List <File> files = new ArrayList <File>();
  14. File[] listOfFiles = mSystemCacheDir.listFiles();
  15. if (listOfFiles != null) {
  16. files.addAll(Arrays.asList(listOfFiles));
  17. }
  18. listOfFiles = mDownloadDataDir.listFiles();
  19. if (listOfFiles != null) {
  20. files.addAll(Arrays.asList(listOfFiles));
  21. }
  22. if (files.size() == 0) {
  23. return;
  24. }
  25. Cursor cursor = mContext.getContentResolver().query(
  26. Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
  27. new String[] { Downloads.Impl._DATA }, null, null, null);
  28. try {
  29. if (cursor != null) {
  30. while (cursor.moveToNext()) {
  31. String filename = cursor.getString(0);
  32. if (!TextUtils.isEmpty(filename)) {
  33. if (true || Constants.LOGV) {
  34. Log.i(Constants.TAG, “in removeSpuriousFiles, preserving file ” +
  35. filename);
  36. }
  37. files.remove(new File(filename));
  38. }
  39. }
  40. }
  41. } finally {
  42. if (cursor != null) {
  43. cursor.close();
  44. }
  45. }
  46. // delete the files not found in the database
  47. for (File file : files) {
  48. if (file.getName().equals(Constants.KNOWN_SPURIOUS_FILENAME) ||
  49. file.getName().equalsIgnoreCase(Constants.RECOVERY_DIRECTORY)) {
  50. continue;
  51. }
  52. if (true || Constants.LOGV) {
  53. Log.i(Constants.TAG, “deleting spurious file ” + file.getAbsolutePath());
  54. }
  55. file.delete();
  56. }
  57. }

   
   
  1. /** A magic filename that is allowed to exist within the system cache */
  2. public static final String KNOWN_SPURIOUS_FILENAME = "lost+found";
  3. /** A magic filename that is allowed to exist within the system cache */
  4. public static final String RECOVERY_DIRECTORY = "recovery";

除 lost+found, recovery这两个目录外的文件都删掉


Setp 2.

frameworks\base\core\java\android\os\RecoverySystem.java:


   
   
  1. /**
  2. * Called after booting to process and remove recovery-related files.
  3. * @return the log file from recovery, or null if none was found.
  4. *
  5. * @hide
  6. */
  7. public static String handleAftermath() {
  8. // Record the tail of the LOG_FILE
  9. String log = null;
  10. try {
  11. log = FileUtils.readTextFile(LOG_FILE, -LOG_FILE_MAX_LENGTH, "...\n");
  12. } catch (FileNotFoundException e) {
  13. Log.i(TAG, "No recovery log file");
  14. } catch (IOException e) {
  15. Log.e(TAG, "Error reading recovery log", e);
  16. }
  17. // Delete everything in RECOVERY_DIR except those beginning
  18. // with LAST_PREFIX
  19. String[] names = RECOVERY_DIR.list();
  20. for (int i = 0; names != null && i < names.length; i++) {
  21. if (names[i].startsWith(LAST_PREFIX)) continue;
  22. File f = new File(RECOVERY_DIR, names[i]);
  23. if (!f.delete()) {
  24. Log.e(TAG, "Can't delete: " + f);
  25. } else {
  26. Log.i(TAG, "Deleted: " + f);
  27. }
  28. }
  29. return log;
  30. }
private static String LAST_PREFIX = "last_";
   
   
/cache/recovery目录中,除last_开头的文件都删掉


————————————————

开机走完这两步后幸存的文件只有 /cache/lost+found目录中的文件,及/cache/recovery/last_ 开头的文件了





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值