From bff8dfbf2dee35a38b2f49c2fc086f769179476c Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Thu, 17 Nov 2016 15:29:26 +0100 Subject: [PATCH] Fix #2279: File permissions are kept upon save --- CHANGELOG.md | 3 ++- .../logic/exporter/FileSaveSession.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d4a2db0441..09ceb1aedb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. @@ -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 diff --git a/src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java b/src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java index 433b28dacb7..f54b70ab51d 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java +++ b/src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java @@ -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; @@ -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 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); }