mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-10-29 20:45:20 +00:00
[JENKINS-47324] - Reduce usage of File.mkdirs() in FilePath and IOUtils (#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
This commit is contained in:
1 parent
b4ff724c0c
commit
09bcc5d653
2 files changed
+17
-20
No files matched your search
@@ -79,6 +79,7 @@ import java.net.URL;
|
||||
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 org.kohsuke.stapler.Stapler;
|
||||
|
||||
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 final class FilePath implements Serializable {
|
||||
// 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 @@ public final class FilePath implements Serializable {
|
||||
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 final class FilePath implements Serializable {
|
||||
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 final class FilePath implements Serializable {
|
||||
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 @@ public final class FilePath implements Serializable {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user