Skip to content

Make TempLocationManager USER aware #8605

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

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
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
Expand Up @@ -295,7 +295,7 @@ private TempLocationManager() {

String pid = PidHelper.getPid();

baseTempDir = configuredTempDir.resolve("ddprof");
baseTempDir = configuredTempDir.resolve(getBaseTempDirName());
baseTempDir.toFile().deleteOnExit();

tempDir = baseTempDir.resolve(TEMPDIR_PREFIX + pid);
Expand All @@ -319,6 +319,17 @@ private TempLocationManager() {
Runtime.getRuntime().addShutdownHook(selfCleanup);
}

// @VisibleForTesting
static String getBaseTempDirName() {
String userName = System.getProperty("user.name");
// unlikely, but fall-back to system env based user name
userName = userName == null ? System.getenv("USER") : userName;
// make sure we do not have any illegal characters in the user name
userName =
userName != null ? userName.replace('.', '_').replace('/', '_').replace(' ', '_') : null;
return "ddprof" + (userName != null ? "_" + userName : "");
}

/**
* Get the temporary directory for the current process. The directory will be removed at JVM exit.
*
Expand Down Expand Up @@ -396,7 +407,7 @@ boolean cleanup(boolean cleanSelf) {
boolean cleanup(boolean cleanSelf, long timeout, TimeUnit unit) {
try {
if (!Files.exists(baseTempDir)) {
// not event the main temp location exists; nothing to clean up
// not even the main temp location exists; nothing to clean up
return true;
}
try (Stream<Path> paths = Files.walk(baseTempDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ void testCleanup(String subPath) throws Exception {

// fake temp location
Path fakeTempDir = tempDir.getParent();
while (fakeTempDir != null && !fakeTempDir.endsWith("ddprof")) {
while (fakeTempDir != null && !fakeTempDir.getFileName().toString().contains("ddprof")) {
fakeTempDir = fakeTempDir.getParent();
}
fakeTempDir = fakeTempDir.resolve("pid_0000");
Files.createDirectories(fakeTempDir);
Path tmpFile = Files.createFile(fakeTempDir.resolve("test.txt"));
tmpFile.toFile().deleteOnExit(); // make sure this is deleted at exit
fakeTempDir.toFile().deleteOnExit(); // also this one
tempLocationManager.cleanup(false);
boolean rslt = tempLocationManager.cleanup(false);
// fake temp location should be deleted
// real temp location should be kept
assertFalse(Files.exists(fakeTempDir));
assertFalse(rslt && Files.exists(fakeTempDir));
assertTrue(Files.exists(tempDir));
}

Expand All @@ -133,7 +133,8 @@ void testConcurrentCleanup(String section) throws Exception {
"ddprof-test-",
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));

Path fakeTempDir = baseDir.resolve("ddprof/pid_1234/scratch");
Path fakeTempDir =
baseDir.resolve(TempLocationManager.getBaseTempDirName() + "/pid_1234/scratch");
Files.createDirectories(fakeTempDir);
Path fakeTempFile = fakeTempDir.resolve("libxxx.so");
Files.createFile(fakeTempFile);
Expand Down