Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.jackhuang.hmcl.ui.FXUtils.onEscPressed;
Expand Down Expand Up @@ -161,10 +162,25 @@ public void onCreateDirectory() {
return;

Path parent = currentDirectory.path;

Set<String> existingFolders;

try (var list = Files.list(parent)) {
existingFolders = list.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toSet());
} catch (IOException e) {
LOG.warning("Failed to list folders in " + parent, e);
existingFolders = Set.of();
}

Set<String> finalExistingFolders = existingFolders;

Controllers.prompt(
i18n("schematics.create_directory.prompt"),
(result, handler) -> {
Path targetDir = parent.resolve(result);

if (Files.exists(targetDir)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要删除这一处检查?

handler.reject(i18n("schematics.create_directory.failed.already_exists"));
return;
Expand All @@ -178,7 +194,7 @@ public void onCreateDirectory() {
LOG.warning("Failed to create directory: " + targetDir, e);
handler.reject(i18n("schematics.create_directory.failed", targetDir));
}
}, "", new RequiredValidator(), new Validator(i18n("schematics.create_directory.failed.invalid_name"), FileUtils::isNameValid));
}, "", new RequiredValidator(), new Validator(i18n("schematics.create_directory.failed.invalid_name"), FileUtils::isNameValid), new Validator(i18n("schematics.create_directory.failed.already_exists"), (it) -> !finalExistingFolders.contains(it)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recheck directory existence when submitting the name

The validator only checks a case-sensitive snapshot captured before the prompt opens. If another process creates the directory while the dialog is open, or on a case-insensitive filesystem the user enters different casing for an existing name, validation succeeds and Files.createDirectories(targetDir) silently accepts the already-existing directory; the dialog then resolves as though a new folder was created. Retain a live Files.exists(targetDir) check in the submission callback in addition to the interactive validator.

Useful? React with 👍 / 👎.

}

private DirItem loadAll(Path dir, @Nullable DirItem parent) {
Expand Down