hansva commented on code in PR #9326:
URL: https://github.com/apache/hop/pull/9326#discussion_r4491227651
##########
ui/src/test/java/org/apache/hop/ui/hopgui/vfs/explorer/VfsFileTransferTest.java:
##########
@@ -311,6 +315,29 @@ void aStoppedBatchKeepsWhatWasNotMoved() throws Exception {
result.getRemaining().stream().map(VfsFileTransfer.Entry::getName).toList());
}
+ @Test
+ void aCaseOnlyLocalNameIsTheSameFile() throws Exception {
Review Comment:
Could you add a test for the symbolic link case, so that `sameFile(link,
target)` is false? Creating a link needs extra privileges on Windows, so skip
the test when it fails:
```java
Path target = Files.writeString(dir.resolve("a.csv"), "precious");
Path link;
try {
link = Files.createSymbolicLink(dir.resolve("link.csv"),
target.getFileName());
} catch (UnsupportedOperationException | IOException e) {
Assumptions.abort("symbolic links are not available: " + e.getMessage());
return;
}
assertFalse(
VfsFileTransfer.sameFile(
HopVfs.getFileObject(link.toString()),
HopVfs.getFileObject(target.toString())));
```
##########
ui/src/main/java/org/apache/hop/ui/hopgui/vfs/explorer/VfsFileTransfer.java:
##########
@@ -460,6 +461,30 @@ public static boolean same(FileObject left, FileObject
right) {
}
}
+ /**
+ * True when both objects name one file. {@link #same} compares the path as
written, so on a
+ * case-insensitive disk {@code data.csv} and {@code Data.csv} are not the
same to it. The local
+ * disk still has one canonical path for that pair, which is what a
letter-case rename is. Two
+ * different files stay different, including on a case-sensitive disk.
+ */
+ static boolean sameFile(FileObject left, FileObject right) {
+ if (same(left, right)) {
+ return true;
+ }
+ try {
+ String leftName = HopVfs.getFilename(left);
+ String rightName = HopVfs.getFilename(right);
+ if (!HopVfs.isLocalFileSystem(leftName) ||
!HopVfs.isLocalFileSystem(rightName)) {
+ return false;
+ }
+ // The canonical path is the on-disk identity. Comparing the File
objects themselves would
+ // ignore case on Windows even when the directory is case-sensitive.
+ return new File(leftName).getCanonicalPath().equals(new
File(rightName).getCanonicalPath());
Review Comment:
**Blocker: this can delete the file a symbolic link points to.**
`getCanonicalPath()` resolves symbolic links, so a link and its target have the
same canonical path and count as one file here. `renameEntry` then calls
`file.moveTo(destination)`. Commons VFS compares the two `File` objects, sees
two different files, and deletes the destination before it renames.
I reproduced it on macOS with the same check as this method:
1. A folder with `a.csv` (content `precious`) and `link.csv`, a symbolic
link to `a.csv`.
2. Rename `link.csv` to `a.csv`.
Result: the rename is allowed, the real `a.csv` is deleted, and `a.csv` is
now a symbolic link to itself that can't be read. Before this change the rename
was refused with "already exists". The same can happen with a case-only rename
on a case-sensitive disk when `Data.csv` is a link to `data.csv`.
Suggestion: only treat the pair as one file when the names differ only in
letter case, neither path is a symbolic link, and the disk says it is the same
file. This also replaces `java.io.File` with `java.nio.file`, which the project
prefers:
```java
static boolean sameFile(FileObject left, FileObject right) {
if (same(left, right)) {
return true;
}
try {
String leftName = HopVfs.getFilename(left);
String rightName = HopVfs.getFilename(right);
if (!leftName.equalsIgnoreCase(rightName)
|| !HopVfs.isLocalFileSystem(leftName)
|| !HopVfs.isLocalFileSystem(rightName)) {
return false;
}
Path leftPath = Path.of(leftName);
Path rightPath = Path.of(rightName);
return !Files.isSymbolicLink(leftPath)
&& !Files.isSymbolicLink(rightPath)
&& Files.isSameFile(leftPath, rightPath);
} catch (Exception ignored) {
return false;
}
}
```
`Files.isSameFile` compares the file identity (the inode on macOS and Linux,
the file index on Windows), so it also handles a folder that is case-sensitive
on Windows.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]