mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
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:
committed by
Fredrik Fornwall
parent
d1f0c76db3
commit
b7864d6ac2
@@ -209,18 +209,18 @@ final class TermuxInstaller {
|
|||||||
Arrays.toString(Build.SUPPORTED_ABIS));
|
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 {
|
static void deleteFolder(File fileOrDirectory) throws IOException {
|
||||||
File[] children = fileOrDirectory.listFiles();
|
if (fileOrDirectory.getCanonicalPath().equals(fileOrDirectory.getAbsolutePath()) && fileOrDirectory.isDirectory()) {
|
||||||
if (children != null) {
|
File[] children = fileOrDirectory.listFiles();
|
||||||
for (File child : children) {
|
|
||||||
if (child.getCanonicalFile().equals(child.getAbsoluteFile())) {
|
if (children != null) {
|
||||||
|
for (File child : children) {
|
||||||
deleteFolder(child);
|
deleteFolder(child);
|
||||||
} else {
|
|
||||||
child.delete();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileOrDirectory.delete()) {
|
if (!fileOrDirectory.delete()) {
|
||||||
throw new RuntimeException("Unable to delete " + (fileOrDirectory.isDirectory() ? "directory " : "file ") + fileOrDirectory.getAbsolutePath());
|
throw new RuntimeException("Unable to delete " + (fileOrDirectory.isDirectory() ? "directory " : "file ") + fileOrDirectory.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user