diff options
| author | Jeff Sharkey <jsharkey@android.com> | 2014-01-10 16:27:19 -0800 |
|---|---|---|
| committer | Jeff Sharkey <jsharkey@android.com> | 2014-01-15 17:00:56 -0800 |
| commit | 4ca728c064aeab644f6d044e0285eaa056818b8a (patch) | |
| tree | 338bfa8e27e1ab4a6ac5834bcaba13332c41d71a /core/java/android/os/FileUtils.java | |
| parent | ac6b332764264b1a940a3037434934582b9a9f68 (diff) | |
Detect removable and emulated secondary storage.
Also rename existing secondary storage API to match naming
convention in rest of class.
Bug: 11536709
Change-Id: I2684c817de4982b414893d2d9927a21e3f171d53
Diffstat (limited to 'core/java/android/os/FileUtils.java')
| -rw-r--r-- | core/java/android/os/FileUtils.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java index 2d60df0e19dc..15a154aba257 100644 --- a/core/java/android/os/FileUtils.java +++ b/core/java/android/os/FileUtils.java @@ -355,4 +355,26 @@ public class FileUtils { } } } + + /** + * Test if a file lives under the given directory, either as a direct child + * or a distant grandchild. + * <p> + * Both files <em>must</em> have been resolved using + * {@link File#getCanonicalFile()} to avoid symlink or path traversal + * attacks. + */ + public static boolean contains(File dir, File file) { + String dirPath = dir.getPath(); + String filePath = file.getPath(); + + if (dirPath.equals(filePath)) { + return true; + } + + if (!dirPath.endsWith("/")) { + dirPath += "/"; + } + return filePath.startsWith(dirPath); + } } |
