Skip to content

Commit

Permalink
Merge pull request #2914 from jglick/FilePath.list
Browse files Browse the repository at this point in the history
[JENKINS-44942] - FilePath.list() & .listDirectories() null safety
  • Loading branch information
oleg-nenashev committed Jun 16, 2017
2 parents 70f80e9 + 064bdbb commit a5dc255
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 11 additions & 1 deletion core/src/main/java/hudson/FilePath.java
Expand Up @@ -127,6 +127,7 @@
import static hudson.Util.deleteFile;
import static hudson.Util.fixEmpty;
import static hudson.Util.isSymlink;
import java.util.Collections;

/**
* {@link File} like object with remoting support.
Expand Down Expand Up @@ -1636,6 +1637,7 @@ public Integer invoke(File f, VirtualChannel channel) throws IOException {
* <p>
* This method returns direct children of the directory denoted by the 'this' object.
*/
@Nonnull
public List<FilePath> list() throws IOException, InterruptedException {
return list((FileFilter)null);
}
Expand All @@ -1645,6 +1647,7 @@ public List<FilePath> list() throws IOException, InterruptedException {
*
* @return can be empty but never null. Doesn't contain "." and ".."
*/
@Nonnull
public List<FilePath> listDirectories() throws IOException, InterruptedException {
return list(new DirectoryFilter());
}
Expand All @@ -1665,6 +1668,7 @@ public boolean accept(File f) {
* If this {@link FilePath} represents a remote path,
* the filter object will be executed on the remote machine.
*/
@Nonnull
public List<FilePath> list(final FileFilter filter) throws IOException, InterruptedException {
if (filter != null && !(filter instanceof Serializable)) {
throw new IllegalArgumentException("Non-serializable filter of " + filter.getClass());
Expand All @@ -1673,7 +1677,9 @@ public List<FilePath> list(final FileFilter filter) throws IOException, Interrup
private static final long serialVersionUID = 1L;
public List<FilePath> invoke(File f, VirtualChannel channel) throws IOException {
File[] children = reading(f).listFiles(filter);
if(children ==null) return null;
if (children == null) {
return Collections.emptyList();
}

ArrayList<FilePath> r = new ArrayList<FilePath>(children.length);
for (File child : children)
Expand All @@ -1692,6 +1698,7 @@ public List<FilePath> invoke(File f, VirtualChannel channel) throws IOException
* @return
* can be empty but always non-null.
*/
@Nonnull
public FilePath[] list(final String includes) throws IOException, InterruptedException {
return list(includes, null);
}
Expand All @@ -1706,6 +1713,7 @@ public FilePath[] list(final String includes) throws IOException, InterruptedExc
* can be empty but always non-null.
* @since 1.407
*/
@Nonnull
public FilePath[] list(final String includes, final String excludes) throws IOException, InterruptedException {
return list(includes, excludes, true);
}
Expand All @@ -1721,6 +1729,7 @@ public FilePath[] list(final String includes, final String excludes) throws IOEx
* can be empty but always non-null.
* @since 1.465
*/
@Nonnull
public FilePath[] list(final String includes, final String excludes, final boolean defaultExcludes) throws IOException, InterruptedException {
return act(new SecureFileCallable<FilePath[]>() {
private static final long serialVersionUID = 1L;
Expand All @@ -1742,6 +1751,7 @@ public FilePath[] invoke(File f, VirtualChannel channel) throws IOException {
* @return
* A set of relative file names from the base directory.
*/
@Nonnull
private static String[] glob(File dir, String includes, String excludes, boolean defaultExcludes) throws IOException {
if(isAbsolute(includes))
throw new IOException("Expecting Ant GLOB pattern, but saw '"+includes+"'. See http://ant.apache.org/manual/Types/fileset.html for syntax");
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/java/jenkins/util/VirtualFile.java
Expand Up @@ -365,9 +365,6 @@ private static final class FilePathVF extends VirtualFile {
@Override public VirtualFile[] list() throws IOException {
try {
List<FilePath> kids = f.list();
if (kids == null) {
return new VirtualFile[0];
}
VirtualFile[] vfs = new VirtualFile[kids.size()];
for (int i = 0; i < vfs.length; i++) {
vfs[i] = forFilePath(kids.get(i));
Expand Down

0 comments on commit a5dc255

Please sign in to comment.