From 4ca728c064aeab644f6d044e0285eaa056818b8a Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Fri, 10 Jan 2014 16:27:19 -0800 Subject: 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 --- core/java/android/os/FileUtils.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'core/java/android/os/FileUtils.java') 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. + *

+ * Both files must 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); + } } -- cgit v1.2.3