Android: /cache中的檔案是怎麼消失的

來源:互聯網
上載者:User

自己放某個檔案到/cache分區,重啟後發現檔案消失了,那麼是怎麼消失的呢?

Step 1. 

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

[html]
view plaincopyprint?
  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. }  
    /**     * Removes files in the systemcache and downloads data dir without corresponding entries in     * the downloads database.     * This can occur if a delete is done on the database but the file is not removed from the     * filesystem (due to sudden death of the process, for example).     * This is not a very common occurrence. So, do this only once in a while.     */    private void removeSpuriousFiles() {        if (true || Constants.LOGV) {            Log.i(Constants.TAG, "in removeSpuriousFiles");        }        // get a list of all files in system cache dir and downloads data dir        List<File> files = new ArrayList<File>();        File[] listOfFiles = mSystemCacheDir.listFiles();        if (listOfFiles != null) {            files.addAll(Arrays.asList(listOfFiles));        }        listOfFiles = mDownloadDataDir.listFiles();        if (listOfFiles != null) {            files.addAll(Arrays.asList(listOfFiles));        }        if (files.size() == 0) {            return;        }        Cursor cursor = mContext.getContentResolver().query(                Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,                new String[] { Downloads.Impl._DATA }, null, null, null);        try {            if (cursor != null) {                while (cursor.moveToNext()) {                    String filename = cursor.getString(0);                    if (!TextUtils.isEmpty(filename)) {                        if (true || Constants.LOGV) {                            Log.i(Constants.TAG, "in removeSpuriousFiles, preserving file " +                                    filename);                        }                        files.remove(new File(filename));                    }                }            }        } finally {            if (cursor != null) {                cursor.close();            }        }        // delete the files not found in the database        for (File file : files) {            if (file.getName().equals(Constants.KNOWN_SPURIOUS_FILENAME) ||                    file.getName().equalsIgnoreCase(Constants.RECOVERY_DIRECTORY)) {                continue;            }            if (true || Constants.LOGV) {                Log.i(Constants.TAG, "deleting spurious file " + file.getAbsolutePath());            }            file.delete();        }    }
[html]
view plaincopyprint?
  1. /** A magic filename that is allowed to exist within the system cache */  
  2. public static final String KNOWN_SPURIOUS_FILENAME = "lost+found";  
  3.   
  4. /** A magic filename that is allowed to exist within the system cache */  
  5. public static final String RECOVERY_DIRECTORY = "recovery";  
    /** A magic filename that is allowed to exist within the system cache */    public static final String KNOWN_SPURIOUS_FILENAME = "lost+found";    /** A magic filename that is allowed to exist within the system cache */    public static final String RECOVERY_DIRECTORY = "recovery";

除 lost+found, recovery這兩個目錄外的檔案都刪掉

Setp 2.

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

[html]
view plaincopyprint?
  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.   
  18.     // Delete everything in RECOVERY_DIR except those beginning  
  19.     // with LAST_PREFIX  
  20.     String[] names = RECOVERY_DIR.list();  
  21.     for (int i = 0; names != null && i < names.length; i++) {  
  22.         if (names[i].startsWith(LAST_PREFIX)) continue;  
  23.         File f = new File(RECOVERY_DIR, names[i]);  
  24.         if (!f.delete()) {  
  25.             Log.e(TAG, "Can't delete: " + f);  
  26.         } else {  
  27.             Log.i(TAG, "Deleted: " + f);  
  28.         }  
  29.     }  
  30.   
  31.     return log;  
  32. }  
    /**     * Called after booting to process and remove recovery-related files.     * @return the log file from recovery, or null if none was found.     *     * @hide     */    public static String handleAftermath() {        // Record the tail of the LOG_FILE        String log = null;        try {            log = FileUtils.readTextFile(LOG_FILE, -LOG_FILE_MAX_LENGTH, "...\n");        } catch (FileNotFoundException e) {            Log.i(TAG, "No recovery log file");        } catch (IOException e) {            Log.e(TAG, "Error reading recovery log", e);        }        // Delete everything in RECOVERY_DIR except those beginning        // with LAST_PREFIX        String[] names = RECOVERY_DIR.list();        for (int i = 0; names != null && i < names.length; i++) {            if (names[i].startsWith(LAST_PREFIX)) continue;            File f = new File(RECOVERY_DIR, names[i]);            if (!f.delete()) {                Log.e(TAG, "Can't delete: " + f);            } else {                Log.i(TAG, "Deleted: " + f);            }        }        return log;    }
[html]
view plaincopyprint?
  1. private static String LAST_PREFIX = "last_";  
private static String LAST_PREFIX = "last_";

/cache/recovery目錄中,除last_開頭的檔案都刪掉

------------------------------------------------

開機走完這兩步後倖存的檔案只有 /cache/lost+found目錄中的檔案,及/cache/recovery/last_ 開頭的檔案了

來自:http://blog.csdn.net/zmyde2010/article/details/7408207

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.