diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 72b61011281d9302226c897e3df0d73546d7aaaa..c08bef859c026bb3237c6926c38d0623c0881f4d 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -929,6 +929,22 @@ public final class FilePath implements Serializable { return list((FileFilter)null); } + /** + * List up subdirectories. + * + * @return can be empty but never null. Doesn't contain "." and ".." + */ + public List listDirectories() throws IOException, InterruptedException { + return list(new DirectoryFilter()); + } + + private static final class DirectoryFilter implements FileFilter, Serializable { + public boolean accept(File f) { + return f.isDirectory(); + } + private static final long serialVersionUID = 1L; + } + /** * List up files in this directory, just like {@link File#listFiles(FileFilter)}. * @@ -1015,6 +1031,18 @@ public final class FilePath implements Serializable { return p.getIn(); } + /** + * Reads this file into a string, by using the current system encoding. + */ + public String readToString() throws IOException { + InputStream in = read(); + try { + return IOUtils.toString(in); + } finally { + in.close(); + } + } + /** * Writes to this file. * If this file already exists, it will be overwritten. @@ -1087,6 +1115,30 @@ public final class FilePath implements Serializable { }); } + /** + * Moves all the contents of this directory into the specified directory, then delete this directory itself. + * + * @since 1.308. + */ + public void moveAllChildrenTo(final FilePath target) throws IOException, InterruptedException { + if(this.channel != target.channel) { + throw new IOException("pullUpTo target must be on the same host"); + } + act(new FileCallable() { + public Void invoke(File f, VirtualChannel channel) throws IOException { + File t = new File(target.getRemote()); + + for(File child : f.listFiles()) { + File target = new File(t, child.getName()); + if(!child.renameTo(target)) + throw new IOException("Failed to rename "+child+" to "+target); + } + f.delete(); + return null; + } + }); + } + /** * Copies this file to the specified target. */