Skip to content

Commit 571a051

Browse files
authored
Fulltext file deleted when being renamed to the name differing just by letter case (JabRef#6123)
Fixes JabRef#6120
1 parent 8dd7444 commit 571a051

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java

+9-15
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,22 @@ public void renameFileToName(String targetFileName) {
223223
}
224224

225225
private void performRenameWithConflictCheck(String targetFileName) {
226-
Optional<Path> fileConflictCheck = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName);
227-
if (fileConflictCheck.isPresent()) {
228-
boolean confirmOverwrite = dialogService.showConfirmationDialogAndWait(
226+
Optional<Path> existingFile = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName);
227+
boolean overwriteFile = false;
228+
229+
if (existingFile.isPresent()) {
230+
overwriteFile = dialogService.showConfirmationDialogAndWait(
229231
Localization.lang("File exists"),
230232
Localization.lang("'%0' exists. Overwrite file?", targetFileName),
231233
Localization.lang("Overwrite"));
232234

233-
if (confirmOverwrite) {
234-
try {
235-
Files.delete(fileConflictCheck.get());
236-
} catch (IOException e) {
237-
dialogService.showErrorDialogAndWait(
238-
Localization.lang("Rename failed"),
239-
Localization.lang("JabRef cannot access the file because it is being used by another process."),
240-
e);
241-
return;
242-
}
243-
} else {
235+
if (!overwriteFile) {
244236
return;
245237
}
246238
}
247239

248240
try {
249-
linkedFileHandler.renameToName(targetFileName);
241+
linkedFileHandler.renameToName(targetFileName, overwriteFile);
250242
} catch (IOException e) {
251243
dialogService.showErrorDialogAndWait(Localization.lang("Rename failed"), Localization.lang("JabRef cannot access the file because it is being used by another process."));
252244
}
@@ -284,6 +276,7 @@ public void moveToDefaultDirectory() {
284276

285277
/**
286278
* Gets the filename for the current linked file and compares it to the new suggested filename.
279+
*
287280
* @return true if the suggested filename is same as current filename.
288281
*/
289282
public boolean isGeneratedNameSameAsOriginal() {
@@ -296,6 +289,7 @@ public boolean isGeneratedNameSameAsOriginal() {
296289

297290
/**
298291
* Compares suggested filepath of current linkedFile with existing filepath.
292+
*
299293
* @return true if suggested filepath is same as existing filepath.
300294
*/
301295
public boolean isGeneratedPathSameAsOriginal() {

src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55
import java.nio.file.Path;
6+
import java.nio.file.StandardCopyOption;
67
import java.util.List;
78
import java.util.Objects;
89
import java.util.Optional;
@@ -71,35 +72,38 @@ public boolean moveToDefaultDirectory() throws IOException {
7172
}
7273

7374
public boolean renameToSuggestedName() throws IOException {
74-
return renameToName(getSuggestedFileName());
75+
return renameToName(getSuggestedFileName(), false);
7576
}
7677

77-
public boolean renameToName(String targetFileName) throws IOException {
78+
public boolean renameToName(String targetFileName, boolean overwriteExistingFile) throws IOException {
7879
Optional<Path> oldFile = fileEntry.findIn(databaseContext, filePreferences);
7980
if (!oldFile.isPresent()) {
80-
// Could not find file
8181
return false;
8282
}
8383

84-
Path newPath = oldFile.get().resolveSibling(targetFileName);
84+
final Path oldPath = oldFile.get();
85+
final Path newPath = oldPath.resolveSibling(targetFileName);
8586

86-
String expandedOldFilePath = oldFile.get().toString();
87+
String expandedOldFilePath = oldPath.toString();
8788
boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath)
88-
&& !newPath.toString().equals(expandedOldFilePath);
89+
&& !newPath.toString().equals(expandedOldFilePath);
8990

90-
if (Files.exists(newPath) && !pathsDifferOnlyByCase) {
91-
// We do not overwrite files
92-
// Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we
93-
// nonetheless rename files to a new name which just differs by case.
94-
LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldFile.get(), newPath);
91+
// Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we
92+
// nonetheless rename files to a new name which just differs by case.
93+
if (Files.exists(newPath) && !pathsDifferOnlyByCase && !overwriteExistingFile) {
94+
LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldPath, newPath);
9595
return false;
96+
}
97+
98+
if (Files.exists(newPath) && !pathsDifferOnlyByCase && overwriteExistingFile) {
99+
Files.createDirectories(newPath.getParent());
100+
LOGGER.debug("Overwriting existing file {}", newPath);
101+
Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
96102
} else {
97103
Files.createDirectories(newPath.getParent());
104+
Files.move(oldPath, newPath);
98105
}
99106

100-
// Rename
101-
Files.move(oldFile.get(), newPath);
102-
103107
// Update path
104108
fileEntry.setLink(relativize(newPath));
105109

0 commit comments

Comments
 (0)