Skip to content

Commit

Permalink
Merge pull request #2849 from stephenc/jenkins-43531
Browse files Browse the repository at this point in the history
[JENKINS-43531] Plugins may not be expecting InvalidPathException
  • Loading branch information
oleg-nenashev committed Apr 22, 2017
2 parents 77a9f02 + 3871d77 commit d1d72ca
Show file tree
Hide file tree
Showing 31 changed files with 209 additions and 46 deletions.
3 changes: 3 additions & 0 deletions cli/src/main/java/hudson/cli/PrivateKeyProvider.java
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -134,6 +135,8 @@ private static String readPemFile(File f) throws IOException{
byte[] bytes = new byte[(int) f.length()];
dis.readFully(bytes);
return new String(bytes);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/hudson/ClassicPluginStrategy.java
Expand Up @@ -25,6 +25,7 @@

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;
Expand Down Expand Up @@ -127,6 +128,8 @@ private static Manifest loadLinkedManifest(File archive) throws IOException {
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
Expand All @@ -138,6 +141,8 @@ private static Manifest loadLinkedManifest(File archive) throws IOException {
// 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);
Expand Down Expand Up @@ -171,6 +176,8 @@ private static Manifest loadLinkedManifest(File archive) throws IOException {
}
try (InputStream fin = Files.newInputStream(manifestFile.toPath())) {
manifest = new Manifest(fin);
} catch (InvalidPathException e) {
throw new IOException(e);
}
}

Expand Down
39 changes: 32 additions & 7 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -78,6 +78,7 @@
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;
Expand Down Expand Up @@ -1469,8 +1470,13 @@ public void touch(final long timestamp) throws IOException, InterruptedException
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;
Expand Down Expand Up @@ -1750,8 +1756,13 @@ private static String[] glob(File dir, String includes, String excludes, boolean
* 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>() {
Expand All @@ -1762,6 +1773,8 @@ public Void invoke(File f, VirtualChannel channel) throws IOException, Interrupt
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);
}
Expand Down Expand Up @@ -1862,16 +1875,24 @@ public OutputStream write() throws IOException, InterruptedException {
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>() {
private static final long serialVersionUID = 1L;
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);
}
}
});
}
Expand All @@ -1891,6 +1912,8 @@ public Void invoke(File f, VirtualChannel channel) throws IOException {
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;
}
Expand Down Expand Up @@ -1994,6 +2017,8 @@ public Void invoke(File f, VirtualChannel channel) throws IOException {
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();
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/FileSystemProvisioner.java
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -218,6 +219,8 @@ public WorkspaceSnapshot snapshot(AbstractBuild<?, ?> build, FilePath ws, String
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();
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/hudson/Main.java
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -155,6 +156,8 @@ public static int remotePost(String[] args) throws Exception {
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");
Expand All @@ -173,6 +176,8 @@ public static int remotePost(String[] args) throws Exception {
// 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) {
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/hudson/PluginWrapper.java
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -496,8 +497,11 @@ public void enable() throws IOException {
*/
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);
}
}

/**
Expand Down
17 changes: 15 additions & 2 deletions core/src/main/java/hudson/Util.java
Expand Up @@ -23,6 +23,7 @@
*/
package hudson;

import java.nio.file.InvalidPathException;
import jenkins.util.SystemProperties;
import com.sun.jna.Native;

Expand Down Expand Up @@ -202,6 +203,8 @@ public static String loadFile(@Nonnull File logfile, @Nonnull Charset charset) t
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();
Expand Down Expand Up @@ -283,7 +286,11 @@ private static void tryOnceDeleteFile(File f) throws IOException {

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
Expand Down Expand Up @@ -811,6 +818,8 @@ public static String getDigestOf(@Nonnull String text) {
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);
}
}

Expand Down Expand Up @@ -1143,7 +1152,11 @@ public static String xmlEscape(@Nonnull String text) {
* 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);
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/WebAppMain.java
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -278,7 +279,7 @@ public void joinInit() throws InterruptedException {
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);
}
}
Expand Down
27 changes: 17 additions & 10 deletions core/src/main/java/hudson/XmlFile.java
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -140,7 +141,7 @@ public Object read() throws IOException {
}
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);
}
}
Expand All @@ -157,7 +158,7 @@ public Object unmarshal( Object o ) throws IOException {
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);
}
}
Expand Down Expand Up @@ -201,14 +202,18 @@ public String toString() {
* @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);
}
}

Expand Down Expand Up @@ -288,7 +293,9 @@ private void attempt() throws Eureka {
// 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
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/lifecycle/WindowsInstallerLink.java
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -310,6 +311,8 @@ static int runElevated(File jenkinsExe, String command, TaskListener out, File p
} finally {
try (InputStream fin = Files.newInputStream(new File(pwd,"redirect.log").toPath())) {
IOUtils.copy(fin, out.getLogger());
} catch (InvalidPathException e) {
// ignore;
}
}
}
Expand Down

0 comments on commit d1d72ca

Please sign in to comment.