Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fulltext file deleted when being renamed to the name differing just b… #6123

Merged
merged 1 commit into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
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
24 changes: 9 additions & 15 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,30 +223,22 @@ public void renameFileToName(String targetFileName) {
}

private void performRenameWithConflictCheck(String targetFileName) {
Optional<Path> fileConflictCheck = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName);
if (fileConflictCheck.isPresent()) {
boolean confirmOverwrite = dialogService.showConfirmationDialogAndWait(
Optional<Path> existingFile = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName);
boolean overwriteFile = false;

if (existingFile.isPresent()) {
overwriteFile = dialogService.showConfirmationDialogAndWait(
Localization.lang("File exists"),
Localization.lang("'%0' exists. Overwrite file?", targetFileName),
Localization.lang("Overwrite"));

if (confirmOverwrite) {
try {
Files.delete(fileConflictCheck.get());
} catch (IOException e) {
dialogService.showErrorDialogAndWait(
Localization.lang("Rename failed"),
Localization.lang("JabRef cannot access the file because it is being used by another process."),
e);
return;
}
} else {
if (!overwriteFile) {
return;
}
}

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

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

/**
* Compares suggested filepath of current linkedFile with existing filepath.
*
* @return true if suggested filepath is same as existing filepath.
*/
public boolean isGeneratedPathSameAsOriginal() {
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/org/jabref/logic/externalfiles/LinkedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -71,35 +72,38 @@ public boolean moveToDefaultDirectory() throws IOException {
}

public boolean renameToSuggestedName() throws IOException {
return renameToName(getSuggestedFileName());
return renameToName(getSuggestedFileName(), false);
}

public boolean renameToName(String targetFileName) throws IOException {
public boolean renameToName(String targetFileName, boolean overwriteExistingFile) throws IOException {
Optional<Path> oldFile = fileEntry.findIn(databaseContext, filePreferences);
if (!oldFile.isPresent()) {
// Could not find file
return false;
}

Path newPath = oldFile.get().resolveSibling(targetFileName);
final Path oldPath = oldFile.get();
final Path newPath = oldPath.resolveSibling(targetFileName);

String expandedOldFilePath = oldFile.get().toString();
String expandedOldFilePath = oldPath.toString();
boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath)
&& !newPath.toString().equals(expandedOldFilePath);
&& !newPath.toString().equals(expandedOldFilePath);

if (Files.exists(newPath) && !pathsDifferOnlyByCase) {
// We do not overwrite files
// Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we
// nonetheless rename files to a new name which just differs by case.
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);
// Since Files.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we
// nonetheless rename files to a new name which just differs by case.
if (Files.exists(newPath) && !pathsDifferOnlyByCase && !overwriteExistingFile) {
LOGGER.debug("The file {} would have been moved to {}. However, there exists already a file with that name so we do nothing.", oldPath, newPath);
return false;
}

if (Files.exists(newPath) && !pathsDifferOnlyByCase && overwriteExistingFile) {
Files.createDirectories(newPath.getParent());
LOGGER.debug("Overwriting existing file {}", newPath);
Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
} else {
Files.createDirectories(newPath.getParent());
Files.move(oldPath, newPath);
}

// Rename
Files.move(oldFile.get(), newPath);

// Update path
fileEntry.setLink(relativize(newPath));

Expand Down