|
32 | 32 | import java.nio.file.attribute.FileAttribute; |
33 | 33 | import java.nio.file.attribute.PosixFilePermission; |
34 | 34 | import java.nio.file.attribute.PosixFilePermissions; |
| 35 | +import java.nio.file.attribute.UserPrincipal; |
| 36 | +import java.nio.file.attribute.UserPrincipalNotFoundException; |
35 | 37 | import java.time.LocalDateTime; |
36 | 38 | import java.time.format.DateTimeFormatter; |
37 | 39 | import java.util.EnumSet; |
@@ -93,10 +95,21 @@ private static boolean isPosixFileSystem(Path path) { |
93 | 95 | static void verifyPosixOwner(Path file) throws IOException { |
94 | 96 | if (isPosixFileSystem(file)) { |
95 | 97 | String currentUser = System.getProperty("user.name"); |
96 | | - String ownerName = Files.getOwner(file, LinkOption.NOFOLLOW_LINKS).getName(); |
97 | | - if (currentUser == null || currentUser.isEmpty() || !currentUser.equals(ownerName)) { |
| 98 | + UserPrincipal fileOwner = Files.getOwner(file, LinkOption.NOFOLLOW_LINKS); |
| 99 | + UserPrincipal currentOwner = null; |
| 100 | + if (currentUser != null && !currentUser.isEmpty()) { |
| 101 | + try { |
| 102 | + currentOwner = |
| 103 | + file.getFileSystem() |
| 104 | + .getUserPrincipalLookupService() |
| 105 | + .lookupPrincipalByName(currentUser); |
| 106 | + } catch (UserPrincipalNotFoundException ignored) { |
| 107 | + // Leave currentOwner null so FileAlreadyExistsException is thrown below. |
| 108 | + } |
| 109 | + } |
| 110 | + if (!fileOwner.equals(currentOwner)) { |
98 | 111 | throw new FileAlreadyExistsException( |
99 | | - file.toString(), null, "Log file owner could not be verified: " + ownerName); |
| 112 | + file.toString(), null, "Log file owner could not be verified: " + fileOwner.getName()); |
100 | 113 | } |
101 | 114 | } |
102 | 115 | } |
@@ -208,6 +221,9 @@ private static SeekableByteChannel configureNewChannel( |
208 | 221 | } |
209 | 222 |
|
210 | 223 | private static void closeQuietlyWithSuppressed(SeekableByteChannel channel, Throwable primary) { |
| 224 | + if (channel == null) { |
| 225 | + return; |
| 226 | + } |
211 | 227 | try { |
212 | 228 | channel.close(); |
213 | 229 | } catch (IOException closeEx) { |
|
0 commit comments