Skip to content

Commit 77ae50e

Browse files
committed
use string comparison for owner
1 parent 5b55b06 commit 77ae50e

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.nio.file.attribute.FileAttribute;
3333
import java.nio.file.attribute.PosixFilePermission;
3434
import java.nio.file.attribute.PosixFilePermissions;
35+
import java.nio.file.attribute.UserPrincipal;
36+
import java.nio.file.attribute.UserPrincipalNotFoundException;
3537
import java.time.LocalDateTime;
3638
import java.time.format.DateTimeFormatter;
3739
import java.util.EnumSet;
@@ -93,10 +95,21 @@ private static boolean isPosixFileSystem(Path path) {
9395
static void verifyPosixOwner(Path file) throws IOException {
9496
if (isPosixFileSystem(file)) {
9597
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)) {
98111
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());
100113
}
101114
}
102115
}
@@ -208,6 +221,9 @@ private static SeekableByteChannel configureNewChannel(
208221
}
209222

210223
private static void closeQuietlyWithSuppressed(SeekableByteChannel channel, Throwable primary) {
224+
if (channel == null) {
225+
return;
226+
}
211227
try {
212228
channel.close();
213229
} catch (IOException closeEx) {

0 commit comments

Comments
 (0)