diff options
| author | 2014-05-01 13:57:13 +0000 | |
|---|---|---|
| committer | 2014-05-01 13:57:13 +0000 | |
| commit | 6af42aea725c2afd8e5fbff9bc33ba353c05e1a9 (patch) | |
| tree | 2ee1031913b31289eb9691058a6b944b9ac07771 | |
| parent | b6c04899e1ed614985d638b55dafe2c20e7b673a (diff) | |
| parent | 9f34234f1c0e39ee43c4a86e23d333ba2a30ec0e (diff) | |
am 9f34234f: am b916d8ad: Merge "Implement FileUtils#contains."
* commit '9f34234f1c0e39ee43c4a86e23d333ba2a30ec0e':
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); + } } |