[JENKINS-48227] Use "Files.createTempDirectory" to create temp directory (#3161)

* Use "Files.createTempDirectory" to create temp directory instead
See SonarQube critical vulnerability squid:S2976 (tag: owasp-a9)
https://next.sonarqube.com/sonarqube/coding_rules#rule_key=squid%3AS2976

* [JENKINS-48227]
Creating a utility "static Path toPath(File file) throws IOException" method,
which wraps InvalidPathException to IOException so that it will be checked.
- also fixed public static final reference
- fixed broken test cases
- added new test cases for toPath() and createTempDir()

* Revert back to public static int

* adjustments due to merges

* Add posix check to determine if Posix FileAttributes should be included
in call to Files.createTempDirectory()

* Remove reference to private element

* Updated to use explicit imports
This commit is contained in:
Larry Singleton authored and Oleg Nenashev committed 2017-12-22 17:38:57 +01:00
1 parent 001a33c747
commit 8e78ab1c66
2 files changed
+60 -13

No files matched your search

+31 -5
View File
@@ -77,13 +77,20 @@ import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.LinkOption;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
@@ -113,6 +120,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
@@ -1403,17 +1411,35 @@ public final class FilePath implements Serializable {
* @return
* The new FilePath pointing to the temporary directory
* @since 1.311
* @see File#createTempFile(String, String)
* @see Files#createTempDirectory(Path, String, FileAttribute[])
*/
public FilePath createTempDir(final String prefix, final String suffix) throws IOException, InterruptedException {
try {
String[] s;
if (StringUtils.isBlank(suffix)) {
s = new String[]{prefix, "tmp"}; // see File.createTempFile - tmp is used if suffix is null
} else {
s = new String[]{prefix, suffix};
}
String name = StringUtils.join(s, ".");
return new FilePath(this,act(new SecureFileCallable<String>() {
private static final long serialVersionUID = 1L;
public String invoke(File dir, VirtualChannel channel) throws IOException {
File f = File.createTempFile(prefix, suffix, dir);
f.delete();
f.mkdir();
return f.getName();
Path tempPath;
final boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
if (isPosix) {
tempPath = Files.createTempDirectory(Util.fileToPath(dir), name,
PosixFilePermissions.asFileAttribute(EnumSet.allOf(PosixFilePermission.class)));
} else {
tempPath = Files.createTempDirectory(Util.fileToPath(dir), name, new FileAttribute<?>[] {});
}
if (tempPath.toFile() == null) {
throw new IOException("Failed to obtain file from path " + dir + " on " + remote);
}
return tempPath.toFile().getName();
}
}));
} catch (IOException e) {