The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Jeff Sharkey | 85ced63 | 2015-07-22 11:11:46 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 21 | import android.provider.DocumentsContract.Document; |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 22 | import android.system.ErrnoException; |
| 23 | import android.system.Os; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 24 | import android.system.StructStat; |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 27 | import android.util.Slog; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 28 | import android.webkit.MimeTypeMap; |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 29 | |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
| 31 | |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 32 | import libcore.util.EmptyArray; |
| 33 | |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 34 | import java.io.BufferedInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import java.io.ByteArrayOutputStream; |
| 36 | import java.io.File; |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 37 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import java.io.FileInputStream; |
Wink Saville | 6d25a99 | 2011-06-03 17:03:51 -0700 | [diff] [blame] | 39 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import java.io.FileOutputStream; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 41 | import java.io.FileWriter; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 42 | import java.io.FilenameFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import java.io.IOException; |
| 44 | import java.io.InputStream; |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 45 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 46 | import java.util.Arrays; |
| 47 | import java.util.Comparator; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 48 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import java.util.regex.Pattern; |
Wink Saville | 6d25a99 | 2011-06-03 17:03:51 -0700 | [diff] [blame] | 50 | import java.util.zip.CRC32; |
| 51 | import java.util.zip.CheckedInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Tools for managing files. Not for public consumption. |
| 55 | * @hide |
| 56 | */ |
Wink Saville | 6d25a99 | 2011-06-03 17:03:51 -0700 | [diff] [blame] | 57 | public class FileUtils { |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 58 | private static final String TAG = "FileUtils"; |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | public static final int S_IRWXU = 00700; |
| 61 | public static final int S_IRUSR = 00400; |
| 62 | public static final int S_IWUSR = 00200; |
| 63 | public static final int S_IXUSR = 00100; |
| 64 | |
| 65 | public static final int S_IRWXG = 00070; |
| 66 | public static final int S_IRGRP = 00040; |
| 67 | public static final int S_IWGRP = 00020; |
| 68 | public static final int S_IXGRP = 00010; |
| 69 | |
| 70 | public static final int S_IRWXO = 00007; |
| 71 | public static final int S_IROTH = 00004; |
| 72 | public static final int S_IWOTH = 00002; |
| 73 | public static final int S_IXOTH = 00001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
Andreas Gampe | 0693fd8 | 2016-04-28 19:33:05 -0700 | [diff] [blame] | 75 | /** Regular expression for safe filenames: no spaces or metacharacters. |
| 76 | * |
| 77 | * Use a preload holder so that FileUtils can be compile-time initialized. |
| 78 | */ |
| 79 | private static class NoImagePreloadHolder { |
| 80 | public static final Pattern SAFE_FILENAME_PATTERN = Pattern.compile("[\\w%+,./=_-]+"); |
| 81 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
Jeff Sharkey | 85ced63 | 2015-07-22 11:11:46 -0700 | [diff] [blame] | 83 | private static final File[] EMPTY = new File[0]; |
| 84 | |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Set owner and mode of of given {@link File}. |
| 87 | * |
| 88 | * @param mode to apply through {@code chmod} |
| 89 | * @param uid to apply through {@code chown}, or -1 to leave unchanged |
| 90 | * @param gid to apply through {@code chown}, or -1 to leave unchanged |
| 91 | * @return 0 on success, otherwise errno. |
| 92 | */ |
| 93 | public static int setPermissions(File path, int mode, int uid, int gid) { |
| 94 | return setPermissions(path.getAbsolutePath(), mode, uid, gid); |
| 95 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Set owner and mode of of given path. |
| 99 | * |
| 100 | * @param mode to apply through {@code chmod} |
| 101 | * @param uid to apply through {@code chown}, or -1 to leave unchanged |
| 102 | * @param gid to apply through {@code chown}, or -1 to leave unchanged |
| 103 | * @return 0 on success, otherwise errno. |
| 104 | */ |
| 105 | public static int setPermissions(String path, int mode, int uid, int gid) { |
| 106 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 107 | Os.chmod(path, mode); |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 108 | } catch (ErrnoException e) { |
| 109 | Slog.w(TAG, "Failed to chmod(" + path + "): " + e); |
| 110 | return e.errno; |
| 111 | } |
| 112 | |
| 113 | if (uid >= 0 || gid >= 0) { |
| 114 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 115 | Os.chown(path, uid, gid); |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 116 | } catch (ErrnoException e) { |
| 117 | Slog.w(TAG, "Failed to chown(" + path + "): " + e); |
| 118 | return e.errno; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set owner and mode of of given {@link FileDescriptor}. |
| 127 | * |
| 128 | * @param mode to apply through {@code chmod} |
| 129 | * @param uid to apply through {@code chown}, or -1 to leave unchanged |
| 130 | * @param gid to apply through {@code chown}, or -1 to leave unchanged |
| 131 | * @return 0 on success, otherwise errno. |
| 132 | */ |
| 133 | public static int setPermissions(FileDescriptor fd, int mode, int uid, int gid) { |
| 134 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 135 | Os.fchmod(fd, mode); |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 136 | } catch (ErrnoException e) { |
| 137 | Slog.w(TAG, "Failed to fchmod(): " + e); |
| 138 | return e.errno; |
| 139 | } |
| 140 | |
| 141 | if (uid >= 0 || gid >= 0) { |
| 142 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 143 | Os.fchown(fd, uid, gid); |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 144 | } catch (ErrnoException e) { |
| 145 | Slog.w(TAG, "Failed to fchown(): " + e); |
| 146 | return e.errno; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 153 | public static void copyPermissions(File from, File to) throws IOException { |
| 154 | try { |
| 155 | final StructStat stat = Os.stat(from.getAbsolutePath()); |
| 156 | Os.chmod(to.getAbsolutePath(), stat.st_mode); |
| 157 | Os.chown(to.getAbsolutePath(), stat.st_uid, stat.st_gid); |
| 158 | } catch (ErrnoException e) { |
| 159 | throw e.rethrowAsIOException(); |
| 160 | } |
| 161 | } |
| 162 | |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Return owning UID of given path, otherwise -1. |
| 165 | */ |
| 166 | public static int getUid(String path) { |
| 167 | try { |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 168 | return Os.stat(path).st_uid; |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 169 | } catch (ErrnoException e) { |
| 170 | return -1; |
| 171 | } |
| 172 | } |
Dianne Hackborn | 053f61d | 2013-06-26 18:07:43 -0700 | [diff] [blame] | 173 | |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Perform an fsync on the given FileOutputStream. The stream at this |
| 176 | * point must be flushed but not yet closed. |
| 177 | */ |
| 178 | public static boolean sync(FileOutputStream stream) { |
| 179 | try { |
| 180 | if (stream != null) { |
| 181 | stream.getFD().sync(); |
| 182 | } |
| 183 | return true; |
| 184 | } catch (IOException e) { |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 189 | @Deprecated |
| 190 | public static boolean copyFile(File srcFile, File destFile) { |
| 191 | try { |
| 192 | copyFileOrThrow(srcFile, destFile); |
| 193 | return true; |
| 194 | } catch (IOException e) { |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | // copy a file from srcFile to destFile, return true if succeed, return |
| 200 | // false if fail |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 201 | public static void copyFileOrThrow(File srcFile, File destFile) throws IOException { |
| 202 | try (InputStream in = new FileInputStream(srcFile)) { |
| 203 | copyToFileOrThrow(in, destFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Deprecated |
| 208 | public static boolean copyToFile(InputStream inputStream, File destFile) { |
| 209 | try { |
| 210 | copyToFileOrThrow(inputStream, destFile); |
| 211 | return true; |
| 212 | } catch (IOException e) { |
| 213 | return false; |
| 214 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Copy data from a source stream to destFile. |
| 219 | * Return true if succeed, return false if failed. |
| 220 | */ |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 221 | public static void copyToFileOrThrow(InputStream inputStream, File destFile) |
| 222 | throws IOException { |
| 223 | if (destFile.exists()) { |
| 224 | destFile.delete(); |
| 225 | } |
| 226 | FileOutputStream out = new FileOutputStream(destFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | try { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 228 | byte[] buffer = new byte[4096]; |
| 229 | int bytesRead; |
| 230 | while ((bytesRead = inputStream.read(buffer)) >= 0) { |
| 231 | out.write(buffer, 0, bytesRead); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 232 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 233 | } finally { |
| 234 | out.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | try { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 236 | out.getFD().sync(); |
| 237 | } catch (IOException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 239 | out.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Check if a filename is "safe" (no metacharacters or spaces). |
| 245 | * @param file The file to check |
| 246 | */ |
| 247 | public static boolean isFilenameSafe(File file) { |
| 248 | // Note, we check whether it matches what's known to be safe, |
| 249 | // rather than what's known to be unsafe. Non-ASCII, control |
| 250 | // characters, etc. are all unsafe by default. |
Andreas Gampe | 0693fd8 | 2016-04-28 19:33:05 -0700 | [diff] [blame] | 251 | return NoImagePreloadHolder.SAFE_FILENAME_PATTERN.matcher(file.getPath()).matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Read a text file into a String, optionally limiting the length. |
| 256 | * @param file to read (will not seek, so things like /proc files are OK) |
| 257 | * @param max length (positive for head, negative of tail, 0 for no limit) |
| 258 | * @param ellipsis to add of the file was truncated (can be null) |
| 259 | * @return the contents of the file, possibly truncated |
| 260 | * @throws IOException if something goes wrong reading the file |
| 261 | */ |
| 262 | public static String readTextFile(File file, int max, String ellipsis) throws IOException { |
| 263 | InputStream input = new FileInputStream(file); |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 264 | // wrapping a BufferedInputStream around it because when reading /proc with unbuffered |
| 265 | // input stream, bytes read not equal to buffer size is not necessarily the correct |
| 266 | // indication for EOF; but it is true for BufferedInputStream due to its implementation. |
| 267 | BufferedInputStream bis = new BufferedInputStream(input); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 269 | long size = file.length(); |
| 270 | if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the first N bytes |
| 271 | if (size > 0 && (max == 0 || size < max)) max = (int) size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | byte[] data = new byte[max + 1]; |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 273 | int length = bis.read(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | if (length <= 0) return ""; |
| 275 | if (length <= max) return new String(data, 0, length); |
| 276 | if (ellipsis == null) return new String(data, 0, max); |
| 277 | return new String(data, 0, max) + ellipsis; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 278 | } else if (max < 0) { // "tail" mode: keep the last N |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | int len; |
| 280 | boolean rolled = false; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 281 | byte[] last = null; |
| 282 | byte[] data = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | do { |
| 284 | if (last != null) rolled = true; |
| 285 | byte[] tmp = last; last = data; data = tmp; |
| 286 | if (data == null) data = new byte[-max]; |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 287 | len = bis.read(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } while (len == data.length); |
| 289 | |
| 290 | if (last == null && len <= 0) return ""; |
| 291 | if (last == null) return new String(data, 0, len); |
| 292 | if (len > 0) { |
| 293 | rolled = true; |
| 294 | System.arraycopy(last, len, last, 0, last.length - len); |
| 295 | System.arraycopy(data, 0, last, last.length - len, len); |
| 296 | } |
| 297 | if (ellipsis == null || !rolled) return new String(last); |
| 298 | return ellipsis + new String(last); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 299 | } else { // "cat" mode: size unknown, read it all in streaming fashion |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | ByteArrayOutputStream contents = new ByteArrayOutputStream(); |
| 301 | int len; |
| 302 | byte[] data = new byte[1024]; |
| 303 | do { |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 304 | len = bis.read(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | if (len > 0) contents.write(data, 0, len); |
| 306 | } while (len == data.length); |
| 307 | return contents.toString(); |
| 308 | } |
| 309 | } finally { |
Guang Zhu | 9061981 | 2012-10-12 15:50:44 -0700 | [diff] [blame] | 310 | bis.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | input.close(); |
| 312 | } |
| 313 | } |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 314 | |
Jeff Sharkey | 2271ba3 | 2016-02-01 17:57:08 -0700 | [diff] [blame] | 315 | public static void stringToFile(File file, String string) throws IOException { |
| 316 | stringToFile(file.getAbsolutePath(), string); |
| 317 | } |
| 318 | |
Narayan Kamath | 6d051fc | 2016-11-22 20:20:00 +0000 | [diff] [blame] | 319 | /* |
| 320 | * Writes the bytes given in {@code content} to the file whose absolute path |
| 321 | * is {@code filename}. |
| 322 | */ |
| 323 | public static void bytesToFile(String filename, byte[] content) throws IOException { |
| 324 | try (FileOutputStream fos = new FileOutputStream(filename)) { |
| 325 | fos.write(content); |
| 326 | } |
| 327 | } |
| 328 | |
Jeff Sharkey | 2271ba3 | 2016-02-01 17:57:08 -0700 | [diff] [blame] | 329 | /** |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 330 | * Writes string to file. Basically same as "echo -n $string > $filename" |
| 331 | * |
| 332 | * @param filename |
| 333 | * @param string |
| 334 | * @throws IOException |
| 335 | */ |
| 336 | public static void stringToFile(String filename, String string) throws IOException { |
| 337 | FileWriter out = new FileWriter(filename); |
| 338 | try { |
| 339 | out.write(string); |
| 340 | } finally { |
| 341 | out.close(); |
| 342 | } |
| 343 | } |
Wink Saville | 1b9a6a6 | 2011-06-04 07:31:35 -0700 | [diff] [blame] | 344 | |
Wink Saville | 6d25a99 | 2011-06-03 17:03:51 -0700 | [diff] [blame] | 345 | /** |
| 346 | * Computes the checksum of a file using the CRC32 checksum routine. |
| 347 | * The value of the checksum is returned. |
| 348 | * |
| 349 | * @param file the file to checksum, must not be null |
| 350 | * @return the checksum value or an exception is thrown. |
| 351 | */ |
| 352 | public static long checksumCrc32(File file) throws FileNotFoundException, IOException { |
| 353 | CRC32 checkSummer = new CRC32(); |
| 354 | CheckedInputStream cis = null; |
| 355 | |
| 356 | try { |
| 357 | cis = new CheckedInputStream( new FileInputStream(file), checkSummer); |
| 358 | byte[] buf = new byte[128]; |
| 359 | while(cis.read(buf) >= 0) { |
| 360 | // Just read for checksum to get calculated. |
| 361 | } |
| 362 | return checkSummer.getValue(); |
| 363 | } finally { |
| 364 | if (cis != null) { |
| 365 | try { |
| 366 | cis.close(); |
| 367 | } catch (IOException e) { |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 372 | |
| 373 | /** |
| 374 | * Delete older files in a directory until only those matching the given |
| 375 | * constraints remain. |
| 376 | * |
| 377 | * @param minCount Always keep at least this many files. |
| 378 | * @param minAge Always keep files younger than this age. |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 379 | * @return if any files were deleted. |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 380 | */ |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 381 | public static boolean deleteOlderFiles(File dir, int minCount, long minAge) { |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 382 | if (minCount < 0 || minAge < 0) { |
| 383 | throw new IllegalArgumentException("Constraints must be positive or 0"); |
| 384 | } |
| 385 | |
| 386 | final File[] files = dir.listFiles(); |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 387 | if (files == null) return false; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 388 | |
| 389 | // Sort with newest files first |
| 390 | Arrays.sort(files, new Comparator<File>() { |
| 391 | @Override |
| 392 | public int compare(File lhs, File rhs) { |
Ian Rogers | 660e6de | 2016-05-17 11:50:54 -0700 | [diff] [blame] | 393 | return Long.compare(rhs.lastModified(), lhs.lastModified()); |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 394 | } |
| 395 | }); |
| 396 | |
| 397 | // Keep at least minCount files |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 398 | boolean deleted = false; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 399 | for (int i = minCount; i < files.length; i++) { |
| 400 | final File file = files[i]; |
| 401 | |
| 402 | // Keep files newer than minAge |
| 403 | final long age = System.currentTimeMillis() - file.lastModified(); |
| 404 | if (age > minAge) { |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 405 | if (file.delete()) { |
| 406 | Log.d(TAG, "Deleted old file " + file); |
| 407 | deleted = true; |
| 408 | } |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
Jeff Sharkey | ebf8ad5 | 2014-01-30 15:01:22 -0800 | [diff] [blame] | 411 | return deleted; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 412 | } |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * Test if a file lives under the given directory, either as a direct child |
| 416 | * or a distant grandchild. |
| 417 | * <p> |
| 418 | * Both files <em>must</em> have been resolved using |
| 419 | * {@link File#getCanonicalFile()} to avoid symlink or path traversal |
| 420 | * attacks. |
| 421 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 422 | public static boolean contains(File[] dirs, File file) { |
| 423 | for (File dir : dirs) { |
| 424 | if (contains(dir, file)) { |
| 425 | return true; |
| 426 | } |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Test if a file lives under the given directory, either as a direct child |
| 433 | * or a distant grandchild. |
| 434 | * <p> |
| 435 | * Both files <em>must</em> have been resolved using |
| 436 | * {@link File#getCanonicalFile()} to avoid symlink or path traversal |
| 437 | * attacks. |
| 438 | */ |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 439 | public static boolean contains(File dir, File file) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 440 | if (dir == null || file == null) return false; |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 441 | |
Jeff Sharkey | 21de56a | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 442 | String dirPath = dir.getAbsolutePath(); |
| 443 | String filePath = file.getAbsolutePath(); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 444 | |
| 445 | if (dirPath.equals(filePath)) { |
| 446 | return true; |
| 447 | } |
| 448 | |
| 449 | if (!dirPath.endsWith("/")) { |
| 450 | dirPath += "/"; |
| 451 | } |
| 452 | return filePath.startsWith(dirPath); |
| 453 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 454 | |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 455 | public static boolean deleteContentsAndDir(File dir) { |
| 456 | if (deleteContents(dir)) { |
| 457 | return dir.delete(); |
| 458 | } else { |
| 459 | return false; |
| 460 | } |
| 461 | } |
| 462 | |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 463 | public static boolean deleteContents(File dir) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 464 | File[] files = dir.listFiles(); |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 465 | boolean success = true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 466 | if (files != null) { |
| 467 | for (File file : files) { |
| 468 | if (file.isDirectory()) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 469 | success &= deleteContents(file); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 470 | } |
Jeff Sharkey | 73767b9 | 2014-07-04 20:18:13 -0700 | [diff] [blame] | 471 | if (!file.delete()) { |
| 472 | Log.w(TAG, "Failed to delete " + file); |
| 473 | success = false; |
| 474 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 477 | return success; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 480 | private static boolean isValidExtFilenameChar(char c) { |
| 481 | switch (c) { |
| 482 | case '\0': |
| 483 | case '/': |
| 484 | return false; |
| 485 | default: |
| 486 | return true; |
| 487 | } |
| 488 | } |
| 489 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 490 | /** |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 491 | * Check if given filename is valid for an ext4 filesystem. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 492 | */ |
| 493 | public static boolean isValidExtFilename(String name) { |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 494 | return (name != null) && name.equals(buildValidExtFilename(name)); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Mutate the given filename to make it valid for an ext4 filesystem, |
| 499 | * replacing any invalid characters with "_". |
| 500 | */ |
| 501 | public static String buildValidExtFilename(String name) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 502 | if (TextUtils.isEmpty(name) || ".".equals(name) || "..".equals(name)) { |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 503 | return "(invalid)"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 504 | } |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 505 | final StringBuilder res = new StringBuilder(name.length()); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 506 | for (int i = 0; i < name.length(); i++) { |
| 507 | final char c = name.charAt(i); |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 508 | if (isValidExtFilenameChar(c)) { |
| 509 | res.append(c); |
| 510 | } else { |
| 511 | res.append('_'); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 514 | trimFilename(res, 255); |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 515 | return res.toString(); |
| 516 | } |
| 517 | |
| 518 | private static boolean isValidFatFilenameChar(char c) { |
| 519 | if ((0x00 <= c && c <= 0x1f)) { |
| 520 | return false; |
| 521 | } |
| 522 | switch (c) { |
| 523 | case '"': |
| 524 | case '*': |
| 525 | case '/': |
| 526 | case ':': |
| 527 | case '<': |
| 528 | case '>': |
| 529 | case '?': |
| 530 | case '\\': |
| 531 | case '|': |
| 532 | case 0x7F: |
| 533 | return false; |
| 534 | default: |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Check if given filename is valid for a FAT filesystem. |
| 541 | */ |
| 542 | public static boolean isValidFatFilename(String name) { |
| 543 | return (name != null) && name.equals(buildValidFatFilename(name)); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Mutate the given filename to make it valid for a FAT filesystem, |
| 548 | * replacing any invalid characters with "_". |
| 549 | */ |
| 550 | public static String buildValidFatFilename(String name) { |
| 551 | if (TextUtils.isEmpty(name) || ".".equals(name) || "..".equals(name)) { |
| 552 | return "(invalid)"; |
| 553 | } |
| 554 | final StringBuilder res = new StringBuilder(name.length()); |
| 555 | for (int i = 0; i < name.length(); i++) { |
| 556 | final char c = name.charAt(i); |
| 557 | if (isValidFatFilenameChar(c)) { |
| 558 | res.append(c); |
| 559 | } else { |
| 560 | res.append('_'); |
| 561 | } |
| 562 | } |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 563 | // Even though vfat allows 255 UCS-2 chars, we might eventually write to |
| 564 | // ext4 through a FUSE layer, so use that limit. |
| 565 | trimFilename(res, 255); |
Jeff Sharkey | 0cce535 | 2014-11-26 13:38:26 -0800 | [diff] [blame] | 566 | return res.toString(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 567 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 568 | |
Jeff Sharkey | 4f5e8b3 | 2015-06-11 19:13:37 -0700 | [diff] [blame] | 569 | @VisibleForTesting |
| 570 | public static String trimFilename(String str, int maxBytes) { |
| 571 | final StringBuilder res = new StringBuilder(str); |
| 572 | trimFilename(res, maxBytes); |
| 573 | return res.toString(); |
| 574 | } |
| 575 | |
| 576 | private static void trimFilename(StringBuilder res, int maxBytes) { |
| 577 | byte[] raw = res.toString().getBytes(StandardCharsets.UTF_8); |
| 578 | if (raw.length > maxBytes) { |
| 579 | maxBytes -= 3; |
| 580 | while (raw.length > maxBytes) { |
| 581 | res.deleteCharAt(res.length() / 2); |
| 582 | raw = res.toString().getBytes(StandardCharsets.UTF_8); |
| 583 | } |
| 584 | res.insert(res.length() / 2, "..."); |
| 585 | } |
| 586 | } |
| 587 | |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 588 | public static String rewriteAfterRename(File beforeDir, File afterDir, String path) { |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 589 | if (path == null) return null; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 590 | final File result = rewriteAfterRename(beforeDir, afterDir, new File(path)); |
| 591 | return (result != null) ? result.getAbsolutePath() : null; |
| 592 | } |
| 593 | |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 594 | public static String[] rewriteAfterRename(File beforeDir, File afterDir, String[] paths) { |
| 595 | if (paths == null) return null; |
| 596 | final String[] result = new String[paths.length]; |
| 597 | for (int i = 0; i < paths.length; i++) { |
| 598 | result[i] = rewriteAfterRename(beforeDir, afterDir, paths[i]); |
| 599 | } |
| 600 | return result; |
| 601 | } |
| 602 | |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 603 | /** |
| 604 | * Given a path under the "before" directory, rewrite it to live under the |
| 605 | * "after" directory. For example, {@code /before/foo/bar.txt} would become |
| 606 | * {@code /after/foo/bar.txt}. |
| 607 | */ |
| 608 | public static File rewriteAfterRename(File beforeDir, File afterDir, File file) { |
Jeff Sharkey | 41be35d | 2015-05-13 12:38:16 -0700 | [diff] [blame] | 609 | if (file == null || beforeDir == null || afterDir == null) return null; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 610 | if (contains(beforeDir, file)) { |
| 611 | final String splice = file.getAbsolutePath().substring( |
| 612 | beforeDir.getAbsolutePath().length()); |
| 613 | return new File(afterDir, splice); |
| 614 | } |
| 615 | return null; |
| 616 | } |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 617 | |
Jeff Sharkey | aa44476 | 2016-09-20 18:54:46 -0600 | [diff] [blame] | 618 | private static File buildUniqueFileWithExtension(File parent, String name, String ext) |
| 619 | throws FileNotFoundException { |
| 620 | File file = buildFile(parent, name, ext); |
| 621 | |
| 622 | // If conflicting file, try adding counter suffix |
| 623 | int n = 0; |
| 624 | while (file.exists()) { |
| 625 | if (n++ >= 32) { |
| 626 | throw new FileNotFoundException("Failed to create unique file"); |
| 627 | } |
| 628 | file = buildFile(parent, name + " (" + n + ")", ext); |
| 629 | } |
| 630 | |
| 631 | return file; |
| 632 | } |
| 633 | |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 634 | /** |
| 635 | * Generates a unique file name under the given parent directory. If the display name doesn't |
| 636 | * have an extension that matches the requested MIME type, the default extension for that MIME |
| 637 | * type is appended. If a file already exists, the name is appended with a numerical value to |
| 638 | * make it unique. |
| 639 | * |
| 640 | * For example, the display name 'example' with 'text/plain' MIME might produce |
| 641 | * 'example.txt' or 'example (1).txt', etc. |
| 642 | * |
| 643 | * @throws FileNotFoundException |
| 644 | */ |
| 645 | public static File buildUniqueFile(File parent, String mimeType, String displayName) |
| 646 | throws FileNotFoundException { |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 647 | final String[] parts = splitFileName(mimeType, displayName); |
Jeff Sharkey | aa44476 | 2016-09-20 18:54:46 -0600 | [diff] [blame] | 648 | return buildUniqueFileWithExtension(parent, parts[0], parts[1]); |
| 649 | } |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 650 | |
Jeff Sharkey | aa44476 | 2016-09-20 18:54:46 -0600 | [diff] [blame] | 651 | /** |
| 652 | * Generates a unique file name under the given parent directory, keeping |
| 653 | * any extension intact. |
| 654 | */ |
| 655 | public static File buildUniqueFile(File parent, String displayName) |
| 656 | throws FileNotFoundException { |
| 657 | final String name; |
| 658 | final String ext; |
| 659 | |
| 660 | // Extract requested extension from display name |
| 661 | final int lastDot = displayName.lastIndexOf('.'); |
| 662 | if (lastDot >= 0) { |
| 663 | name = displayName.substring(0, lastDot); |
| 664 | ext = displayName.substring(lastDot + 1); |
| 665 | } else { |
| 666 | name = displayName; |
| 667 | ext = null; |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 668 | } |
| 669 | |
Jeff Sharkey | aa44476 | 2016-09-20 18:54:46 -0600 | [diff] [blame] | 670 | return buildUniqueFileWithExtension(parent, name, ext); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Splits file name into base name and extension. |
| 675 | * If the display name doesn't have an extension that matches the requested MIME type, the |
| 676 | * extension is regarded as a part of filename and default extension for that MIME type is |
| 677 | * appended. |
| 678 | */ |
| 679 | public static String[] splitFileName(String mimeType, String displayName) { |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 680 | String name; |
| 681 | String ext; |
| 682 | |
| 683 | if (Document.MIME_TYPE_DIR.equals(mimeType)) { |
| 684 | name = displayName; |
| 685 | ext = null; |
| 686 | } else { |
| 687 | String mimeTypeFromExt; |
| 688 | |
| 689 | // Extract requested extension from display name |
| 690 | final int lastDot = displayName.lastIndexOf('.'); |
| 691 | if (lastDot >= 0) { |
| 692 | name = displayName.substring(0, lastDot); |
| 693 | ext = displayName.substring(lastDot + 1); |
| 694 | mimeTypeFromExt = MimeTypeMap.getSingleton().getMimeTypeFromExtension( |
| 695 | ext.toLowerCase()); |
| 696 | } else { |
| 697 | name = displayName; |
| 698 | ext = null; |
| 699 | mimeTypeFromExt = null; |
| 700 | } |
| 701 | |
| 702 | if (mimeTypeFromExt == null) { |
| 703 | mimeTypeFromExt = "application/octet-stream"; |
| 704 | } |
| 705 | |
| 706 | final String extFromMimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType( |
| 707 | mimeType); |
| 708 | if (Objects.equals(mimeType, mimeTypeFromExt) || Objects.equals(ext, extFromMimeType)) { |
| 709 | // Extension maps back to requested MIME type; allow it |
| 710 | } else { |
| 711 | // No match; insist that create file matches requested MIME |
| 712 | name = displayName; |
| 713 | ext = extFromMimeType; |
| 714 | } |
| 715 | } |
| 716 | |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 717 | if (ext == null) { |
| 718 | ext = ""; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 721 | return new String[] { name, ext }; |
Ben Kwa | 62539a2 | 2015-04-22 15:43:17 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | private static File buildFile(File parent, String name, String ext) { |
| 725 | if (TextUtils.isEmpty(ext)) { |
| 726 | return new File(parent, name); |
| 727 | } else { |
| 728 | return new File(parent, name + "." + ext); |
| 729 | } |
| 730 | } |
Jeff Sharkey | 85ced63 | 2015-07-22 11:11:46 -0700 | [diff] [blame] | 731 | |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 732 | public static @NonNull String[] listOrEmpty(@Nullable File dir) { |
| 733 | if (dir == null) return EmptyArray.STRING; |
| 734 | final String[] res = dir.list(); |
| 735 | if (res != null) { |
| 736 | return res; |
| 737 | } else { |
| 738 | return EmptyArray.STRING; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | public static @NonNull File[] listFilesOrEmpty(@Nullable File dir) { |
| 743 | if (dir == null) return EMPTY; |
| 744 | final File[] res = dir.listFiles(); |
Jeff Sharkey | 85ced63 | 2015-07-22 11:11:46 -0700 | [diff] [blame] | 745 | if (res != null) { |
| 746 | return res; |
| 747 | } else { |
| 748 | return EMPTY; |
| 749 | } |
| 750 | } |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 751 | |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 752 | public static @NonNull File[] listFilesOrEmpty(@Nullable File dir, FilenameFilter filter) { |
| 753 | if (dir == null) return EMPTY; |
| 754 | final File[] res = dir.listFiles(filter); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 755 | if (res != null) { |
| 756 | return res; |
| 757 | } else { |
| 758 | return EMPTY; |
| 759 | } |
| 760 | } |
| 761 | |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 762 | public static @Nullable File newFileOrNull(@Nullable String path) { |
| 763 | return (path != null) ? new File(path) : null; |
| 764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |