diff --git a/core/src/main/java/hudson/ClassicPluginStrategy.java b/core/src/main/java/hudson/ClassicPluginStrategy.java index 523474a89d..ef29f48158 100644 --- a/core/src/main/java/hudson/ClassicPluginStrategy.java +++ b/core/src/main/java/hudson/ClassicPluginStrategy.java @@ -480,7 +480,7 @@ public class ClassicPluginStrategy implements PluginStrategy { * Explodes the plugin into a directory, if necessary. */ private static void explode(File archive, File destDir) throws IOException { - Files.createDirectories(Util.fileToPath(destDir)); + Util.createDirectories(Util.fileToPath(destDir)); // timestamp check File explodeTime = new File(destDir,".timestamp2"); @@ -549,7 +549,7 @@ public class ClassicPluginStrategy implements PluginStrategy { }; z.setProject(prj); z.setTaskType("zip"); - Files.createDirectories(Util.fileToPath(classesJar.getParentFile())); + Util.createDirectories(Util.fileToPath(classesJar.getParentFile())); z.setDestFile(classesJar); z.add(mapper); z.execute(); diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index 528cdce64b..99313e18c7 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -356,7 +356,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas this.rootDir = rootDir; try { - Files.createDirectories(rootDir.toPath()); + Util.createDirectories(rootDir.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 32d114efb2..df22f9faab 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -67,6 +67,7 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.DosFileAttributes; +import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.security.DigestInputStream; @@ -1727,6 +1728,63 @@ public class Util { } } + /** + * Create a directory by creating all nonexistent parent directories first. + * + *

Unlike {@link Files#createDirectory}, an exception is not thrown + * if the directory could not be created because it already exists. + * Unlike {@link Files#createDirectories}, an exception is not thrown + * if the directory (or one of its parents) is a symbolic link. + * + *

The {@code attrs} parameter contains optional {@link FileAttribute file attributes} + * to set atomically when creating the nonexistent directories. + * Each file attribute is identified by its {@link FileAttribute#name}. + * If more than one attribute of the same name is included in the array, + * then all but the last occurrence is ignored. + * + *

If this method fails, + * then it may do so after creating some, but not all, of the parent directories. + * + * @param dir The directory to create. + * @param attrs An optional list of file attributes to set atomically + * when creating the directory. + * @return The directory. + * @throws UnsupportedOperationException If the array contains an attribute + * that cannot be set atomically when creating the directory. + * @throws FileAlreadyExistsException If {@code dir} exists but is not a directory. + * @throws IOException If an I/O error occurs. + * @see Files#createDirectories(Path, FileAttribute[]) + */ + @Restricted(NoExternalUse.class) + public static Path createDirectories(@NonNull Path dir, FileAttribute... attrs) throws IOException { + dir = dir.toAbsolutePath(); + + Path parent; + for (parent = dir.getParent(); parent != null; parent = parent.getParent()) { + if (Files.exists(parent)) { + break; + } + } + + if (parent == null) { + if (Files.isDirectory(dir)) { + return dir; + } else { + return Files.createDirectory(dir, attrs); + } + } + + Path child = parent; + for (Path name : parent.relativize(dir)) { + child = child.resolve(name); + if (!Files.isDirectory(child)) { + Files.createDirectory(child, attrs); + } + } + + return dir; + } + /** * Compute the number of calendar days elapsed since the given date. * As it's only the calendar days difference that matter, "11.00pm" to "2.00am the day after" returns 1, diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index f41de96eac..cfa828ec9c 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -190,7 +190,7 @@ public class WebAppMain implements ServletContextListener { final FileAndDescription describedHomeDir = getHomeDir(event); home = describedHomeDir.file.getAbsoluteFile(); try { - Files.createDirectories(home.toPath()); + Util.createDirectories(home.toPath()); } catch (IOException | InvalidPathException e) { throw (NoHomeDir)new NoHomeDir(home).initCause(e); } diff --git a/core/src/main/java/hudson/XmlFile.java b/core/src/main/java/hudson/XmlFile.java index 46e70835a2..c9f0478f0b 100644 --- a/core/src/main/java/hudson/XmlFile.java +++ b/core/src/main/java/hudson/XmlFile.java @@ -252,7 +252,7 @@ public final class XmlFile { } public void mkdirs() throws IOException { - Files.createDirectories(Util.fileToPath(file.getParentFile())); + Util.createDirectories(Util.fileToPath(file.getParentFile())); } @Override diff --git a/core/src/main/java/hudson/init/impl/InitialUserContent.java b/core/src/main/java/hudson/init/impl/InitialUserContent.java index e057ba16a3..810bcdcce7 100644 --- a/core/src/main/java/hudson/init/impl/InitialUserContent.java +++ b/core/src/main/java/hudson/init/impl/InitialUserContent.java @@ -43,7 +43,7 @@ public class InitialUserContent { public static void init(Jenkins h) throws IOException { File userContentDir = new File(h.getRootDir(), "userContent"); if (!Files.isDirectory(Util.fileToPath(userContentDir))) { - Files.createDirectories(Util.fileToPath(userContentDir)); + Util.createDirectories(Util.fileToPath(userContentDir)); FileUtils.writeStringToFile(new File(userContentDir,"readme.txt"), Messages.Hudson_USER_CONTENT_README() + "\n"); } } diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index a7ac71edc8..8c1ce41e1a 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -1678,7 +1678,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces if (m.matches()) { File newLocation = new File(dir, "logs/slaves/" + m.group(1) + "/slave.log" + Util.fixNull(m.group(2))); try { - Files.createDirectories(newLocation.getParentFile().toPath()); + Util.createDirectories(newLocation.getParentFile().toPath()); Files.move(f.toPath(), newLocation.toPath(), StandardCopyOption.REPLACE_EXISTING); LOGGER.log(Level.INFO, "Relocated log file {0} to {1}",new Object[] {f.getPath(),newLocation.getPath()}); } catch (IOException | InvalidPathException e) { diff --git a/core/src/main/java/hudson/model/ItemGroupMixIn.java b/core/src/main/java/hudson/model/ItemGroupMixIn.java index b1d52d9bfe..54798d4978 100644 --- a/core/src/main/java/hudson/model/ItemGroupMixIn.java +++ b/core/src/main/java/hudson/model/ItemGroupMixIn.java @@ -99,7 +99,7 @@ public abstract class ItemGroupMixIn { */ public static Map loadChildren(ItemGroup parent, File modulesDir, Function1 key) { try { - Files.createDirectories(modulesDir.toPath()); + Util.createDirectories(modulesDir.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -273,7 +273,7 @@ public abstract class ItemGroupMixIn { final File dir = configXml.getParentFile(); boolean success = false; try { - Files.createDirectories(dir.toPath()); + Util.createDirectories(dir.toPath()); XMLUtils.safeTransform(new StreamSource(xml), new StreamResult(configXml)); // load it diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java index dc1ac34a42..1e0d90c8ca 100644 --- a/core/src/main/java/hudson/model/Job.java +++ b/core/src/main/java/hudson/model/Job.java @@ -664,7 +664,7 @@ public abstract class Job, RunT extends Run> extends AbstractLazyLoadRunMap proposeNewNumber(r.getNumber()); } try { - Files.createDirectories(rootDir.toPath()); + Util.createDirectories(rootDir.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/core/src/main/java/hudson/util/TextFile.java b/core/src/main/java/hudson/util/TextFile.java index 99be263843..9b792bdee6 100644 --- a/core/src/main/java/hudson/util/TextFile.java +++ b/core/src/main/java/hudson/util/TextFile.java @@ -96,7 +96,7 @@ public class TextFile { * Overwrites the file by the given string. */ public void write(String text) throws IOException { - Files.createDirectories(Util.fileToPath(file.getParentFile())); + Util.createDirectories(Util.fileToPath(file.getParentFile())); try (AtomicFileWriter w = new AtomicFileWriter(file)) { try { w.write(text); diff --git a/core/src/main/java/jenkins/fingerprints/FileFingerprintStorage.java b/core/src/main/java/jenkins/fingerprints/FileFingerprintStorage.java index 6d30bdcecc..9a10790e1a 100644 --- a/core/src/main/java/jenkins/fingerprints/FileFingerprintStorage.java +++ b/core/src/main/java/jenkins/fingerprints/FileFingerprintStorage.java @@ -147,7 +147,7 @@ public class FileFingerprintStorage extends FingerprintStorage { */ public static void save(Fingerprint fp, File file) throws IOException { if (fp.getPersistedFacets().isEmpty()) { - Files.createDirectories(Util.fileToPath(file.getParentFile())); + Util.createDirectories(Util.fileToPath(file.getParentFile())); // JENKINS-16301: fast path for the common case. AtomicFileWriter afw = new AtomicFileWriter(file); try (PrintWriter w = new PrintWriter(new BufferedWriter(afw))) { diff --git a/core/src/main/java/jenkins/management/AsynchronousAdministrativeMonitor.java b/core/src/main/java/jenkins/management/AsynchronousAdministrativeMonitor.java index 65ee4cd653..a74fb080de 100644 --- a/core/src/main/java/jenkins/management/AsynchronousAdministrativeMonitor.java +++ b/core/src/main/java/jenkins/management/AsynchronousAdministrativeMonitor.java @@ -3,6 +3,7 @@ package jenkins.management; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.AbortException; import hudson.Functions; +import hudson.Util; import hudson.console.AnnotatedLargeText; import hudson.model.AdministrativeMonitor; import hudson.model.TaskListener; @@ -13,7 +14,6 @@ import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.charset.Charset; -import java.nio.file.Files; import java.util.logging.Level; import java.util.logging.Logger; import jenkins.model.Jenkins; @@ -60,7 +60,7 @@ public abstract class AsynchronousAdministrativeMonitor extends AdministrativeMo protected File getLogFile() { File base = getBaseDir(); try { - Files.createDirectories(base.toPath()); + Util.createDirectories(base.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/core/src/main/java/jenkins/util/io/FileBoolean.java b/core/src/main/java/jenkins/util/io/FileBoolean.java index 83ab8a507a..9963150d7f 100644 --- a/core/src/main/java/jenkins/util/io/FileBoolean.java +++ b/core/src/main/java/jenkins/util/io/FileBoolean.java @@ -1,5 +1,6 @@ package jenkins.util.io; +import hudson.Util; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -59,7 +60,7 @@ public class FileBoolean { public void on() { try { - Files.createDirectories(file.getParentFile().toPath()); + Util.createDirectories(file.getParentFile().toPath()); Files.newOutputStream(file.toPath()).close(); get(); // update state } catch (IOException | InvalidPathException e) { diff --git a/core/src/test/java/hudson/UtilTest.java b/core/src/test/java/hudson/UtilTest.java index c1ca84ff57..37b1cf4924 100644 --- a/core/src/test/java/hudson/UtilTest.java +++ b/core/src/test/java/hudson/UtilTest.java @@ -34,6 +34,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeTrue; import hudson.model.TaskListener; @@ -43,8 +44,10 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.AccessDeniedException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.attribute.PosixFilePermissions; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -630,6 +633,52 @@ public class UtilTest { assertNull(Util.resolveSymlinkToFile(new File(_a, "aa/aa.txt"))); } + @Test + @Issue("JENKINS-67372") + public void createDirectories() throws Exception { + assumeFalse(Functions.isWindows()); + // root + // /a + // /a1 + // /a2 => symlink to a1 + // /b => symlink to a + Path root = tmp.getRoot().toPath(); + Path a = root.resolve("a"); + Path a1 = a.resolve("a1"); + Files.createDirectories(a1); + + Path a2 = a.resolve("a2"); + Util.createSymlink(a2.getParent().toFile(), a1.getFileName().toString(), a2.getFileName().toString(), TaskListener.NULL); + + Path b = root.resolve("b"); + Util.createSymlink(b.getParent().toFile(), a.getFileName().toString(), b.getFileName().toString(), TaskListener.NULL); + + assertTrue(Files.isSymbolicLink(a2)); + assertTrue(Files.isSymbolicLink(b)); + + assertEquals(a.resolve("new1"), Util.createDirectories(a.resolve("new1")).toRealPath()); + assertEquals(a1.resolve("new2"), Util.createDirectories(a1.resolve("new2")).toRealPath()); + assertEquals(a1.resolve("new3"), Util.createDirectories(a2.resolve("new3")).toRealPath()); + assertEquals(a.resolve("new4"), Util.createDirectories(b.resolve("new4")).toRealPath()); + assertEquals(a1.resolve("new5"), Util.createDirectories(b.resolve("a1").resolve("new5")).toRealPath()); + assertEquals(a1.resolve("new6"), Util.createDirectories(b.resolve("a2").resolve("new6")).toRealPath()); + } + + @Test + @Issue("JENKINS-67372") + public void createDirectoriesInRoot() throws Exception { + Path newDirInRoot = Paths.get("/new-dir-in-root"); + Path newSymlinkInRoot = Paths.get("/new-symlink-in-root"); + try { + assertEquals(newDirInRoot.resolve("new1"), Util.createDirectories(newDirInRoot.resolve("new1")).toRealPath()); + Util.createSymlink(newSymlinkInRoot.getParent().toFile(), newDirInRoot.getFileName().toString(), newSymlinkInRoot.getFileName().toString(), TaskListener.NULL); + assertEquals(newDirInRoot.resolve("new2"), Util.createDirectories(newSymlinkInRoot.resolve("new2")).toRealPath()); + } catch (AccessDeniedException e) { + // Not running as root + assumeNoException(e); + } + } + @Test public void ifOverriddenSuccess() { assertTrue(Util.ifOverridden(() -> true, BaseClass.class, DerivedClassSuccess.class, "method"));