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

Copy file Attributes #2286

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 1 addition & 18 deletions src/main/java/net/sf/jabref/logic/exporter/FileSaveSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
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 @@ -86,24 +83,10 @@ 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
12 changes: 7 additions & 5 deletions src/main/java/net/sf/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class FileUtil {
private static final Pattern SLASH = Pattern.compile("/");
private static final Pattern BACKSLASH = Pattern.compile("\\\\");


/**
* Returns the extension of a file or Optional.empty() if the file does not have one (no . in name).
*
Expand Down Expand Up @@ -141,7 +142,8 @@ public static boolean copyFile(Path pathToSourceFile, Path pathToDestinationFile
return false;
}
try {
return Files.copy(pathToSourceFile, pathToDestinationFile, StandardCopyOption.REPLACE_EXISTING) != null;
return Files.copy(pathToSourceFile, pathToDestinationFile, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES) != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code ensures that the file permission of the temporary file are kept in https://github.com/JabRef/jabref/pull/2286/files#diff-4925b1e29b2566af2796f9c99008b9ddR86, but as far as I can see there is no guarantee that the temporary file has the same permission attributes as the original file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tobiasdiez I am not sure either. However, the problem is that PosixPermission will fail on Windows with an exception

} catch (IOException e) {
LOGGER.error("Copying Files failed.", e);
return false;
Expand Down Expand Up @@ -314,8 +316,8 @@ private static File shortenFileName(File fileName, String directory) {
}
}

public static Map<BibEntry, List<File>> findAssociatedFiles(List<BibEntry> entries,
List<String> extensions, List<File> directories, boolean autolinkExactKeyOnly) {
public static Map<BibEntry, List<File>> findAssociatedFiles(List<BibEntry> entries, List<String> extensions,
List<File> directories, boolean autolinkExactKeyOnly) {
Map<BibEntry, List<File>> result = new HashMap<>();

// First scan directories
Expand Down Expand Up @@ -390,8 +392,8 @@ public static List<File> getListOfLinkedFiles(List<BibEntry> bes, List<String> f
* @param prefs the layout preferences
* @return a suggested fileName
*/
public static String createFileNameFromPattern(BibDatabase database, BibEntry entry,
String fileNamePattern, LayoutFormatterPreferences prefs) {
public static String createFileNameFromPattern(BibDatabase database, BibEntry entry, String fileNamePattern,
LayoutFormatterPreferences prefs) {
String targetName = null;

StringReader sr = new StringReader(fileNamePattern);
Expand Down