Skip to content

Commit e158c78

Browse files
anuengineerdeepakdamri
authored andcommitted
HADOOP-16026:Replace incorrect use of system property user.name.
Contributed by Dinesh Chitlangia.
1 parent 8c8f1b0 commit e158c78

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.hadoop.fs.permission.FsPermission;
5252
import org.apache.hadoop.security.AccessControlException;
5353
import org.apache.hadoop.security.SecurityUtil;
54+
import org.apache.hadoop.security.UserGroupInformation;
5455
import org.apache.hadoop.security.token.Token;
5556
import org.apache.hadoop.util.LambdaUtils;
5657
import org.apache.hadoop.util.Progressable;
@@ -456,8 +457,16 @@ public Path getInitialWorkingDirectory() {
456457
* @return current user's home directory.
457458
*/
458459
public Path getHomeDirectory() {
459-
return new Path("/user/"+System.getProperty("user.name")).makeQualified(
460-
getUri(), null);
460+
String username;
461+
try {
462+
username = UserGroupInformation.getCurrentUser().getShortUserName();
463+
} catch(IOException ex) {
464+
LOG.warn("Unable to get user name. Fall back to system property " +
465+
"user.name", ex);
466+
username = System.getProperty("user.name");
467+
}
468+
return new Path("/user/" + username)
469+
.makeQualified(getUri(), null);
461470
}
462471

463472
/**

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2270,8 +2270,16 @@ public LocatedFileStatus next() throws IOException {
22702270
* The default implementation returns {@code "/user/$USER/"}.
22712271
*/
22722272
public Path getHomeDirectory() {
2273+
String username;
2274+
try {
2275+
username = UserGroupInformation.getCurrentUser().getShortUserName();
2276+
} catch(IOException ex) {
2277+
LOGGER.warn("Unable to get user name. Fall back to system property " +
2278+
"user.name", ex);
2279+
username = System.getProperty("user.name");
2280+
}
22732281
return this.makeQualified(
2274-
new Path(USER_HOME_PREFIX + "/" + System.getProperty("user.name")));
2282+
new Path(USER_HOME_PREFIX + "/" + username));
22752283
}
22762284

22772285

0 commit comments

Comments
 (0)