Android: /cache中的文件是怎么消失的[转]
16lz
2021-01-25
自己放某个文件到/cache分区,重启后发现文件消失了,那么是怎么消失的呢?
Step 1.
packages\providers\DownloadProvider\src\com\android\providers\downloads\StorageManager.java:
- /**
- *Removesfilesinthesystemcacheanddownloadsdatadirwithoutcorrespondingentriesin
- *thedownloadsdatabase.
- *Thiscanoccurifadeleteisdoneonthedatabasebutthefileisnotremovedfromthe
- *filesystem(duetosuddendeathoftheprocess,forexample).
- *Thisisnotaverycommonoccurrence.So,dothisonlyonceinawhile.
- */
- privatevoidremoveSpuriousFiles(){
- if(true||Constants.LOGV){
- Log.i(Constants.TAG,"inremoveSpuriousFiles");
- }
- //getalistofallfilesinsystemcachediranddownloadsdatadir
- List<File>files=newArrayList<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;
- }
- Cursorcursor=mContext.getContentResolver().query(
- Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
- newString[]{Downloads.Impl._DATA},null,null,null);
- try{
- if(cursor!=null){
- while(cursor.moveToNext()){
- Stringfilename=cursor.getString(0);
- if(!TextUtils.isEmpty(filename)){
- if(true||Constants.LOGV){
- Log.i(Constants.TAG,"inremoveSpuriousFiles,preservingfile"+
- filename);
- }
- files.remove(newFile(filename));
- }
- }
- }
- }finally{
- if(cursor!=null){
- cursor.close();
- }
- }
- //deletethefilesnotfoundinthedatabase
- for(Filefile:files){
- if(file.getName().equals(Constants.KNOWN_SPURIOUS_FILENAME)||
- file.getName().equalsIgnoreCase(Constants.RECOVERY_DIRECTORY)){
- continue;
- }
- if(true||Constants.LOGV){
- Log.i(Constants.TAG,"deletingspuriousfile"+file.getAbsolutePath());
- }
- file.delete();
- }
- }
/** * 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 plain copy print ?
- /**Amagicfilenamethatisallowedtoexistwithinthesystemcache*/
- publicstaticfinalStringKNOWN_SPURIOUS_FILENAME="lost+found";
- /**Amagicfilenamethatisallowedtoexistwithinthesystemcache*/
- publicstaticfinalStringRECOVERY_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:
- /**
- *Calledafterbootingtoprocessandremoverecovery-relatedfiles.
- *@returnthelogfilefromrecovery,ornullifnonewasfound.
- *
- *@hide
- */
- publicstaticStringhandleAftermath(){
- //RecordthetailoftheLOG_FILE
- Stringlog=null;
- try{
- log=FileUtils.readTextFile(LOG_FILE,-LOG_FILE_MAX_LENGTH,"...\n");
- }catch(FileNotFoundExceptione){
- Log.i(TAG,"Norecoverylogfile");
- }catch(IOExceptione){
- Log.e(TAG,"Errorreadingrecoverylog",e);
- }
- //DeleteeverythinginRECOVERY_DIRexceptthosebeginning
- //withLAST_PREFIX
- String[]names=RECOVERY_DIR.list();
- for(inti=0;names!=null&&i<names.length;i++){
- if(names[i].startsWith(LAST_PREFIX))continue;
- Filef=newFile(RECOVERY_DIR,names[i]);
- if(!f.delete()){
- Log.e(TAG,"Can'tdelete:"+f);
- }else{
- Log.i(TAG,"Deleted:"+f);
- }
- }
- returnlog;
- }
/** * 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 plain copy print ?
- privatestaticStringLAST_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
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Android之——AIDL小结
- android(NDK+JNI)---NDK编译生成so文件
- android java.lang.IllegalArgumentException contains a path s
- android选择图片或拍照图片上传到服务器(包括上传参数)
- Android(安卓)App程序结构