From 05531effa755a3f8e6e352a1793b04f8f76d60b9 Mon Sep 17 00:00:00 2001 From: "Antonio.Busuladzich" Date: Thu, 21 Nov 2024 20:04:08 +0200 Subject: [PATCH] Create a tmp directory when such doesn't exist. In some cases the tmp director won't exist and an Exception will be thrown when attempting to create a file in that directory. An example of such case is creating a JenkinsRule instance in some tests and executing those tests from an IDE. --- core/src/main/java/hudson/Util.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 07d00a44d006..fdcced538ef3 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -436,6 +436,14 @@ public static File createTempDir() throws IOException { // https://github.com/jenkinsci/jenkins/pull/3161 ) final Path tempPath; final String tempDirNamePrefix = "jenkins"; + + final Path systemTmpDirectoryPath = Path.of(System.getProperty("java.io.tmpdir")); + if (!systemTmpDirectoryPath.toFile().exists()){ + // In some cases the tmp directory set in the java.io.tmpdir property will not exist and hence will have to + // be created here. + systemTmpDirectoryPath.toFile().mkdirs(); + } + if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { tempPath = Files.createTempDirectory(tempDirNamePrefix, PosixFilePermissions.asFileAttribute(EnumSet.allOf(PosixFilePermission.class)));