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