handle windows with owner only file and directory creation

This commit is contained in:
Craig Raw 2021-05-05 12:05:48 +02:00
parent e0b6530275
commit dd56e2b42e
2 changed files with 33 additions and 8 deletions

View file

@ -444,7 +444,7 @@ public class Config {
try { try {
File configFile = getConfigFile(); File configFile = getConfigFile();
if(!configFile.exists()) { if(!configFile.exists()) {
Files.createFile(configFile.toPath(), PosixFilePermissions.asFileAttribute(Storage.getFileOwnerOnlyFilePermissions())); Storage.createOwnerOnlyFile(configFile);
} }
Writer writer = new FileWriter(configFile); Writer writer = new FileWriter(configFile);

View file

@ -170,7 +170,7 @@ public class Storage {
} }
if(!walletFile.exists()) { if(!walletFile.exists()) {
Files.createFile(walletFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); createOwnerOnlyFile(walletFile);
} }
Writer writer = new FileWriter(walletFile); Writer writer = new FileWriter(walletFile);
@ -195,7 +195,7 @@ public class Storage {
} }
if(!walletFile.exists()) { if(!walletFile.exists()) {
Files.createFile(walletFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); createOwnerOnlyFile(walletFile);
} }
OutputStream outputStream = new FileOutputStream(walletFile); OutputStream outputStream = new FileOutputStream(walletFile);
@ -249,7 +249,7 @@ public class Storage {
File backupFile = new File(backupDir, backupName); File backupFile = new File(backupDir, backupName);
if(!backupFile.exists()) { if(!backupFile.exists()) {
Files.createFile(backupFile.toPath(), PosixFilePermissions.asFileAttribute(getFileOwnerOnlyFilePermissions())); createOwnerOnlyFile(backupFile);
} }
com.google.common.io.Files.copy(walletFile, backupFile); com.google.common.io.Files.copy(walletFile, backupFile);
} }
@ -468,8 +468,15 @@ public class Storage {
private static boolean createOwnerOnlyDirectory(File directory) { private static boolean createOwnerOnlyDirectory(File directory) {
try { try {
Files.createDirectories(directory.toPath(), PosixFilePermissions.asFileAttribute(getDirectoryOwnerOnlyFilePermissions())); if(Platform.getCurrent() == Platform.WINDOWS) {
Files.createDirectories(directory.toPath());
return true; return true;
}
Files.createDirectories(directory.toPath(), PosixFilePermissions.asFileAttribute(getDirectoryOwnerOnlyPosixFilePermissions()));
return true;
} catch(UnsupportedOperationException e) {
return directory.mkdirs();
} catch(IOException e) { } catch(IOException e) {
log.error("Could not create directory " + directory.getAbsolutePath(), e); log.error("Could not create directory " + directory.getAbsolutePath(), e);
} }
@ -477,14 +484,32 @@ public class Storage {
return false; return false;
} }
public static Set<PosixFilePermission> getDirectoryOwnerOnlyFilePermissions() { public static boolean createOwnerOnlyFile(File file) {
Set<PosixFilePermission> ownerOnly = getFileOwnerOnlyFilePermissions(); 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<PosixFilePermission> getDirectoryOwnerOnlyPosixFilePermissions() {
Set<PosixFilePermission> ownerOnly = getFileOwnerOnlyPosixFilePermissions();
ownerOnly.add(PosixFilePermission.OWNER_EXECUTE); ownerOnly.add(PosixFilePermission.OWNER_EXECUTE);
return ownerOnly; return ownerOnly;
} }
public static Set<PosixFilePermission> getFileOwnerOnlyFilePermissions() { private static Set<PosixFilePermission> getFileOwnerOnlyPosixFilePermissions() {
Set<PosixFilePermission> ownerOnly = EnumSet.noneOf(PosixFilePermission.class); Set<PosixFilePermission> ownerOnly = EnumSet.noneOf(PosixFilePermission.class);
ownerOnly.add(PosixFilePermission.OWNER_READ); ownerOnly.add(PosixFilePermission.OWNER_READ);
ownerOnly.add(PosixFilePermission.OWNER_WRITE); ownerOnly.add(PosixFilePermission.OWNER_WRITE);