Skip to content

Commit

Permalink
deleteFolder(): check if passed argument is a symlink
Browse files Browse the repository at this point in the history
Prevents possible data loss when user replaced directory '~/storage' with
a symlink.
  • Loading branch information
Leonid Plyushch authored and fornwall committed Jul 1, 2018
1 parent d1f0c76 commit b7864d6
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/com/termux/app/TermuxInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,18 @@ private static String determineTermuxArchName() {
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());
}
Expand Down

0 comments on commit b7864d6

Please sign in to comment.