Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HADOOP-19488 fix the temporary directory creation on Windows #7511

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -27,12 +27,17 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.UserPrincipal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
@@ -51,6 +56,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.nio.file.attribute.AclEntryFlag.DIRECTORY_INHERIT;
import static java.nio.file.attribute.AclEntryFlag.FILE_INHERIT;
import static java.nio.file.attribute.AclEntryType.ALLOW;

/** Run a Hadoop job jar. */
@InterfaceAudience.Private
@InterfaceStability.Unstable
@@ -286,25 +295,19 @@ public void run(String[] args) throws Throwable {
}
mainClassName = mainClassName.replaceAll("/", ".");

File tmpDir = new File(System.getProperty("java.io.tmpdir"));
ensureDirectory(tmpDir);

final File workDir;
try {
FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions
.asFileAttribute(PosixFilePermissions.fromString("rwx------"));
workDir = Files.createTempDirectory(tmpDir.toPath(), "hadoop-unjar", perms).toFile();
workDir = createWorkDirectory();
} catch (IOException | SecurityException e) {
// If user has insufficient perms to write to tmpDir, default
// "Permission denied" message doesn't specify a filename.
System.err.println("Error creating temp dir in java.io.tmpdir "
+ tmpDir + " due to " + e.getMessage());
+ System.getProperty("java.io.tmpdir") + " due to "
+ e.getMessage());
System.exit(-1);
return;
}

ensureDirectory(workDir);

ShutdownHookManager.get().addShutdownHook(
new Runnable() {
@Override
@@ -333,6 +336,55 @@ public void run() {
}
}

static File createWorkDirectory() throws IOException {
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
ensureDirectory(tmpDir);

File workDir = Files.createTempDirectory(tmpDir.toPath(), "hadoop-unjar",
directoryPermissions()).toFile();
ensureDirectory(workDir);
return workDir;
}

private static FileAttribute<?> directoryPermissions() throws IOException {
Set<String> views = FileSystems.getDefault().supportedFileAttributeViews();
if (views.contains("posix")) {
return PosixFilePermissions
.asFileAttribute(PosixFilePermissions.fromString("rwx------"));
} else if (views.contains("acl")) {
return userOnly();
} else {
throw new IOException("unrecognized FileSystem type " +
FileSystems.getDefault());
}
}

private static FileAttribute<?> userOnly() throws IOException {
UserPrincipal user =
FileSystems.getDefault()
.getUserPrincipalLookupService()
.lookupPrincipalByName(System.getProperty("user.name"));
List<AclEntry> acl =
Collections.singletonList(AclEntry.newBuilder()
.setType(ALLOW)
.setPrincipal(user)
.setPermissions(EnumSet.allOf(AclEntryPermission.class))
.setFlags(DIRECTORY_INHERIT, FILE_INHERIT)
.build());
return
new FileAttribute<List<AclEntry>>() {
@Override
public String name() {
return "acl:acl";
}

@Override
public List<AclEntry> value() {
return acl;
}
};
}

/**
* Creates a classloader based on the environment that was specified by the
* user. If HADOOP_USE_CLIENT_CLASSLOADER is specified, it creates an
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import static org.apache.hadoop.util.RunJar.MATCH_ANY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
@@ -197,6 +198,24 @@ private File getUnjarDir(String dirName) {
return unjarDir;
}

/**
* Tests the creation of the temp working directory into which the jars are
* unjarred.
*/
@Test
public void testCreateWorkDirectory() throws Exception {
File workDir = null;
try {
workDir = RunJar.createWorkDirectory();

assertNotNull(workDir, "Work directory should exist and not null");
} finally {
if (workDir != null) {
FileUtil.fullyDelete(workDir);
}
}
}

/**
* Tests the client classloader to verify the main class and its dependent
* class are loaded correctly by the application classloader, and others are