Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.util; |
michael@0 | 6 | |
michael@0 | 7 | import android.util.Log; |
michael@0 | 8 | |
michael@0 | 9 | import java.io.File; |
michael@0 | 10 | import java.io.IOException; |
michael@0 | 11 | import java.io.FilenameFilter; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.mozglue.RobocopTarget; |
michael@0 | 14 | |
michael@0 | 15 | public class FileUtils { |
michael@0 | 16 | private static final String LOGTAG= "GeckoFileUtils"; |
michael@0 | 17 | /* |
michael@0 | 18 | * A basic Filter for checking a filename and age. |
michael@0 | 19 | **/ |
michael@0 | 20 | static public class NameAndAgeFilter implements FilenameFilter { |
michael@0 | 21 | final private String mName; |
michael@0 | 22 | final private double mMaxAge; |
michael@0 | 23 | |
michael@0 | 24 | public NameAndAgeFilter(String name, double age) { |
michael@0 | 25 | mName = name; |
michael@0 | 26 | mMaxAge = age; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | @Override |
michael@0 | 30 | public boolean accept(File dir, String filename) { |
michael@0 | 31 | if (mName == null || mName.matches(filename)) { |
michael@0 | 32 | File f = new File(dir, filename); |
michael@0 | 33 | |
michael@0 | 34 | if (mMaxAge < 0 || System.currentTimeMillis() - f.lastModified() > mMaxAge) { |
michael@0 | 35 | return true; |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | return false; |
michael@0 | 40 | } |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | @RobocopTarget |
michael@0 | 44 | public static void delTree(File dir, FilenameFilter filter, boolean recurse) { |
michael@0 | 45 | String[] files = null; |
michael@0 | 46 | |
michael@0 | 47 | if (filter != null) { |
michael@0 | 48 | files = dir.list(filter); |
michael@0 | 49 | } else { |
michael@0 | 50 | files = dir.list(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | if (files == null) { |
michael@0 | 54 | return; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | for (String file : files) { |
michael@0 | 58 | File f = new File(dir, file); |
michael@0 | 59 | delete(f, recurse); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | public static boolean delete(File file) throws IOException { |
michael@0 | 64 | return delete(file, true); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | public static boolean delete(File file, boolean recurse) { |
michael@0 | 68 | if (file.isDirectory() && recurse) { |
michael@0 | 69 | // If the quick delete failed and this is a dir, recursively delete the contents of the dir |
michael@0 | 70 | String files[] = file.list(); |
michael@0 | 71 | for (String temp : files) { |
michael@0 | 72 | File fileDelete = new File(file, temp); |
michael@0 | 73 | try { |
michael@0 | 74 | delete(fileDelete); |
michael@0 | 75 | } catch(IOException ex) { |
michael@0 | 76 | Log.i(LOGTAG, "Error deleting " + fileDelete.getPath(), ex); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // Even if this is a dir, it should now be empty and delete should work |
michael@0 | 82 | return file.delete(); |
michael@0 | 83 | } |
michael@0 | 84 | } |