Android:/How does the files in the cache disappear?

Source: Internet
Author: User
Tags addall

Put a file to the/cache partition by yourself. After restarting, the file disappears. How does it disappear?

Step 1.

Packages \ providers \ downloadprovider \ SRC \ com \ Android \ providers \ downloads \ storagemanager. Java:

    /**     * 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();        }    }
    /** 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";

All files except the lost + found and recovery directories are deleted.

Setp 2.

Frameworks \ base \ core \ Java \ Android \ OS \ recoverysystem. Java:

    /**     * 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;    }
private static String LAST_PREFIX = "last_";

In the/Cache/recovery directory, all files starting with last _ are deleted.

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

After the two steps are completed, only files in the/Cache/lost + found directory and files starting with/Cache/recovery/last _ exist.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.