Skip to content

Commit

Permalink
Fix JabRef#2279: File permissions are kept upon save
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 17, 2016
1 parent 19a3af1 commit bff8dfb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This project **does not** adhere to [Semantic Versioning](http://semver.org/).
This file tries to follow the conventions proposed by [keepachangelog.com](http://keepachangelog.com/).
Here, the categories "Changed" for added and changed functionality,
"Fixed" for fixed functionality, and
"Removed" for removed functionality is used.
"Removed" for removed functionality are used.

We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#NUM`.

Expand All @@ -14,6 +14,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
### Changed

### Fixed
- We fixed an issue where the file permissions of the .bib-file were changed upon saving [#2279](https://github.com/JabRef/jabref/issues/2279).

### Removed

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.Set;

import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.logic.util.io.FileUtil;
Expand Down Expand Up @@ -83,7 +86,24 @@ public void commit(Path file) throws SaveException {
LOGGER.error("Error when creating lock file.", ex);
}

// Try to save file permissions to restore them later (by default: allow everything)
Set<PosixFilePermission> oldFilePermissions = EnumSet.allOf(PosixFilePermission.class);
if (Files.exists(file)) {
try {
oldFilePermissions = Files.getPosixFilePermissions(file);
} catch (IOException exception) {
LOGGER.warn("Error getting file permissions.", exception);
}
}

FileUtil.copyFile(temporaryFile, file, true);

// Restore file permissions
try {
Files.setPosixFilePermissions(file, oldFilePermissions);
} catch (IOException exception) {
throw new SaveException(exception);
}
} finally {
FileBasedLock.deleteLockFile(file);
}
Expand Down

0 comments on commit bff8dfb

Please sign in to comment.