From dd56e2b42e3440a1db9204191d914a3541fa70fd Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 5 May 2021 12:05:48 +0200 Subject: [PATCH] handle windows with owner only file and directory creation --- .../com/sparrowwallet/sparrow/io/Config.java | 2 +- .../com/sparrowwallet/sparrow/io/Storage.java | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index a4e39b49..781cc04c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -444,7 +444,7 @@ public class Config { try { File configFile = getConfigFile(); if(!configFile.exists()) { - Files.createFile(configFile.toPath(), PosixFilePermissions.asFileAttribute(Storage.getFileOwnerOnlyFilePermissions())); + Storage.createOwnerOnlyFile(configFile); } Writer writer = new FileWriter(configFile); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index ed6efd3e..26132dce 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -170,7 +170,7 @@ public class Storage { } if(!walletFile.exists()) { - Files.createFile(walletFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); + createOwnerOnlyFile(walletFile); } Writer writer = new FileWriter(walletFile); @@ -195,7 +195,7 @@ public class Storage { } if(!walletFile.exists()) { - Files.createFile(walletFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); + createOwnerOnlyFile(walletFile); } OutputStream outputStream = new FileOutputStream(walletFile); @@ -249,7 +249,7 @@ public class Storage { File backupFile = new File(backupDir, backupName); if(!backupFile.exists()) { - Files.createFile(backupFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); + createOwnerOnlyFile(backupFile); } com.google.common.io.Files.copy(walletFile, backupFile); } @@ -468,8 +468,15 @@ public class Storage { private static boolean createOwnerOnlyDirectory(File directory) { try { - Files.createDirectories(directory.toPath(), PosixFilePermissions.asFileAttribute(getDirectoryOwnerOnlyFilePermissions())); + if(Platform.getCurrent() == Platform.WINDOWS) { + Files.createDirectories(directory.toPath()); + return true; + } + + Files.createDirectories(directory.toPath(), PosixFilePermissions.asFileAttribute(getDirectoryOwnerOnlyPosixFilePermissions())); return true; + } catch(UnsupportedOperationException e) { + return directory.mkdirs(); } catch(IOException e) { log.error("Could not create directory " + directory.getAbsolutePath(), e); } @@ -477,14 +484,32 @@ public class Storage { return false; } - public static Set getDirectoryOwnerOnlyFilePermissions() { - Set ownerOnly = getFileOwnerOnlyFilePermissions(); + public static boolean createOwnerOnlyFile(File file) { + try { + if(Platform.getCurrent() == Platform.WINDOWS) { + Files.createFile(file.toPath()); + return true; + } + + Files.createFile(file.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyPosixFilePermissions())); + return true; + } catch(UnsupportedOperationException e) { + return false; + } catch(IOException e) { + log.error("Could not create file " + file.getAbsolutePath(), e); + } + + return false; + } + + private static Set getDirectoryOwnerOnlyPosixFilePermissions() { + Set ownerOnly = getFileOwnerOnlyPosixFilePermissions(); ownerOnly.add(PosixFilePermission.OWNER_EXECUTE); return ownerOnly; } - public static Set getFileOwnerOnlyFilePermissions() { + private static Set getFileOwnerOnlyPosixFilePermissions() { Set ownerOnly = EnumSet.noneOf(PosixFilePermission.class); ownerOnly.add(PosixFilePermission.OWNER_READ); ownerOnly.add(PosixFilePermission.OWNER_WRITE);