From 46b540e5eb83a2c3d2df0b0d4f6fb3890ef866ae Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 21 May 2009 18:10:58 +0000 Subject: [PATCH] added more convenience methods git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@18259 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/FilePath.java | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 72b6101128..c08bef859c 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. */