diff options
author | Narayan Kamath <narayan@google.com> | 2014-05-01 13:50:35 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-05-01 13:50:36 +0000 |
commit | b916d8adffd7ea3588bc178e1ee03f68f0a409e5 (patch) | |
tree | e79a96606c6ba0528c24b7a911923b5ac1305366 | |
parent | 41f40581ee0284660d30be8ef28f6bf721c4ce10 (diff) | |
parent | d6d1dbac3f71a292e071dd3108d8333cb5dae44d (diff) | |
download | base-b916d8adffd7ea3588bc178e1ee03f68f0a409e5.tar.gz |
Merge "Implement FileUtils#contains."
-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 411783d1d0b0..3e0b54a4d2cf 100644 --- a/core/java/android/os/FileUtils.java +++ b/core/java/android/os/FileUtils.java @@ -357,4 +357,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.getAbsolutePath(); + String filePath = file.getAbsolutePath(); + + if (dirPath.equals(filePath)) { + return true; + } + + if (!dirPath.endsWith("/")) { + dirPath += "/"; + } + return filePath.startsWith(dirPath); + } } |