提交 3871d777 编写于 作者: S Stephen Connolly

[JENKINS-43531] Plugins may not be expecting InvalidPathException

- Plugins should really be expecting it, but as historically they have not, may as well convert to IOE for them
上级 33942677
......@@ -32,6 +32,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.KeyPair;
......@@ -134,6 +135,8 @@ public class PrivateKeyProvider {
byte[] bytes = new byte[(int) f.length()];
dis.readFully(bytes);
return new String(bytes);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......
......@@ -25,6 +25,7 @@ package hudson;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import com.google.common.collect.Lists;
import hudson.Plugin.DummyImpl;
......@@ -127,6 +128,8 @@ public class ClassicPluginStrategy implements PluginStrategy {
String firstLine;
try (InputStream manifestHeaderInput = Files.newInputStream(archive.toPath())) {
firstLine = IOUtils.readFirstLine(manifestHeaderInput, "UTF-8");
} catch (InvalidPathException e) {
throw new IOException(e);
}
if (firstLine.startsWith("Manifest-Version:")) {
// this is the manifest already
......@@ -138,6 +141,8 @@ public class ClassicPluginStrategy implements PluginStrategy {
// Read the manifest
try (InputStream manifestInput = Files.newInputStream(archive.toPath())) {
return new Manifest(manifestInput);
} catch (InvalidPathException e) {
throw new IOException(e);
}
} catch (IOException e) {
throw new IOException("Failed to load " + archive, e);
......@@ -171,6 +176,8 @@ public class ClassicPluginStrategy implements PluginStrategy {
}
try (InputStream fin = Files.newInputStream(manifestFile.toPath())) {
manifest = new Manifest(fin);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......
......@@ -78,6 +78,7 @@ import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
......@@ -1469,8 +1470,13 @@ public final class FilePath implements Serializable {
act(new SecureFileCallable<Void>() {
private static final long serialVersionUID = -5094638816500738429L;
public Void invoke(File f, VirtualChannel channel) throws IOException {
if(!f.exists())
Files.newOutputStream(creating(f).toPath()).close();
if(!f.exists()) {
try {
Files.newOutputStream(creating(f).toPath()).close();
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
if(!stating(f).setLastModified(timestamp))
throw new IOException("Failed to set the timestamp of "+f+" to "+timestamp);
return null;
......@@ -1750,8 +1756,13 @@ public final class FilePath implements Serializable {
* Reads this file.
*/
public InputStream read() throws IOException, InterruptedException {
if(channel==null)
return Files.newInputStream(reading(new File(remote)).toPath());
if(channel==null) {
try {
return Files.newInputStream(reading(new File(remote)).toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
final Pipe p = Pipe.createRemoteToLocal();
actAsync(new SecureFileCallable<Void>() {
......@@ -1762,6 +1773,8 @@ public final class FilePath implements Serializable {
try (InputStream fis = Files.newInputStream(reading(f).toPath());
OutputStream out = p.getOut()) {
org.apache.commons.io.IOUtils.copy(fis, out);
} catch (InvalidPathException e) {
p.error(new IOException(e));
} catch (Exception x) {
p.error(x);
}
......@@ -1862,7 +1875,11 @@ public final class FilePath implements Serializable {
if(channel==null) {
File f = new File(remote).getAbsoluteFile();
mkdirs(f.getParentFile());
return Files.newOutputStream(writing(f).toPath());
try {
return Files.newOutputStream(writing(f).toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
return act(new SecureFileCallable<OutputStream>() {
......@@ -1870,8 +1887,12 @@ public final class FilePath implements Serializable {
public OutputStream invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
f = f.getAbsoluteFile();
mkdirs(f.getParentFile());
OutputStream fos = Files.newOutputStream(writing(f).toPath());
return new RemoteOutputStream(fos);
try {
OutputStream fos = Files.newOutputStream(writing(f).toPath());
return new RemoteOutputStream(fos);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
});
}
......@@ -1891,6 +1912,8 @@ public final class FilePath implements Serializable {
try (OutputStream fos = Files.newOutputStream(writing(f).toPath());
Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos)) {
w.write(content);
} catch (InvalidPathException e) {
throw new IOException(e);
}
return null;
}
......@@ -1994,6 +2017,8 @@ public final class FilePath implements Serializable {
try (InputStream fis = Files.newInputStream(reading(f).toPath())) {
org.apache.commons.io.IOUtils.copy(fis, out);
return null;
} catch (InvalidPathException e) {
throw new IOException(e);
} finally {
out.close();
}
......
......@@ -32,6 +32,7 @@ import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.util.io.ArchiverFactory;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import hudson.model.listeners.RunListener;
import hudson.scm.SCM;
......@@ -218,6 +219,8 @@ public abstract class FileSystemProvisioner implements ExtensionPoint, Describab
File wss = new File(build.getRootDir(),"workspace.tgz");
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(wss.toPath()))) {
ws.archive(ArchiverFactory.TARGZ, os, glob);
} catch (InvalidPathException e) {
throw new IOException(e);
}
return new WorkspaceSnapshotImpl();
}
......
......@@ -26,6 +26,7 @@ package hudson;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import hudson.util.DualOutputStream;
import hudson.util.EncodingStream;
......@@ -155,6 +156,8 @@ public class Main {
ret = proc.join();
w.write("</log><result>"+ret+"</result><duration>"+(System.currentTimeMillis()-start)+"</duration></run>");
} catch (InvalidPathException e) {
throw new IOException(e);
}
URL location = new URL(jobURL, "postBuildResult");
......@@ -173,6 +176,8 @@ public class Main {
// send the data
try (InputStream in = Files.newInputStream(tmpFile.toPath())) {
org.apache.commons.io.IOUtils.copy(in, con.getOutputStream());
} catch (InvalidPathException e) {
throw new IOException(e);
}
if(con.getResponseCode()!=200) {
......
......@@ -30,6 +30,7 @@ import hudson.model.AdministrativeMonitor;
import hudson.model.Api;
import hudson.model.ModelObject;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.YesNoMaybe;
import jenkins.model.Jenkins;
import hudson.model.UpdateCenter;
......@@ -496,8 +497,11 @@ public class PluginWrapper implements Comparable<PluginWrapper>, ModelObject {
*/
public void disable() throws IOException {
// creates an empty file
OutputStream os = Files.newOutputStream(disableFile.toPath());
os.close();
try (OutputStream os = Files.newOutputStream(disableFile.toPath())) {
os.close();
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
/**
......
......@@ -23,6 +23,7 @@
*/
package hudson;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import com.sun.jna.Native;
......@@ -202,6 +203,8 @@ public class Util {
int len;
while ((len = r.read(buf, 0, buf.length)) > 0)
str.append(buf, 0, len);
} catch (InvalidPathException e) {
throw new IOException(e);
}
return str.toString();
......@@ -283,7 +286,11 @@ public class Util {
if(!f.delete() && f.exists()) {
// trouble-shooting.
Files.deleteIfExists(f.toPath());
try {
Files.deleteIfExists(f.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
// see https://java.net/projects/hudson/lists/users/archive/2008-05/message/357
// I suspect other processes putting files in this directory
......@@ -811,6 +818,8 @@ public class Util {
public static String getDigestOf(@Nonnull File file) throws IOException {
try (InputStream is = Files.newInputStream(file.toPath())) {
return getDigestOf(new BufferedInputStream(is));
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......@@ -1143,7 +1152,11 @@ public class Util {
* Creates an empty file.
*/
public static void touch(@Nonnull File file) throws IOException {
Files.newOutputStream(file.toPath()).close();
try {
Files.newOutputStream(file.toPath()).close();
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
/**
......
......@@ -26,6 +26,7 @@ package hudson;
import hudson.security.ACLContext;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.StandardOpenOption;
import jenkins.util.SystemProperties;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
......@@ -278,7 +279,7 @@ public class WebAppMain implements ServletContextListener {
private void recordBootAttempt(File home) {
try (OutputStream o=Files.newOutputStream(BootFailure.getBootFailureFile(home).toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
o.write((new Date().toString() + System.getProperty("line.separator", "\n")).toString().getBytes());
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
LOGGER.log(WARNING, "Failed to record boot attempts",e);
}
}
......
......@@ -34,6 +34,7 @@ import hudson.model.Descriptor;
import hudson.util.AtomicFileWriter;
import hudson.util.XStream2;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
......@@ -140,7 +141,7 @@ public final class XmlFile {
}
try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
return xs.fromXML(in);
} catch (XStreamException | Error e) {
} catch (XStreamException | Error | InvalidPathException e) {
throw new IOException("Unable to read "+file,e);
}
}
......@@ -157,7 +158,7 @@ public final class XmlFile {
try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
// TODO: expose XStream the driver from XStream
return xs.unmarshal(DEFAULT_DRIVER.createReader(in), o);
} catch (XStreamException | Error e) {
} catch (XStreamException | Error | InvalidPathException e) {
throw new IOException("Unable to read "+file,e);
}
}
......@@ -201,14 +202,18 @@ public final class XmlFile {
* @return Reader for the file. should be close externally once read.
*/
public Reader readRaw() throws IOException {
InputStream fileInputStream = Files.newInputStream(file.toPath());
try {
return new InputStreamReader(fileInputStream, sniffEncoding());
} catch(IOException ex) {
// Exception may happen if we fail to find encoding or if this encoding is unsupported.
// In such case we close the underlying stream and rethrow.
Util.closeAndLogFailures(fileInputStream, LOGGER, "FileInputStream", file.toString());
throw ex;
InputStream fileInputStream = Files.newInputStream(file.toPath());
try {
return new InputStreamReader(fileInputStream, sniffEncoding());
} catch (IOException ex) {
// Exception may happen if we fail to find encoding or if this encoding is unsupported.
// In such case we close the underlying stream and rethrow.
Util.closeAndLogFailures(fileInputStream, LOGGER, "FileInputStream", file.toString());
throw ex;
}
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......@@ -288,7 +293,9 @@ public final class XmlFile {
// in such a case, assume UTF-8 rather than fail, since Jenkins internally always write XML in UTF-8
return "UTF-8";
} catch (SAXException e) {
throw new IOException("Failed to detect encoding of "+file,e);
throw new IOException("Failed to detect encoding of " + file, e);
} catch (InvalidPathException e) {
throw new IOException(e);
} catch (ParserConfigurationException e) {
throw new AssertionError(e); // impossible
}
......
......@@ -33,6 +33,7 @@ import hudson.util.jna.SHELLEXECUTEINFO;
import hudson.util.jna.Shell32;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import hudson.AbortException;
import hudson.Extension;
......@@ -310,6 +311,8 @@ public class WindowsInstallerLink extends ManagementLink {
} finally {
try (InputStream fin = Files.newInputStream(new File(pwd,"redirect.log").toPath())) {
IOUtils.copy(fin, out.getLogger());
} catch (InvalidPathException e) {
// ignore;
}
}
}
......
......@@ -35,6 +35,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import javax.servlet.ServletException;
import org.apache.commons.fileupload.FileItem;
......@@ -212,6 +213,8 @@ public class FileParameterValue extends ParameterValue {
} else {
response.serveFile(request, data, lastModified, contentLength, originalFileName);
}
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
}
......@@ -241,7 +244,11 @@ public class FileParameterValue extends ParameterValue {
}
public InputStream getInputStream() throws IOException {
return Files.newInputStream(file.toPath());
try {
return Files.newInputStream(file.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
public String getContentType() {
......@@ -265,7 +272,7 @@ public class FileParameterValue extends ParameterValue {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return IOUtils.toByteArray(inputStream);
}
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
throw new Error(e);
}
}
......@@ -302,7 +309,11 @@ public class FileParameterValue extends ParameterValue {
@Deprecated
public OutputStream getOutputStream() throws IOException {
return Files.newOutputStream(file.toPath());
try {
return Files.newOutputStream(file.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
@Override
......
......@@ -64,6 +64,7 @@ import hudson.model.queue.WorkUnitContext;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.security.QueueItemAuthenticatorProvider;
import jenkins.util.Timer;
import hudson.triggers.SafeTimerTask;
......@@ -384,6 +385,8 @@ public class Queue extends ResourceController implements Saveable {
if (j != null)
j.scheduleBuild();
}
} catch (InvalidPathException e) {
throw new IOException(e);
}
// discard the queue file now that we are done
queueFile.delete();
......
......@@ -42,6 +42,7 @@ import hudson.console.ConsoleNote;
import hudson.console.ModelHyperlinkNote;
import hudson.console.PlainTextConsoleOutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.StandardOpenOption;
import jenkins.util.SystemProperties;
import hudson.Util;
......@@ -1367,12 +1368,16 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
if (logFile.exists() ) {
// Checking if a ".gz" file was return
InputStream fis = Files.newInputStream(logFile.toPath());
if (logFile.getName().endsWith(".gz")) {
return new GZIPInputStream(fis);
} else {
return fis;
}
try {
InputStream fis = Files.newInputStream(logFile.toPath());
if (logFile.getName().endsWith(".gz")) {
return new GZIPInputStream(fis);
} else {
return fis;
}
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
String message = "No such file: " + logFile;
......@@ -1804,7 +1809,12 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
// don't do buffering so that what's written to the listener
// gets reflected to the file immediately, which can then be
// served to the browser immediately
OutputStream logger = Files.newOutputStream(getLogFile().toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
OutputStream logger;
try {
logger = Files.newOutputStream(getLogFile().toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (InvalidPathException e) {
throw new IOException(e);
}
RunT build = job.getBuild();
// Global log filters
......
......@@ -32,6 +32,7 @@ import hudson.PluginWrapper;
import hudson.ProxyConfiguration;
import hudson.security.ACLContext;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import hudson.Util;
import hudson.XmlFile;
......@@ -1137,7 +1138,7 @@ public class UpdateCenter extends AbstractModelObject implements Saveable, OnMas
out.write(buf,0,len);
job.status = job.new Installing(total == -1 ? -1 : cin.getCount() * 100 / total);
}
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
throw new IOException("Failed to load "+src+" to "+tmp,e);
} finally {
t.setName(oldName);
......
......@@ -40,6 +40,7 @@ import hudson.util.HttpResponses;
import hudson.util.Secret;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
......@@ -519,6 +520,8 @@ public class JDKInstaller extends ToolInstaller {
tmp.getParentFile().mkdirs();
try (OutputStream out = Files.newOutputStream(tmp.toPath())) {
IOUtils.copy(m.getResponseBodyAsStream(), out);
} catch (InvalidPathException e) {
throw new IOException(e);
}
tmp.renameTo(cache);
......
......@@ -33,6 +33,7 @@ import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
/**
* Buffered {@link FileWriter} that supports atomic operations.
......@@ -71,7 +72,11 @@ public class AtomicFileWriter extends Writer {
destFile = f;
if (encoding==null)
encoding = Charset.defaultCharset().name();
core = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(tmpFile.toPath()),encoding));
try {
core = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(tmpFile.toPath()), encoding));
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
@Override
......
......@@ -33,6 +33,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
......@@ -78,7 +79,11 @@ public class CompressedFile {
public OutputStream write() throws IOException {
if(gz.exists())
gz.delete();
return Files.newOutputStream(file.toPath());
try {
return Files.newOutputStream(file.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
/**
......@@ -86,11 +91,19 @@ public class CompressedFile {
*/
public InputStream read() throws IOException {
if(file.exists())
return Files.newInputStream(file.toPath());
try {
return Files.newInputStream(file.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
// check if the compressed file exists
if(gz.exists())
return new GZIPInputStream(Files.newInputStream(gz.toPath()));
try {
return new GZIPInputStream(Files.newInputStream(gz.toPath()));
} catch (InvalidPathException e) {
throw new IOException(e);
}
// no such file
throw new FileNotFoundException(file.getName());
......
......@@ -4,6 +4,7 @@ import hudson.Functions;
import hudson.os.PosixAPI;
import hudson.os.PosixException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import org.apache.commons.io.LineIterator;
import java.io.*;
......@@ -29,12 +30,16 @@ public class IOUtils {
public static void copy(File src, OutputStream out) throws IOException {
try (InputStream in = Files.newInputStream(src.toPath())) {
org.apache.commons.io.IOUtils.copy(in, out);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
public static void copy(InputStream in, File out) throws IOException {
try (OutputStream fos = Files.newOutputStream(out.toPath())) {
org.apache.commons.io.IOUtils.copy(in, fos);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......
......@@ -6,6 +6,7 @@ import hudson.model.TaskListener;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.io.BufferedReader;
......@@ -112,6 +113,8 @@ public class SecretRewriter {
buf.append(line.substring(copied));
out.println(buf.toString());
}
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......@@ -141,7 +144,7 @@ public class SecretRewriter {
String canonical;
try {
canonical = dir.toPath().toRealPath(new LinkOption[0]).toString();
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
canonical = dir.getAbsolutePath(); //
}
if (!callstack.add(canonical)) {
......
......@@ -42,7 +42,9 @@ import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -96,7 +98,15 @@ public class StreamTaskListener extends AbstractTaskListener implements Serializ
// don't do buffering so that what's written to the listener
// gets reflected to the file immediately, which can then be
// served to the browser immediately
this(Files.newOutputStream(out.toPath()),charset);
this(Files.newOutputStream(asPath(out)), charset);
}
private static Path asPath(File out) throws IOException {
try {
return out.toPath();
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
/**
......@@ -113,7 +123,7 @@ public class StreamTaskListener extends AbstractTaskListener implements Serializ
// gets reflected to the file immediately, which can then be
// served to the browser immediately
this(Files.newOutputStream(
out.toPath(),
asPath(out),
StandardOpenOption.CREATE, append ? StandardOpenOption.APPEND: StandardOpenOption.TRUNCATE_EXISTING
),
charset
......
......@@ -26,6 +26,7 @@ package hudson.util;
import com.google.common.collect.*;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
......@@ -71,6 +72,8 @@ public class TextFile {
String line;
while ((line = in.readLine()) != null)
w.println(line);
} catch (InvalidPathException e) {
throw new IOException(e);
}
return out.toString();
}
......@@ -101,7 +104,7 @@ public class TextFile {
}
}
};
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
throw new RuntimeException(e);
}
}
......
......@@ -29,6 +29,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
......@@ -57,7 +58,7 @@ import java.nio.file.StandardOpenOption;
try {
current = Files.newOutputStream(out.toPath(), StandardOpenOption.CREATE,
appendOnNextOpen ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING);
} catch (FileNotFoundException | NoSuchFileException e) {
} catch (FileNotFoundException | NoSuchFileException | InvalidPathException e) {
throw new IOException("Failed to open "+out,e);
}
return current;
......
......@@ -29,6 +29,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.FileUtils;
......@@ -55,7 +56,7 @@ public class RewindableFileOutputStream extends OutputStream {
FileUtils.forceMkdir(out.getParentFile());
try {
current = Files.newOutputStream(out.toPath(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (FileNotFoundException | NoSuchFileException e) {
} catch (FileNotFoundException | NoSuchFileException | InvalidPathException e) {
throw new IOException("Failed to open "+out,e);
}
}
......
......@@ -36,6 +36,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarConstants;
......@@ -108,7 +109,7 @@ final class TarArchiver extends Archiver {
while ((len = in.read(buf)) >= 0) {
tar.write(buf, 0, len);
}
} catch (IOException e) {// log the exception in any case
} catch (IOException | InvalidPathException e) {// log the exception in any case
IOException ioE = new IOException("Error writing to tar file from: " + file, e);
throw ioE;
}
......
......@@ -28,6 +28,7 @@ import hudson.util.FileVisitor;
import hudson.util.IOUtils;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
......@@ -75,6 +76,8 @@ final class ZipArchiver extends Archiver {
int len;
while((len=in.read(buf))>=0)
zip.write(buf,0,len);
} catch (InvalidPathException e) {
throw new IOException(e);
}
zip.closeEntry();
}
......
......@@ -7,6 +7,7 @@ import hudson.Util;
import hudson.model.AdministrativeMonitor;
import hudson.util.jna.Kernel32Utils;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import jenkins.model.Jenkins;
......@@ -55,6 +56,8 @@ public class HsErrPidList extends AdministrativeMonitor {
try {
try (FileChannel ch = FileChannel.open(getSecretKeyFile().toPath(), StandardOpenOption.READ)) {
map = ch.map(MapMode.READ_ONLY,0,1);
} catch (InvalidPathException e) {
throw new IOException(e);
}
scan("./hs_err_pid%p.log");
......
......@@ -7,6 +7,7 @@ import hudson.util.TextFile;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import javax.crypto.Cipher;
......@@ -82,6 +83,8 @@ public class DefaultConfidentialStore extends ConfidentialStore {
}
} catch (GeneralSecurityException e) {
throw new IOException("Failed to persist the key: "+key.getId(),e);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......@@ -106,6 +109,8 @@ public class DefaultConfidentialStore extends ConfidentialStore {
}
} catch (GeneralSecurityException e) {
throw new IOException("Failed to load the key: "+key.getId(),e);
} catch (InvalidPathException e) {
throw new IOException(e);
} catch (IOException x) {
if (x.getCause() instanceof BadPaddingException) {
return null; // broken somehow
......
......@@ -3,6 +3,7 @@ package jenkins.util;
import com.trilead.ssh2.crypto.Base64;
import hudson.util.FormValidation;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.io.output.NullOutputStream;
......@@ -177,6 +178,8 @@ public class JSONSignatureValidator {
Certificate certificate;
try (InputStream in = Files.newInputStream(cert.toPath())) {
certificate = cf.generateCertificate(in);
} catch (InvalidPathException e) {
throw new IOException(e);
} catch (CertificateException e) {
LOGGER.log(Level.WARNING, String.format("Files in %s are expected to be either "
+ "certificates or .txt files documenting the certificates, "
......
......@@ -295,7 +295,11 @@ public abstract class VirtualFile implements Comparable<VirtualFile>, Serializab
if (isIllegalSymlink()) {
throw new FileNotFoundException(f.getPath());
}
return Files.newInputStream(f.toPath());
try {
return Files.newInputStream(f.toPath());
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
private boolean isIllegalSymlink() { // TODO JENKINS-26838
try {
......
package jenkins.util.io;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.model.Jenkins;
import java.io.File;
......@@ -59,7 +60,7 @@ public class FileBoolean {
file.getParentFile().mkdirs();
Files.newOutputStream(file.toPath()).close();
get(); // update state
} catch (IOException e) {
} catch (IOException | InvalidPathException e) {
LOGGER.log(Level.WARNING, "Failed to touch "+file);
}
}
......
......@@ -2,6 +2,7 @@ package jenkins.util.xml;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import org.apache.commons.io.IOUtils;
import org.kohsuke.accmod.Restricted;
......@@ -142,6 +143,8 @@ public final class XMLUtils {
try (InputStream fileInputStream = Files.newInputStream(file.toPath());
InputStreamReader fileReader = new InputStreamReader(fileInputStream, encoding)) {
return parse(fileReader);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册