Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-44942] - FilePath.list() & .listDirectories() null safety #2914

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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