Skip to content
Permalink
Browse files

[JENKINS-47324] - Reduce usage of File.mkdirs() in FilePath and IOUti…

…ls (#3173)

* Move an instance of renameTo() to Files.move()

* Replace an instance of File.toURI() with an instance of Path.toUri()

* Replace mkdirs() with Files.createDirectories()
Replace mkdir() with Files.createTempDirectory()

* Undo addition of createTempDirectory() as per review comments

* Return to use of FilePath#mkdirs(File) and instead modify it to use the new API.

* Undo addition of toPath() in a URI conversion as it brings no benefits.

* Replace new uses of toPath() with Util.fileToPath() to pre-handle runtime exceptions

* Remove * import.
move mkdirs() to using FilePath method instead of File method.

* Make IOUtils.mkdirs(File) use Java7 API calls

* Add back accidentally removed imports.

* Fixed use of wildcard import

* Use utility method fileToPath() to handle potential exception thrown
  • Loading branch information...
KrishanBhasin authored and oleg-nenashev committed Dec 14, 2017
1 parent b4ff724 commit 09bcc5d6538b3cfffbf71228ebd1679e3e20d8b2
Showing with 17 additions and 20 deletions.
  1. +11 −7 core/src/main/java/hudson/FilePath.java
  2. +6 −13 core/src/main/java/hudson/util/IOUtils.java
@@ -79,6 +79,7 @@
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
@@ -125,8 +126,10 @@

import static hudson.FilePath.TarCompression.GZIP;
import static hudson.Util.deleteFile;
import static hudson.Util.fileToPath;
import static hudson.Util.fixEmpty;
import static hudson.Util.isSymlink;

import java.util.Collections;

/**
@@ -1165,7 +1168,7 @@ public Boolean invoke(File f, VirtualChannel channel) throws IOException, Interr
// following Ant <mkdir> task to avoid possible race condition.
Thread.sleep(10);

return f.mkdirs() || f.exists();
return mkdirs(f) || f.exists();
}
}))
throw new IOException("Failed to mkdirs: "+remote);
@@ -1615,7 +1618,7 @@ private static void _chmod(File f, int mask) throws IOException {
if (Util.NATIVE_CHMOD_MODE) {
PosixAPI.jnr().chmod(f.getAbsolutePath(), mask);
} else {
Files.setPosixFilePermissions(Util.fileToPath(f), Util.modeToPermissions(mask));
Files.setPosixFilePermissions(fileToPath(f), Util.modeToPermissions(mask));
}
}

@@ -1962,7 +1965,7 @@ public void renameTo(final FilePath target) throws IOException, InterruptedExcep
act(new SecureFileCallable<Void>() {
private static final long serialVersionUID = 1L;
public Void invoke(File f, VirtualChannel channel) throws IOException {
reading(f).renameTo(creating(new File(target.remote)));
Files.move(fileToPath(reading(f)), fileToPath(creating(new File(target.remote))), LinkOption.NOFOLLOW_LINKS);
return null;
}
});
@@ -2024,8 +2027,8 @@ public Void invoke(File f, VirtualChannel channel) throws IOException {
File targetFile = new File(target.remote);
File targetDir = targetFile.getParentFile();
filterNonNull().mkdirs(targetDir);
Files.createDirectories(Util.fileToPath(targetDir));
Files.copy(Util.fileToPath(reading(f)), Util.fileToPath(writing(targetFile)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
Files.createDirectories(fileToPath(targetDir));
Files.copy(fileToPath(reading(f)), fileToPath(writing(targetFile)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return null;
}
});
@@ -2957,11 +2960,12 @@ private File deleting(File f) {
return f;
}

private boolean mkdirs(File dir) {
private boolean mkdirs(File dir) throws IOException {
if (dir.exists()) return false;

filterNonNull().mkdirs(dir);
return dir.mkdirs();
Files.createDirectories(fileToPath(dir));
return true;
}

private File mkdirsE(File dir) throws IOException {
@@ -13,6 +13,8 @@
import java.util.List;
import java.util.regex.Pattern;

import static hudson.Util.fileToPath;

/**
* Adds more to commons-io.
*
@@ -51,20 +53,11 @@ public static void copy(InputStream in, File out) throws IOException {
* This method returns the 'dir' parameter so that the method call flows better.
*/
public static File mkdirs(File dir) throws IOException {
if(dir.mkdirs() || dir.exists())
return dir;

// following Ant <mkdir> task to avoid possible race condition.
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// ignore
return Files.createDirectories(fileToPath(dir)).toFile();
} catch (UnsupportedOperationException e) {
throw new IOException(e);
}

if (dir.mkdirs() || dir.exists())
return dir;

throw new IOException("Failed to create a directory at "+dir);
}

/**
@@ -134,7 +127,7 @@ public static int mode(File f) throws PosixException {
if (Util.NATIVE_CHMOD_MODE) {
return PosixAPI.jnr().stat(f.getPath()).mode();
} else {
return Util.permissionsToMode(Files.getPosixFilePermissions(Util.fileToPath(f)));
return Util.permissionsToMode(Files.getPosixFilePermissions(fileToPath(f)));
}
} catch (IOException cause) {
PosixException e = new PosixException("Unable to get file permissions", null);

0 comments on commit 09bcc5d

Please sign in to comment.
You can’t perform that action at this time.