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

Keep EOL setting at backups #9048

Merged
merged 3 commits into from
Aug 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,13 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
}

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException {
// if this code is adapted, please also adapt org.jabref.logic.autosaveandbackup.BackupManager.performBackup

GeneralPreferences generalPreferences = this.preferences.getGeneralPreferences();
SavePreferences savePreferences = this.preferences.getSavePreferences()
.withSaveType(saveType);
BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext();
try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, encoding, savePreferences.shouldMakeBackup())) {
BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext();
BibWriter bibWriter = new BibWriter(fileWriter, bibDatabaseContext.getDatabase().getNewLineSeparator());
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, entryTypesManager);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.logic.autosaveandbackup;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -20,7 +19,6 @@
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.util.CoarseChangeFilter;
import org.jabref.logic.util.DelayTaskThrottler;
import org.jabref.logic.util.OS;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
Expand Down Expand Up @@ -57,7 +55,7 @@ private BackupManager(BibDatabaseContext bibDatabaseContext, BibEntryTypesManage
this.bibDatabaseContext = bibDatabaseContext;
this.entryTypesManager = entryTypesManager;
this.preferences = preferences;
this.throttler = new DelayTaskThrottler(15000);
this.throttler = new DelayTaskThrottler(15_000);

changeFilter = new CoarseChangeFilter(bibDatabaseContext);
changeFilter.registerListener(this);
Expand Down Expand Up @@ -134,13 +132,13 @@ private Optional<Path> determineBackupPath() {
}

private void performBackup(Path backupPath) {
try {
Charset charset = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8);
GeneralPreferences generalPreferences = preferences.getGeneralPreferences();
SavePreferences savePreferences = preferences.getSavePreferences()
.withMakeBackup(false);
Writer writer = new AtomicFileWriter(backupPath, charset);
BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE);
// code similar to org.jabref.gui.exporter.SaveDatabaseAction.saveDatabase
GeneralPreferences generalPreferences = preferences.getGeneralPreferences();
SavePreferences savePreferences = preferences.getSavePreferences()
.withMakeBackup(false);
Charset encoding = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8);
try (AtomicFileWriter fileWriter = new AtomicFileWriter(backupPath, encoding)) {
BibWriter bibWriter = new BibWriter(fileWriter, bibDatabaseContext.getDatabase().getNewLineSeparator());
new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, entryTypesManager)
.saveDatabase(bibDatabaseContext);
} catch (IOException e) {
Expand All @@ -157,7 +155,7 @@ private void logIfCritical(Path backupPath, IOException e) {

// do not print errors in field values into the log during autosave
if (!isErrorInField) {
LOGGER.error("Error while saving to file" + backupPath, e);
LOGGER.error("Error while saving to file {}", backupPath, e);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static boolean copyFile(Path pathToSourceFile, Path pathToDestinationFile
*
* @param fromFile The source filename to rename
* @param toFile The target fileName
* @return True if the rename was successful, false if an exception occurred
* @return True if rename was successful, false if an exception occurred
*/
public static boolean renameFile(Path fromFile, Path toFile) {
return renameFile(fromFile, toFile, false);
Expand All @@ -204,8 +204,8 @@ public static boolean renameFile(Path fromFile, Path toFile) {
*
* @param fromFile The source filename to rename
* @param toFile The target fileName
* @param replaceExisting Wether to replace existing files or not
* @return True if the rename was successful, false if an exception occurred
* @param replaceExisting Whether to replace existing files or not
* @return True if rename was successful, false if an exception occurred
* @deprecated Use {@link Files#move(Path, Path, CopyOption...)} instead and handle exception properly
*/
@Deprecated
Expand Down