am b916d8ad: Merge "Implement FileUtils#contains."

* commit 'b916d8adffd7ea3588bc178e1ee03f68f0a409e5':
  Implement FileUtils#contains.
This commit is contained in:
Narayan Kamath
2014-05-01 13:52:55 +00:00
committed by Android Git Automerger

View File

@@ -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);
}
}