deleteFolder(): check if passed argument is a symlink

Prevents possible data loss when user replaced directory '~/storage' with
a symlink.
This commit is contained in:
Leonid Plyushch
2018-06-29 14:43:38 +03:00
committed by Fredrik Fornwall
parent d1f0c76db3
commit b7864d6ac2

View File

@@ -209,18 +209,18 @@ final class TermuxInstaller {
Arrays.toString(Build.SUPPORTED_ABIS));
}
/** Delete a folder and all its content or throw. */
/** Delete a folder and all its content or throw. Don't follow symlinks. */
static void deleteFolder(File fileOrDirectory) throws IOException {
File[] children = fileOrDirectory.listFiles();
if (children != null) {
for (File child : children) {
if (child.getCanonicalFile().equals(child.getAbsoluteFile())) {
if (fileOrDirectory.getCanonicalPath().equals(fileOrDirectory.getAbsolutePath()) && fileOrDirectory.isDirectory()) {
File[] children = fileOrDirectory.listFiles();
if (children != null) {
for (File child : children) {
deleteFolder(child);
} else {
child.delete();
}
}
}
if (!fileOrDirectory.delete()) {
throw new RuntimeException("Unable to delete " + (fileOrDirectory.isDirectory() ? "directory " : "file ") + fileOrDirectory.getAbsolutePath());
}