fixed file deletion in windows

This commit is contained in:
rdavidek 2026-03-01 17:21:57 +01:00
parent 93f73cc084
commit df70ce0584

View File

@ -17,6 +17,7 @@ import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -343,23 +344,54 @@ public class FileOperations {
private static void deleteDirectoryInternal(Path path) throws IOException { private static void deleteDirectoryInternal(Path path) throws IOException {
if (Files.isSymbolicLink(path)) { if (Files.isSymbolicLink(path)) {
Files.delete(path); deletePathWithForce(path);
return; return;
} }
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file); deletePathWithForce(file);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@Override @Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir); deletePathWithForce(dir);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });
} }
private static void deletePathWithForce(Path path) throws IOException {
try {
Files.delete(path);
return;
} catch (AccessDeniedException e) {
clearDeletionBlockingAttributes(path);
}
Files.delete(path);
}
private static void clearDeletionBlockingAttributes(Path path) {
try {
File file = path.toFile();
if (!file.canWrite()) {
file.setWritable(true);
}
} catch (Exception ignore) {}
if (cz.kamma.kfmanager.MainApp.CURRENT_OS != cz.kamma.kfmanager.MainApp.OS.WINDOWS) {
return;
}
try {
DosFileAttributeView dos = Files.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (dos != null) {
dos.setReadOnly(false);
dos.setSystem(false);
}
} catch (Exception ignore) {}
}
/** /**
* Move files/directories to target directory * Move files/directories to target directory
*/ */
@ -494,7 +526,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onProgress(currentItem[0], totalItems, file.getName()); callback.onProgress(currentItem[0], totalItems, file.getName());
} }
Files.delete(file.toPath()); deletePathWithForce(file.toPath());
} else if (file.isDirectory()) { } else if (file.isDirectory()) {
deleteDirectory(file.toPath(), totalItems, currentItem, callback); deleteDirectory(file.toPath(), totalItems, currentItem, callback);
} else { } else {
@ -502,7 +534,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onProgress(currentItem[0], totalItems, file.getName()); callback.onProgress(currentItem[0], totalItems, file.getName());
} }
Files.delete(file.toPath()); deletePathWithForce(file.toPath());
} }
break; break;
} catch (IOException e) { } catch (IOException e) {
@ -615,7 +647,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onProgress(currentItem[0], totalItems, directory.getFileName().toString()); callback.onProgress(currentItem[0], totalItems, directory.getFileName().toString());
} }
Files.delete(directory); deletePathWithForce(directory);
return; return;
} }
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@ -627,7 +659,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onProgress(currentItem[0], totalItems, file.getFileName().toString()); callback.onProgress(currentItem[0], totalItems, file.getFileName().toString());
} }
Files.delete(file); deletePathWithForce(file);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} catch (IOException e) { } catch (IOException e) {
if (callback != null) { if (callback != null) {
@ -650,7 +682,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onProgress(currentItem[0], totalItems, dir.getFileName().toString()); callback.onProgress(currentItem[0], totalItems, dir.getFileName().toString());
} }
Files.delete(dir); deletePathWithForce(dir);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} catch (IOException e) { } catch (IOException e) {
if (callback != null) { if (callback != null) {