FilePath.java 65.9 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Eric Lefevre-Ardant, Erik Ramfelt, Michael B. Donohue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
K
kohsuke 已提交
24 25
package hudson;

26 27 28 29
import hudson.Launcher.LocalLauncher;
import hudson.Launcher.RemoteLauncher;
import hudson.model.Hudson;
import hudson.model.TaskListener;
K
kohsuke 已提交
30 31
import hudson.model.AbstractProject;
import hudson.model.Item;
K
kohsuke 已提交
32 33
import hudson.remoting.Callable;
import hudson.remoting.Channel;
34
import hudson.remoting.DelegatingCallable;
35
import hudson.remoting.Future;
K
kohsuke 已提交
36 37 38
import hudson.remoting.Pipe;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel;
K
kohsuke 已提交
39
import hudson.remoting.RemoteInputStream;
40
import hudson.util.IOException2;
41
import hudson.util.HeadBufferingStream;
K
kohsuke 已提交
42
import hudson.util.FormValidation;
K
kohsuke 已提交
43
import static hudson.util.jna.GNUCLibrary.LIBC;
K
kohsuke 已提交
44
import static hudson.Util.fixEmpty;
K
kohsuke 已提交
45
import static hudson.FilePath.TarCompression.GZIP;
K
kohsuke 已提交
46
import hudson.os.PosixAPI;
47 48
import hudson.org.apache.tools.tar.TarOutputStream;
import hudson.org.apache.tools.tar.TarInputStream;
K
kohsuke 已提交
49 50
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
51
import org.apache.tools.ant.Project;
K
kohsuke 已提交
52 53
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;
54
import org.apache.tools.tar.TarEntry;
55 56
import org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
K
kohsuke 已提交
57
import org.apache.commons.io.IOUtils;
58
import org.apache.commons.fileupload.FileItem;
K
kohsuke 已提交
59
import org.kohsuke.stapler.Stapler;
K
kohsuke 已提交
60

61 62
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
K
kohsuke 已提交
63
import java.io.File;
K
kohsuke 已提交
64 65 66 67
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
K
kohsuke 已提交
68
import java.io.IOException;
K
kohsuke 已提交
69 70 71 72 73 74
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.Writer;
K
kohsuke 已提交
75
import java.io.OutputStreamWriter;
76
import java.net.URI;
K
kohsuke 已提交
77
import java.net.URL;
K
kohsuke 已提交
78
import java.net.URLConnection;
K
kohsuke 已提交
79 80
import java.util.ArrayList;
import java.util.List;
K
kohsuke 已提交
81
import java.util.StringTokenizer;
82 83
import java.util.Arrays;
import java.util.Comparator;
K
kohsuke 已提交
84
import java.util.regex.Pattern;
85
import java.util.concurrent.ExecutionException;
86 87
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
88 89
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
K
kohsuke 已提交
90
import java.util.zip.ZipInputStream;
K
kohsuke 已提交
91

K
kohsuke 已提交
92 93
import com.sun.jna.Native;

K
kohsuke 已提交
94
/**
K
kohsuke 已提交
95
 * {@link File} like object with remoting support.
K
kohsuke 已提交
96 97
 *
 * <p>
K
kohsuke 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
 * Unlike {@link File}, which always implies a file path on the current computer,
 * {@link FilePath} represents a file path on a specific slave or the master.
 *
 * Despite that, {@link FilePath} can be used much like {@link File}. It exposes
 * a bunch of operations (and we should add more operations as long as they are
 * generally useful), and when invoked against a file on a remote node, {@link FilePath}
 * executes the necessary code remotely, thereby providing semi-transparent file
 * operations.
 *
 * <h2>Using {@link FilePath} smartly</h2>
 * <p>
 * The transparency makes it easy to write plugins without worrying too much about
 * remoting, by making it works like NFS, where remoting happens at the file-system
 * later.
 *
 * <p>
 * But one should note that such use of remoting may not be optional. Sometimes,
 * it makes more sense to move some computation closer to the data, as opposed to
 * move the data to the computation. For example, if you are just computing a MD5
 * digest of a file, then it would make sense to do the digest on the host where
 * the file is located, as opposed to send the whole data to the master and do MD5
 * digesting there.
 *
 * <p>
 * {@link FilePath} supports this "code migration" by in the
 * {@link #act(FileCallable)} method. One can pass in a custom implementation
 * of {@link FileCallable}, to be executed on the node where the data is located.
 * The following code shows the example:
 *
 * <pre>
 * FilePath file = ...;
 *
 * // make 'file' a fresh empty directory.
 * file.act(new FileCallable&lt;Void>() {
 *   // if 'file' is on a different node, this FileCallable will
 *   // be transfered to that node and executed there.
 *   public Void invoke(File f,VirtualChannel channel) {
 *     // f and file represents the same thing
 *     f.deleteContents();
 *     f.mkdirs();
 *   }
 * });
 * </pre>
K
kohsuke 已提交
141 142
 *
 * <p>
K
kohsuke 已提交
143
 * When {@link FileCallable} is transfered to a remote node, it will be done so
E
elefevre 已提交
144
 * by using the same Java serialization scheme that the remoting module uses.
K
kohsuke 已提交
145 146 147 148 149 150 151
 * See {@link Channel} for more about this. 
 *
 * <p>
 * {@link FilePath} itself can be sent over to a remote node as a part of {@link Callable}
 * serialization. For example, sending a {@link FilePath} of a remote node to that
 * node causes {@link FilePath} to become "local". Similarly, sending a
 * {@link FilePath} that represents the local computer causes it to become "remote."
K
kohsuke 已提交
152 153 154
 *
 * @author Kohsuke Kawaguchi
 */
K
kohsuke 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
public final class FilePath implements Serializable {
    /**
     * When this {@link FilePath} represents the remote path,
     * this field is always non-null on master (the field represents
     * the channel to the remote slave.) When transferred to a slave via remoting,
     * this field reverts back to null, since it's transient.
     *
     * When this {@link FilePath} represents a path on the master,
     * this field is null on master. When transferred to a slave via remoting,
     * this field becomes non-null, representing the {@link Channel}
     * back to the master.
     *
     * This is used to determine whether we are running on the master or the slave.
     */
    private transient VirtualChannel channel;

    // since the platform of the slave might be different, can't use java.io.File
K
kohsuke 已提交
172 173
    private final String remote;

K
kohsuke 已提交
174 175 176 177 178 179 180
    /**
     * Creates a {@link FilePath} that represents a path on the given node.
     *
     * @param channel
     *      To create a path that represents a remote path, pass in a {@link Channel}
     *      that's connected to that machine. If null, that means the local file path.
     */
K
kohsuke 已提交
181 182
    public FilePath(VirtualChannel channel, String remote) {
        this.channel = channel;
K
kohsuke 已提交
183 184 185 186
        this.remote = remote;
    }

    /**
K
kohsuke 已提交
187 188 189 190 191
     * To create {@link FilePath} that represents a "local" path.
     *
     * <p>
     * A "local" path means a file path on the computer where the
     * constructor invocation happened.
K
kohsuke 已提交
192
     */
K
kohsuke 已提交
193 194 195
    public FilePath(File localPath) {
        this.channel = null;
        this.remote = localPath.getPath();
K
kohsuke 已提交
196 197
    }

K
kohsuke 已提交
198 199 200 201 202
    /**
     * Construct a path starting with a base location.
     * @param base starting point for resolution, and defines channel
     * @param rel a path which if relative will be resolved against base
     */
K
kohsuke 已提交
203
    public FilePath(FilePath base, String rel) {
K
kohsuke 已提交
204
        this.channel = base.channel;
K
kohsuke 已提交
205
        if(isAbsolute(rel)) {
K
kohsuke 已提交
206 207 208
            // absolute
            this.remote = rel;
        } else 
K
kohsuke 已提交
209 210 211 212 213 214 215
        if(base.isUnix()) {
            this.remote = base.remote+'/'+rel;
        } else {
            this.remote = base.remote+'\\'+rel;
        }
    }

K
kohsuke 已提交
216 217 218 219
    private static boolean isAbsolute(String rel) {
        return rel.startsWith("/") || DRIVE_PATTERN.matcher(rel).matches();
    }

K
kohsuke 已提交
220 221
    private static final Pattern DRIVE_PATTERN = Pattern.compile("[A-Za-z]:\\\\.+");

K
kohsuke 已提交
222 223 224 225
    /**
     * Checks if the remote path is Unix.
     */
    private boolean isUnix() {
226 227 228 229
        // if the path represents a local path, there' no need to guess.
        if(!isRemote())
            return File.pathSeparatorChar!=';';
            
230 231 232 233 234 235
        // note that we can't use the usual File.pathSeparator and etc., as the OS of
        // the machine where this code runs and the OS that this FilePath refers to may be different.

        // Windows absolute path is 'X:\...', so this is usually a good indication of Windows path
        if(remote.length()>3 && remote.charAt(1)==':' && remote.charAt(2)=='\\')
            return false;
K
kohsuke 已提交
236 237 238 239 240 241 242 243 244
        // Windows can handle '/' as a path separator but Unix can't,
        // so err on Unix side
        return remote.indexOf("\\")==-1;
    }

    public String getRemote() {
        return remote;
    }

245 246 247 248 249 250 251 252 253 254 255
    /**
     * Creates a zip file from this directory or a file and sends that to the given output stream.
     */
    public void createZipArchive(OutputStream os) throws IOException, InterruptedException {
        final OutputStream out = (channel!=null)?new RemoteOutputStream(os):os;
        act(new FileCallable<Void>() {
            private transient byte[] buf;
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                buf = new byte[8192];

                ZipOutputStream zip = new ZipOutputStream(out);
256
                zip.setEncoding(System.getProperty("file.encoding"));
257 258 259 260 261 262
                scan(f,zip,"");
                zip.close();
                return null;
            }

            private void scan(File f, ZipOutputStream zip, String path) throws IOException {
263 264 265
                // Bitmask indicating directories in 'external attributes' of a ZIP archive entry.
                final long BITMASK_IS_DIRECTORY = 1<<4;
              
266 267
                if (f.canRead()) {
                    if(f.isDirectory()) {
268 269 270 271
                        ZipEntry dirZipEntry = new ZipEntry(path+f.getName()+'/');
                        // Setting this bit explicitly is needed by some unzipping applications (see HUDSON-3294).
                        dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
                        zip.putNextEntry(dirZipEntry);
272 273 274 275 276 277 278 279 280 281 282 283
                        zip.closeEntry();
                        for( File child : f.listFiles() )
                            scan(child,zip,path+f.getName()+'/');
                    } else {
                        zip.putNextEntry(new ZipEntry(path+f.getName()));
                        FileInputStream in = new FileInputStream(f);
                        int len;
                        while((len=in.read(buf))>0)
                            zip.write(buf,0,len);
                        in.close();
                        zip.closeEntry();
                    }
284 285
                }
            }
K
kohsuke 已提交
286 287
            
            private static final long serialVersionUID = 1L;
288 289 290
        });
    }

K
kohsuke 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    /**
     * Creates a zip file from this directory by only including the files that match the given glob.
     *
     * @param glob
     *      Ant style glob, like "**&#x2F;*.xml". If empty or null, this method
     *      works like {@link #createZipArchive(OutputStream)}
     *
     * @since 1.129
     */
    public void createZipArchive(OutputStream os, final String glob) throws IOException, InterruptedException {
        if(glob==null || glob.length()==0) {
            createZipArchive(os);
            return;
        }
        
        final OutputStream out = (channel!=null)?new RemoteOutputStream(os):os;
        act(new FileCallable<Void>() {
            public Void invoke(File dir, VirtualChannel channel) throws IOException {
                byte[] buf = new byte[8192];

                ZipOutputStream zip = new ZipOutputStream(out);
312
                zip.setEncoding(System.getProperty("file.encoding"));
K
kohsuke 已提交
313
                for( String entry : glob(dir,glob) ) {
314 315 316 317 318 319 320 321 322 323
                    File file = new File(dir,entry);
                    if (file.canRead()) {
                        zip.putNextEntry(new ZipEntry(dir.getName()+'/'+entry));
                        FileInputStream in = new FileInputStream(file);
                        int len;
                        while((len=in.read(buf))>0)
                            zip.write(buf,0,len);
                        in.close();
                        zip.closeEntry();
                    }
K
kohsuke 已提交
324 325 326 327 328 329 330 331 332 333
                }

                zip.close();
                return null;
            }

            private static final long serialVersionUID = 1L;
        });
    }

K
kohsuke 已提交
334 335 336 337 338 339
    /**
     * When this {@link FilePath} represents a zip file, extracts that zip file.
     *
     * @param target
     *      Target directory to expand files to. All the necessary directories will be created.
     * @since 1.248
K
kohsuke 已提交
340
     * @see #unzipFrom(InputStream)
K
kohsuke 已提交
341
     */
K
kohsuke 已提交
342
    public void unzip(final FilePath target) throws IOException, InterruptedException {
K
kohsuke 已提交
343 344
        target.act(new FileCallable<Void>() {
            public Void invoke(File dir, VirtualChannel channel) throws IOException {
K
kohsuke 已提交
345
                unzip(dir,FilePath.this.read());
K
kohsuke 已提交
346 347
                return null;
            }
K
kohsuke 已提交
348 349 350
            private static final long serialVersionUID = 1L;
        });
    }
K
kohsuke 已提交
351

K
kohsuke 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    /**
     * When this {@link FilePath} represents a tar file, extracts that tar file.
     *
     * @param target
     *      Target directory to expand files to. All the necessary directories will be created.
     * @param compression
     *      Compression mode of this tar file.
     * @since 1.292
     * @see #untarFrom(InputStream, TarCompression)
     */
    public void untar(final FilePath target, final TarCompression compression) throws IOException, InterruptedException {
        target.act(new FileCallable<Void>() {
            public Void invoke(File dir, VirtualChannel channel) throws IOException {
                readFromTar(FilePath.this.getName(),dir,compression.extract(FilePath.this.read()));
                return null;
            }
            private static final long serialVersionUID = 1L;
        });
    }

K
kohsuke 已提交
372 373 374 375 376 377
    /**
     * Reads the given InputStream as a zip file and extracts it into this directory.
     *
     * @param _in
     *      The stream will be closed by this method after it's fully read.
     * @since 1.283
K
kohsuke 已提交
378
     * @see #unzip(FilePath)
K
kohsuke 已提交
379 380 381 382 383 384 385 386
     */
    public void unzipFrom(InputStream _in) throws IOException, InterruptedException {
        final InputStream in = new RemoteInputStream(_in);
        act(new FileCallable<Void>() {
            public Void invoke(File dir, VirtualChannel channel) throws IOException {
                unzip(dir, in);
                return null;
            }
K
kohsuke 已提交
387 388 389 390
            private static final long serialVersionUID = 1L;
        });
    }

K
kohsuke 已提交
391 392
    private void unzip(File dir, InputStream in) throws IOException {
        dir = dir.getAbsoluteFile();    // without absolutization, getParentFile below seems to fail
K
kohsuke 已提交
393
        ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
K
kohsuke 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        java.util.zip.ZipEntry e;

        try {
            while((e=zip.getNextEntry())!=null) {
                File f = new File(dir,e.getName());
                if(e.isDirectory()) {
                    f.mkdirs();
                } else {
                    File p = f.getParentFile();
                    if(p!=null) p.mkdirs();
                    FileOutputStream out = new FileOutputStream(f);
                    try {
                        IOUtils.copy(zip, out);
                    } finally {
                        out.close();
                    }
                    f.setLastModified(e.getTime());
                    zip.closeEntry();
                }
            }
        } finally {
            zip.close();
        }
    }

K
kohsuke 已提交
419 420 421 422 423 424 425 426 427 428 429
    /**
     * Absolutizes this {@link FilePath} and returns the new one.
     */
    public FilePath absolutize() throws IOException, InterruptedException {
        return new FilePath(channel,act(new FileCallable<String>() {
            public String invoke(File f, VirtualChannel channel) throws IOException {
                return f.getAbsolutePath();
            }
        }));
    }

K
kohsuke 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    /**
     * Supported tar file compression methods.
     */
    public enum TarCompression {
        NONE {
            public InputStream extract(InputStream in) {
                return in;
            }
            public OutputStream compress(OutputStream out) {
                return out;
            }
        },
        GZIP {
            public InputStream extract(InputStream _in) throws IOException {
                HeadBufferingStream in = new HeadBufferingStream(_in,SIDE_BUFFER_SIZE);
                try {
446
                    return new GZIPInputStream(in,8192);
K
kohsuke 已提交
447 448 449 450 451 452 453
                } catch (IOException e) {
                    // various people reported "java.io.IOException: Not in GZIP format" here, so diagnose this problem better
                    in.fillSide();
                    throw new IOException2(e.getMessage()+"\nstream="+Util.toHexString(in.getSideBuffer()),e);
                }
            }
            public OutputStream compress(OutputStream out) throws IOException {
454
                return new GZIPOutputStream(new BufferedOutputStream(out));
K
kohsuke 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
            }
        };

        public abstract InputStream extract(InputStream in) throws IOException;
        public abstract OutputStream compress(OutputStream in) throws IOException;
    }

    /**
     * Reads the given InputStream as a tar file and extracts it into this directory.
     *
     * @param _in
     *      The stream will be closed by this method after it's fully read.
     * @param compression
     *      The compression method in use.
     * @since 1.292
     */
    public void untarFrom(InputStream _in, final TarCompression compression) throws IOException, InterruptedException {
        try {
            final InputStream in = new RemoteInputStream(_in);
            act(new FileCallable<Void>() {
                public Void invoke(File dir, VirtualChannel channel) throws IOException {
476
                    readFromTar("input stream",dir, compression.extract(in));
K
kohsuke 已提交
477 478 479 480 481 482 483 484 485
                    return null;
                }
                private static final long serialVersionUID = 1L;
            });
        } finally {
            IOUtils.closeQuietly(_in);
        }
    }

K
kohsuke 已提交
486
    /**
K
kohsuke 已提交
487
     * Given a tgz/zip file, extracts it to the given target directory, if necessary.
K
kohsuke 已提交
488 489 490 491 492 493
     *
     * <p>
     * This method is a convenience method designed for installing a binary package to a location
     * that supports upgrade and downgrade. Specifically,
     *
     * <ul>
K
kohsuke 已提交
494
     * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}.
K
kohsuke 已提交
495
     * <li>The timestamp of the .tgz file is left in the installation directory upon extraction.
K
kohsuke 已提交
496 497
     * <li>If the timestamp left in the directory doesn't match with the timestamp of the current archive file,
     *     the directory contents will be discarded and the archive file will be re-extracted.
K
kohsuke 已提交
498
     * <li>If the connection is refused but the target directory already exists, it is left alone.
K
kohsuke 已提交
499 500
     * </ul>
     *
K
kohsuke 已提交
501 502
     * @param archive
     *      The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header.
K
kohsuke 已提交
503 504 505 506 507 508 509 510 511
     *      (Most common usage is to get this from {@link ClassLoader#getResource(String)})
     * @param listener
     *      If non-null, a message will be printed to this listener once this method decides to
     *      extract an archive.
     * @return
     *      true if the archive was extracted. false if the extraction was skipped because the target directory
     *      was considered up to date.
     * @since 1.299
     */
K
kohsuke 已提交
512
    public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message) throws IOException, InterruptedException {
K
kohsuke 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        URLConnection con;
        try {
            con = archive.openConnection();
            con.connect();
        } catch (IOException x) {
            if (this.exists()) {
                // Cannot connect now, so assume whatever was last unpacked is still OK.
                if (listener != null) {
                    listener.getLogger().println("Skipping installation of " + archive + " to " + remote + ": " + x);
                }
                return false;
            } else {
                throw x;
            }
        }
K
kohsuke 已提交
528 529 530 531 532 533 534 535 536 537 538
        long sourceTimestamp = con.getLastModified();
        FilePath timestamp = this.child(".timestamp");

        if(this.exists()) {
            if(timestamp.exists() && sourceTimestamp ==timestamp.lastModified())
                return false;   // already up to date
            this.deleteContents();
        }

        if(listener!=null)
            listener.getLogger().println(message);
K
kohsuke 已提交
539 540 541 542
        if(archive.toExternalForm().endsWith(".zip"))
            unzipFrom(con.getInputStream());
        else
            untarFrom(con.getInputStream(),GZIP);
K
kohsuke 已提交
543 544 545 546
        timestamp.touch(sourceTimestamp);
        return true;
    }

K
kohsuke 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    /**
     * Reads the URL on the current VM, and writes all the data to this {@link FilePath}
     * (this is different from resolving URL remotely.)
     *
     * @since 1.293
     */
    public void copyFrom(URL url) throws IOException, InterruptedException {
        InputStream in = url.openStream();
        try {
            copyFrom(in);
        } finally {
            in.close();
        }
    }

    /**
     * Replaces the content of this file by the data from the given {@link InputStream}.
     *
     * @since 1.293
     */
    public void copyFrom(InputStream in) throws IOException, InterruptedException {
        OutputStream os = write();
        try {
            IOUtils.copy(in, os);
        } finally {
            os.close();
        }
    }
K
kohsuke 已提交
575 576 577 578 579 580 581 582 583

    /**
     * Conveniene method to call {@link FilePath#copyTo(FilePath)}.
     * 
     * @since 1.311
     */
    public void copyFrom(FilePath src) throws IOException, InterruptedException {
        src.copyTo(this);
    }
K
kohsuke 已提交
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    /**
     * Place the data from {@link FileItem} into the file location specified by this {@link FilePath} object.
     */
    public void copyFrom(FileItem file) throws IOException, InterruptedException {
        if(channel==null) {
            try {
                file.write(new File(remote));
            } catch (IOException e) {
                throw e;
            } catch (Exception e) {
                throw new IOException2(e);
            }
        } else {
            InputStream i = file.getInputStream();
            OutputStream o = write();
            try {
                IOUtils.copy(i,o);
            } finally {
                o.close();
                i.close();
            }
        }
    }

K
kohsuke 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    /**
     * Code that gets executed on the machine where the {@link FilePath} is local.
     * Used to act on {@link FilePath}.
     *
     * @see FilePath#act(FileCallable)
     */
    public static interface FileCallable<T> extends Serializable {
        /**
         * Performs the computational task on the node where the data is located.
         *
         * @param f
         *      {@link File} that represents the local file that {@link FilePath} has represented.
         * @param channel
         *      The "back pointer" of the {@link Channel} that represents the communication
         *      with the node from where the code was sent.
         */
        T invoke(File f, VirtualChannel channel) throws IOException;
    }

    /**
     * Executes some program on the machine that this {@link FilePath} exists,
     * so that one can perform local file operations.
     */
    public <T> T act(final FileCallable<T> callable) throws IOException, InterruptedException {
633 634 635 636
        return act(callable,callable.getClass().getClassLoader());
    }

    private <T> T act(final FileCallable<T> callable, ClassLoader cl) throws IOException, InterruptedException {
K
kohsuke 已提交
637 638 639
        if(channel!=null) {
            // run this on a remote system
            try {
640
                return channel.call(new FileCallableWrapper<T>(callable,cl));
K
kohsuke 已提交
641 642
            } catch (AbortException e) {
                throw e;    // pass through so that the caller can catch it as AbortException
K
kohsuke 已提交
643 644 645 646 647 648 649 650 651 652
            } catch (IOException e) {
                // wrap it into a new IOException so that we get the caller's stack trace as well.
                throw new IOException2("remote file operation failed",e);
            }
        } else {
            // the file is on the local machine.
            return callable.invoke(new File(remote), Hudson.MasterComputer.localChannel);
        }
    }

653 654 655 656 657 658 659 660 661 662 663 664 665 666
    /**
     * Executes some program on the machine that this {@link FilePath} exists,
     * so that one can perform local file operations.
     */
    public <T> Future<T> actAsync(final FileCallable<T> callable) throws IOException, InterruptedException {
        try {
            return (channel!=null ? channel : Hudson.MasterComputer.localChannel)
                .callAsync(new FileCallableWrapper<T>(callable));
        } catch (IOException e) {
            // wrap it into a new IOException so that we get the caller's stack trace as well.
            throw new IOException2("remote file operation failed",e);
        }
    }

K
kohsuke 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    /**
     * Executes some program on the machine that this {@link FilePath} exists,
     * so that one can perform local file operations.
     */
    public <V,E extends Throwable> V act(Callable<V,E> callable) throws IOException, InterruptedException, E {
        if(channel!=null) {
            // run this on a remote system
            return channel.call(callable);
        } else {
            // the file is on the local machine
            return callable.call();
        }
    }

    /**
     * Converts this file to the URI, relative to the machine
     * on which this file is available.
     */
    public URI toURI() throws IOException, InterruptedException {
        return act(new FileCallable<URI>() {
            public URI invoke(File f, VirtualChannel channel) {
                return f.toURI();
            }
        });
    }

K
kohsuke 已提交
693 694 695
    /**
     * Creates this directory.
     */
K
kohsuke 已提交
696
    public void mkdirs() throws IOException, InterruptedException {
697
        if(!act(new FileCallable<Boolean>() {
K
kohsuke 已提交
698
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
699 700 701 702 703 704 705 706 707 708 709
                if(f.mkdirs() || f.exists())
                    return true;    // OK

                // following Ant <mkdir> task to avoid possible race condition.
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // ignore
                }

                return f.mkdirs() || f.exists();
K
kohsuke 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
            }
        }))
            throw new IOException("Failed to mkdirs: "+remote);
    }

    /**
     * Deletes this directory, including all its contents recursively.
     */
    public void deleteRecursive() throws IOException, InterruptedException {
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Util.deleteRecursive(f);
                return null;
            }
        });
K
kohsuke 已提交
725 726 727 728 729
    }

    /**
     * Deletes all the contents of this directory, but not the directory itself
     */
K
kohsuke 已提交
730 731 732 733 734 735 736
    public void deleteContents() throws IOException, InterruptedException {
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Util.deleteContentsRecursive(f);
                return null;
            }
        });
K
kohsuke 已提交
737 738 739 740 741 742 743 744
    }

    /**
     * Gets just the file name portion.
     *
     * This method assumes that the file name is the same between local and remote.
     */
    public String getName() {
745 746 747 748 749
        String r = remote;
        if(r.endsWith("\\") || r.endsWith("/"))
            r = r.substring(0,r.length()-1);

        int len = r.length()-1;
K
kohsuke 已提交
750
        while(len>=0) {
751
            char ch = r.charAt(len);
K
kohsuke 已提交
752 753 754 755 756
            if(ch=='\\' || ch=='/')
                break;
            len--;
        }

757
        return r.substring(len+1);
K
kohsuke 已提交
758 759 760
    }

    /**
K
kohsuke 已提交
761 762 763
     * The same as {@link FilePath#FilePath(FilePath,String)} but more OO.
     * @param rel a relative or absolute path
     * @return a file on the same channel
K
kohsuke 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
     */
    public FilePath child(String rel) {
        return new FilePath(this,rel);
    }

    /**
     * Gets the parent file.
     */
    public FilePath getParent() {
        int len = remote.length()-1;
        while(len>=0) {
            char ch = remote.charAt(len);
            if(ch=='\\' || ch=='/')
                break;
            len--;
        }

K
kohsuke 已提交
781
        return new FilePath( channel, remote.substring(0,len) );
K
kohsuke 已提交
782 783 784
    }

    /**
K
kohsuke 已提交
785
     * Creates a temporary file in the directory that this {@link FilePath} object designates.
K
kohsuke 已提交
786
     */
K
kohsuke 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    public FilePath createTempFile(final String prefix, final String suffix) throws IOException, InterruptedException {
        try {
            return new FilePath(this,act(new FileCallable<String>() {
                public String invoke(File dir, VirtualChannel channel) throws IOException {
                    File f = File.createTempFile(prefix, suffix, dir);
                    return f.getName();
                }
            }));
        } catch (IOException e) {
            throw new IOException2("Failed to create a temp file on "+remote,e);
        }
    }

    /**
     * Creates a temporary file in this directory and set the contents by the
     * given text (encoded in the platform default encoding)
     */
    public FilePath createTextTempFile(final String prefix, final String suffix, final String contents) throws IOException, InterruptedException {
805 806 807 808 809 810 811 812
        return createTextTempFile(prefix,suffix,contents,true);
    }

    /**
     * Creates a temporary file in this directory and set the contents by the
     * given text (encoded in the platform default encoding)
     */
    public FilePath createTextTempFile(final String prefix, final String suffix, final String contents, final boolean inThisDirectory) throws IOException, InterruptedException {
K
kohsuke 已提交
813
        try {
K
kohsuke 已提交
814
            return new FilePath(channel,act(new FileCallable<String>() {
K
kohsuke 已提交
815
                public String invoke(File dir, VirtualChannel channel) throws IOException {
816
                    if(!inThisDirectory)
K
kohsuke 已提交
817
                        dir = new File(System.getProperty("java.io.tmpdir"));
818 819
                    else
                        dir.mkdirs();
820 821 822 823 824 825 826

                    File f;
                    try {
                        f = File.createTempFile(prefix, suffix, dir);
                    } catch (IOException e) {
                        throw new IOException2("Failed to create a temporary directory in "+dir,e);
                    }
K
kohsuke 已提交
827 828 829 830 831

                    Writer w = new FileWriter(f);
                    w.write(contents);
                    w.close();

K
kohsuke 已提交
832
                    return f.getAbsolutePath();
K
kohsuke 已提交
833 834
                }
            }));
K
kohsuke 已提交
835
        } catch (IOException e) {
K
kohsuke 已提交
836
            throw new IOException2("Failed to create a temp file on "+remote,e);
K
kohsuke 已提交
837
        }
K
kohsuke 已提交
838 839
    }

K
kohsuke 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
    /**
     * Creates a temporary directory inside the directory represented by 'this'
     * @since 1.311
     */
    public FilePath createTempDir(final String prefix, final String suffix) throws IOException, InterruptedException {
        try {
            return new FilePath(this,act(new FileCallable<String>() {
                public String invoke(File dir, VirtualChannel channel) throws IOException {
                    File f = File.createTempFile(prefix, suffix, dir);
                    f.delete();
                    f.mkdir();
                    return f.getName();
                }
            }));
        } catch (IOException e) {
            throw new IOException2("Failed to create a temp directory on "+remote,e);
        }
    }

K
kohsuke 已提交
859 860 861
    /**
     * Deletes this file.
     */
K
kohsuke 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    public boolean delete() throws IOException, InterruptedException {
        return act(new FileCallable<Boolean>() {
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
                return f.delete();
            }
        });
    }

    /**
     * Checks if the file exists.
     */
    public boolean exists() throws IOException, InterruptedException {
        return act(new FileCallable<Boolean>() {
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
                return f.exists();
            }
        });
K
kohsuke 已提交
879 880
    }

K
kohsuke 已提交
881 882 883 884 885
    /**
     * Gets the last modified time stamp of this file, by using the clock
     * of the machine where this file actually resides.
     *
     * @see File#lastModified()
K
kohsuke 已提交
886
     * @see #touch(long)
K
kohsuke 已提交
887 888 889 890 891 892 893
     */
    public long lastModified() throws IOException, InterruptedException {
        return act(new FileCallable<Long>() {
            public Long invoke(File f, VirtualChannel channel) throws IOException {
                return f.lastModified();
            }
        });
K
kohsuke 已提交
894 895
    }

K
kohsuke 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
    /**
     * Creates a file (if not already exist) and sets the timestamp.
     *
     * @since 1.299
     */
    public void touch(final long timestamp) throws IOException, InterruptedException {
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                if(!f.exists())
                    new FileOutputStream(f).close();
                if(!f.setLastModified(timestamp))
                    throw new IOException("Failed to set the timestamp of "+f+" to "+timestamp);
                return null;
            }
        });
    }

K
kohsuke 已提交
913 914 915 916 917 918 919 920 921
    /**
     * Checks if the file is a directory.
     */
    public boolean isDirectory() throws IOException, InterruptedException {
        return act(new FileCallable<Boolean>() {
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
                return f.isDirectory();
            }
        });
K
kohsuke 已提交
922
    }
K
kohsuke 已提交
923 924 925
    
    /**
     * Returns the file size in bytes.
K
kohsuke 已提交
926 927
     *
     * @since 1.129
K
kohsuke 已提交
928 929 930 931 932 933 934 935
     */
    public long length() throws IOException, InterruptedException {
        return act(new FileCallable<Long>() {
            public Long invoke(File f, VirtualChannel channel) throws IOException {
                return f.length();
            }
        });
    }
K
kohsuke 已提交
936

K
kohsuke 已提交
937 938 939 940 941
    /**
     * Sets the file permission.
     *
     * On Windows, no-op.
     *
K
kohsuke 已提交
942 943 944
     * @param mask
     *      File permission mask. To simplify the permission copying,
     *      if the parameter is -1, this method becomes no-op.
K
kohsuke 已提交
945
     * @since 1.303
K
kohsuke 已提交
946
     * @see #mode()
K
kohsuke 已提交
947 948
     */
    public void chmod(final int mask) throws IOException, InterruptedException {
K
kohsuke 已提交
949
        if(!isUnix() || mask==-1)   return;
K
kohsuke 已提交
950 951 952 953 954 955 956 957 958
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                if(LIBC.chmod(f.getAbsolutePath(),mask)!=0)
                    throw new IOException("Failed to chmod "+f+" : "+LIBC.strerror(Native.getLastError()));
                return null;
            }
        });
    }

K
kohsuke 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    /**
     * Gets the file permission bit mask.
     *
     * @return
     *      -1 on Windows, since such a concept doesn't make sense.
     * @since 1.311
     * @see #chmod(int)
     */
    public int mode() throws IOException, InterruptedException {
        if(!isUnix())   return -1;
        return act(new FileCallable<Integer>() {
            public Integer invoke(File f, VirtualChannel channel) throws IOException {
                return PosixAPI.get().stat(f.getPath()).mode();
            }
        });
    }

K
kohsuke 已提交
976
    /**
K
kohsuke 已提交
977 978 979 980 981 982 983 984 985
     * List up files and directories in this directory.
     *
     * <p>
     * This method returns direct children of the directory denoted by the 'this' object.
     */
    public List<FilePath> list() throws IOException, InterruptedException {
        return list((FileFilter)null);
    }

K
kohsuke 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    /**
     * List up subdirectories.
     *
     * @return can be empty but never null. Doesn't contain "." and ".."
     */
    public List<FilePath> 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;
    }

K
kohsuke 已提交
1002 1003
    /**
     * List up files in this directory, just like {@link File#listFiles(FileFilter)}.
K
kohsuke 已提交
1004 1005 1006 1007 1008 1009
     *
     * @param filter
     *      The optional filter used to narrow down the result.
     *      If non-null, must be {@link Serializable}.
     *      If this {@link FilePath} represents a remote path,
     *      the filter object will be executed on the remote machine.
K
kohsuke 已提交
1010
     */
K
kohsuke 已提交
1011
    public List<FilePath> list(final FileFilter filter) throws IOException, InterruptedException {
1012 1013 1014
        if (filter != null && !(filter instanceof Serializable)) {
            throw new IllegalArgumentException("Non-serializable filter of " + filter.getClass());
        }
K
kohsuke 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        return act(new FileCallable<List<FilePath>>() {
            public List<FilePath> invoke(File f, VirtualChannel channel) throws IOException {
                File[] children = f.listFiles(filter);
                if(children ==null)     return null;

                ArrayList<FilePath> r = new ArrayList<FilePath>(children.length);
                for (File child : children)
                    r.add(new FilePath(child));

                return r;
            }
1026
        }, (filter!=null?filter:this).getClass().getClassLoader());
K
kohsuke 已提交
1027 1028
    }

K
kohsuke 已提交
1029 1030 1031 1032
    /**
     * List up files in this directory that matches the given Ant-style filter.
     *
     * @param includes
K
kohsuke 已提交
1033
     *      See {@link FileSet} for the syntax. String like "foo/*.zip" or "foo/*&#42;/*.xml"
K
kohsuke 已提交
1034 1035
     * @return
     *      can be empty but always non-null.
K
kohsuke 已提交
1036 1037 1038 1039
     */
    public FilePath[] list(final String includes) throws IOException, InterruptedException {
        return act(new FileCallable<FilePath[]>() {
            public FilePath[] invoke(File f, VirtualChannel channel) throws IOException {
K
kohsuke 已提交
1040
                String[] files = glob(f,includes);
K
kohsuke 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

                FilePath[] r = new FilePath[files.length];
                for( int i=0; i<r.length; i++ )
                    r[i] = new FilePath(new File(f,files[i]));

                return r;
            }
        });
    }

K
kohsuke 已提交
1051 1052
    /**
     * Runs Ant glob expansion.
K
kohsuke 已提交
1053 1054 1055
     *
     * @return
     *      A set of relative file names from the base directory.
K
kohsuke 已提交
1056
     */
K
kohsuke 已提交
1057 1058 1059
    private static String[] glob(File dir, String includes) throws IOException {
        if(isAbsolute(includes))
            throw new IOException("Expecting Ant GLOB pattern, but saw '"+includes+"'. See http://ant.apache.org/manual/CoreTypes/fileset.html for syntax");
1060
        FileSet fs = Util.createFileSet(dir,includes);
K
kohsuke 已提交
1061 1062 1063 1064 1065
        DirectoryScanner ds = fs.getDirectoryScanner(new Project());
        String[] files = ds.getIncludedFiles();
        return files;
    }

K
kohsuke 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    /**
     * Reads this file.
     */
    public InputStream read() throws IOException {
        if(channel==null)
            return new FileInputStream(new File(remote));

        final Pipe p = Pipe.createRemoteToLocal();
        channel.callAsync(new Callable<Void,IOException>() {
            public Void call() throws IOException {
1076 1077 1078 1079 1080 1081 1082 1083 1084
                FileInputStream fis=null;
                try {
                    fis = new FileInputStream(new File(remote));
                    Util.copyStream(fis,p.getOut());
                    return null;
                } finally {
                    IOUtils.closeQuietly(fis);
                    IOUtils.closeQuietly(p.getOut());
                }
K
kohsuke 已提交
1085 1086 1087 1088 1089 1090
            }
        });

        return p.getIn();
    }

K
kohsuke 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    /**
     * 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();
        }
    }

K
kohsuke 已提交
1103 1104 1105
    /**
     * Writes to this file.
     * If this file already exists, it will be overwritten.
K
kohsuke 已提交
1106
     * If the directory doesn't exist, it will be created.
K
kohsuke 已提交
1107
     */
1108
    public OutputStream write() throws IOException, InterruptedException {
1109
        if(channel==null) {
1110
            File f = new File(remote).getAbsoluteFile();
1111 1112 1113
            f.getParentFile().mkdirs();
            return new FileOutputStream(f);
        }
K
kohsuke 已提交
1114

1115 1116
        return channel.call(new Callable<OutputStream,IOException>() {
            public OutputStream call() throws IOException {
1117
                File f = new File(remote).getAbsoluteFile();
K
kohsuke 已提交
1118 1119
                f.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(f);
1120
                return new RemoteOutputStream(fos);
K
kohsuke 已提交
1121 1122 1123 1124
            }
        });
    }

K
kohsuke 已提交
1125 1126 1127 1128 1129 1130 1131 1132
    /**
     * Overwrites this file by placing the given String as the content.
     *
     * @param encoding
     *      Null to use the platform default encoding.
     * @since 1.105
     */
    public void write(final String content, final String encoding) throws IOException, InterruptedException {
K
kohsuke 已提交
1133 1134
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
K
kohsuke 已提交
1135 1136
                f.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(f);
K
kohsuke 已提交
1137
                Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos);
K
kohsuke 已提交
1138 1139 1140
                try {
                    w.write(content);
                } finally {
K
kohsuke 已提交
1141
                    w.close();
K
kohsuke 已提交
1142 1143 1144 1145 1146 1147
                }
                return null;
            }
        });
    }

K
kohsuke 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    /**
     * Computes the MD5 digest of the file in hex string.
     */
    public String digest() throws IOException, InterruptedException {
        return act(new FileCallable<String>() {
            public String invoke(File f, VirtualChannel channel) throws IOException {
                return Util.getDigestOf(new FileInputStream(f));
            }
        });
    }

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    /**
     * Rename this file/directory to the target filepath.  This FilePath and the target must
     * be on the some host
     */
    public void renameTo(final FilePath target) throws IOException, InterruptedException {
    	if(this.channel != target.channel) {
    		throw new IOException("renameTo target must be on the same host");
    	}
        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
            	f.renameTo(new File(target.remote));
                return null;
K
kohsuke 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
            }
        });
    }

    /**
     * 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<Void>() {
            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;
1195 1196 1197 1198
            }
        });
    }

K
kohsuke 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
    /**
     * Copies this file to the specified target.
     */
    public void copyTo(FilePath target) throws IOException, InterruptedException {
        OutputStream out = target.write();
        try {
            copyTo(out);
        } finally {
            out.close();
        }
    }

K
kohsuke 已提交
1211 1212 1213 1214 1215
    /**
     * Copies this file to the specified target, with file permissions intact.
     * @since 1.311
     */
    public void copyToWithPermission(FilePath target) throws IOException, InterruptedException {
K
kohsuke 已提交
1216
        copyTo(target);
K
kohsuke 已提交
1217 1218 1219 1220
        // copy file permission
        target.chmod(mode());
    }

K
kohsuke 已提交
1221 1222 1223 1224 1225 1226 1227 1228
    /**
     * Sends the contents of this file into the given {@link OutputStream}.
     */
    public void copyTo(OutputStream os) throws IOException, InterruptedException {
        final OutputStream out = new RemoteOutputStream(os);

        act(new FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
1229 1230 1231 1232 1233 1234 1235 1236 1237
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(f);
                    Util.copyStream(fis,out);
                    return null;
                } finally {
                    IOUtils.closeQuietly(fis);
                    IOUtils.closeQuietly(out);
                }
K
kohsuke 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
            }
        });
    }

    /**
     * Remoting interface used for {@link FilePath#copyRecursiveTo(String, FilePath)}.
     *
     * TODO: this might not be the most efficient way to do the copy.
     */
    interface RemoteCopier {
K
kohsuke 已提交
1248 1249 1250 1251
        /**
         * @param fileName
         *      relative path name to the output file. Path separator must be '/'.
         */
K
kohsuke 已提交
1252 1253 1254 1255 1256
        void open(String fileName) throws IOException;
        void write(byte[] buf, int len) throws IOException;
        void close() throws IOException;
    }

K
kohsuke 已提交
1257 1258 1259 1260
    public int copyRecursiveTo(String fileMask, FilePath target) throws IOException, InterruptedException {
        return copyRecursiveTo(fileMask,null,target);
    }

K
kohsuke 已提交
1261 1262 1263
    /**
     * Copies the files that match the given file mask to the specified target node.
     *
K
kohsuke 已提交
1264 1265 1266 1267 1268
     * @param fileMask
     *      Ant GLOB pattern.
     *      String like "foo/bar/*.xml" Multiple patterns can be separated
     *      by ',', and whitespace can surround ',' (so that you can write
     *      "abc, def" and "abc,def" to mean the same thing.
K
kohsuke 已提交
1269 1270
     * @param excludes
     *      Files to be excluded. Can be null.
K
kohsuke 已提交
1271 1272 1273
     * @return
     *      the number of files copied.
     */
K
kohsuke 已提交
1274
    public int copyRecursiveTo(final String fileMask, final String excludes, final FilePath target) throws IOException, InterruptedException {
K
kohsuke 已提交
1275 1276 1277 1278
        if(this.channel==target.channel) {
            // local to local copy.
            return act(new FileCallable<Integer>() {
                public Integer invoke(File base, VirtualChannel channel) throws IOException {
1279
                    if(!base.exists())  return 0;
K
kohsuke 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
                    assert target.channel==null;

                    try {
                        class CopyImpl extends Copy {
                            private int copySize;

                            public CopyImpl() {
                                setProject(new org.apache.tools.ant.Project());
                            }

                            protected void doFileOperations() {
                                copySize = super.fileCopyMap.size();
                                super.doFileOperations();
                            }

                            public int getNumCopied() {
                                return copySize;
                            }
                        }

                        CopyImpl copyTask = new CopyImpl();
                        copyTask.setTodir(new File(target.remote));
1302
                        copyTask.addFileset(Util.createFileSet(base,fileMask,excludes));
1303
                        copyTask.setIncludeEmptyDirs(false);
K
kohsuke 已提交
1304 1305 1306 1307 1308 1309 1310 1311

                        copyTask.execute();
                        return copyTask.getNumCopied();
                    } catch (BuildException e) {
                        throw new IOException2("Failed to copy "+base+"/"+fileMask+" to "+target,e);
                    }
                }
            });
1312 1313 1314 1315 1316 1317 1318
        } else
        if(this.channel==null) {
            // local -> remote copy
            final Pipe pipe = Pipe.createLocalToRemote();

            Future<Void> future = target.actAsync(new FileCallable<Void>() {
                public Void invoke(File f, VirtualChannel channel) throws IOException {
1319
                    try {
1320
                        readFromTar(remote+'/'+fileMask, f,TarCompression.GZIP.extract(pipe.getIn()));
1321 1322 1323 1324
                        return null;
                    } finally {
                        pipe.getIn().close();
                    }
1325 1326
                }
            });
1327
            int r = writeToTar(new File(remote),fileMask,excludes,TarCompression.GZIP.compress(pipe.getOut()));
1328 1329 1330 1331 1332 1333
            try {
                future.get();
            } catch (ExecutionException e) {
                throw new IOException2(e);
            }
            return r;
K
kohsuke 已提交
1334
        } else {
1335 1336
            // remote -> local copy
            final Pipe pipe = Pipe.createRemoteToLocal();
K
kohsuke 已提交
1337

1338 1339
            Future<Integer> future = actAsync(new FileCallable<Integer>() {
                public Integer invoke(File f, VirtualChannel channel) throws IOException {
1340
                    try {
1341
                        return writeToTar(f,fileMask,excludes,TarCompression.GZIP.compress(pipe.getOut()));
1342 1343 1344
                    } finally {
                        pipe.getOut().close();
                    }
K
kohsuke 已提交
1345 1346
                }
            });
1347
            try {
1348
                readFromTar(remote+'/'+fileMask,new File(target.remote),TarCompression.GZIP.extract(pipe.getIn()));
1349 1350 1351 1352 1353 1354
            } catch (IOException e) {// BuildException or IOException
                try {
                    future.get(3,TimeUnit.SECONDS);
                    throw e;    // the remote side completed successfully, so the error must be local
                } catch (ExecutionException x) {
                    // report both errors
1355
                    throw new IOException2(Functions.printThrowable(e),x);
1356
                } catch (TimeoutException _) {
1357 1358 1359 1360
                    // remote is hanging
                    throw e;
                }
            }
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
            try {
                return future.get();
            } catch (ExecutionException e) {
                throw new IOException2(e);
            }
        }
    }

    /**
     * Writes to a tar stream and stores obtained files to the base dir.
     *
     * @return
     *      number of files/directories that are written.
     */
K
kohsuke 已提交
1375
    private Integer writeToTar(File baseDir, String fileMask, String excludes, OutputStream out) throws IOException {
1376
        FileSet fs = Util.createFileSet(baseDir,fileMask,excludes);
1377 1378 1379

        byte[] buf = new byte[8192];

1380 1381 1382 1383 1384 1385 1386 1387
        TarOutputStream tar = new TarOutputStream(new BufferedOutputStream(out) {
            // TarOutputStream uses TarBuffer internally,
            // which flushes the stream for each block. this creates unnecessary
            // data stream fragmentation, and flush request to a remote, which slows things down.
            public void flush() throws IOException {
                // so don't do anything in flush
            }
        });
1388
        tar.setLongFileMode(TarOutputStream.LONGFILE_GNU);
1389 1390 1391 1392 1393 1394 1395
        String[] files;
        if(baseDir.exists()) {
            DirectoryScanner ds = fs.getDirectoryScanner(new org.apache.tools.ant.Project());
            files = ds.getIncludedFiles();
        } else {
            files = new String[0];
        }
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        for( String f : files) {
            if(Functions.isWindows())
                f = f.replace('\\','/');

            File file = new File(baseDir, f);

            TarEntry te = new TarEntry(f);
            te.setModTime(file.lastModified());
            if(!file.isDirectory())
                te.setSize(file.length());

            tar.putNextEntry(te);

            if (!file.isDirectory()) {
                FileInputStream in = new FileInputStream(file);
                int len;
                while((len=in.read(buf))>=0)
                    tar.write(buf,0,len);
                in.close();
            }

            tar.closeEntry();
K
kohsuke 已提交
1418
        }
1419 1420 1421 1422 1423 1424 1425 1426 1427

        tar.close();

        return files.length;
    }

    /**
     * Reads from a tar stream and stores obtained files to the base dir.
     */
K
kohsuke 已提交
1428 1429
    private static void readFromTar(String name, File baseDir, InputStream in) throws IOException {
        TarInputStream t = new TarInputStream(in);
1430
        try {
K
kohsuke 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
            TarEntry te;
            while ((te = t.getNextEntry()) != null) {
                File f = new File(baseDir,te.getName());
                if(te.isDirectory()) {
                    f.mkdirs();
                } else {
                    File parent = f.getParentFile();
                    if (parent != null) parent.mkdirs();

                    OutputStream fos = new FileOutputStream(f);
                    try {
                        IOUtils.copy(t,fos);
                    } finally {
                        fos.close();
                    }
                    f.setLastModified(te.getModTime().getTime());
                    int mode = te.getMode()&0777;
                    if(mode!=0 && !Hudson.isWindows()) // be defensive
K
kohsuke 已提交
1449 1450 1451 1452 1453
                        try {
                            LIBC.chmod(f.getPath(),mode);
                        } catch (NoClassDefFoundError e) {
                            // be defensive. see http://www.nabble.com/-3.0.6--Site-copy-problem%3A-hudson.util.IOException2%3A--java.lang.NoClassDefFoundError%3A-Could-not-initialize-class--hudson.util.jna.GNUCLibrary-td23588879.html
                        }
K
kohsuke 已提交
1454 1455 1456 1457 1458 1459
                }
            }
        } catch(IOException e) {
            throw new IOException2("Failed to extract "+name,e);
        } finally {
            t.close();
1460
        }
K
kohsuke 已提交
1461 1462
    }

1463 1464
    /**
     * Creates a {@link Launcher} for starting processes on the node
K
typo.  
kohsuke 已提交
1465
     * that has this file.
1466
     * @since 1.89
1467
     */
1468
    public Launcher createLauncher(TaskListener listener) throws IOException, InterruptedException {
1469 1470 1471
        if(channel==null)
            return new LocalLauncher(listener);
        else
1472 1473 1474 1475 1476 1477 1478 1479
            return new RemoteLauncher(listener,channel,channel.call(new IsUnix()));
    }

    private static final class IsUnix implements Callable<Boolean,IOException> {
        public Boolean call() throws IOException {
            return File.pathSeparatorChar==':';
        }
        private static final long serialVersionUID = 1L;
1480 1481
    }

1482
    /**
K
kohsuke 已提交
1483
     * Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar")
1484 1485
     * against this directory, and try to point out the problem.
     *
K
kohsuke 已提交
1486
     * <p>
K
kohsuke 已提交
1487
     * This is useful in conjunction with {@link FormValidation}.
K
kohsuke 已提交
1488
     *
1489
     * @return
1490
     *      null if no error was found. Otherwise returns a human readable error message.
1491
     * @since 1.90
K
kohsuke 已提交
1492
     * @see #validateFileMask(FilePath, String)
1493
     */
K
kohsuke 已提交
1494
    public String validateAntFileMask(final String fileMasks) throws IOException, InterruptedException {
1495 1496
        return act(new FileCallable<String>() {
            public String invoke(File dir, VirtualChannel channel) throws IOException {
1497 1498 1499
                if(fileMasks.startsWith("~"))
                    return Messages.FilePath_TildaDoesntWork();

K
kohsuke 已提交
1500
                StringTokenizer tokens = new StringTokenizer(fileMasks,",");
K
kohsuke 已提交
1501 1502 1503

                while(tokens.hasMoreTokens()) {
                    final String fileMask = tokens.nextToken().trim();
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
                    if(hasMatch(dir,fileMask))
                        continue;   // no error on this portion

                    // in 1.172 we introduced an incompatible change to stop using ' ' as the separator
                    // so see if we can match by using ' ' as the separator
                    if(fileMask.contains(" ")) {
                        boolean matched = true;
                        for (String token : Util.tokenize(fileMask))
                            matched &= hasMatch(dir,token);
                        if(matched)
                            return Messages.FilePath_validateAntFileMask_whitespaceSeprator();
                    }

                    // a common mistake is to assume the wrong base dir, and there are two variations
                    // to this: (1) the user gave us aa/bb/cc/dd where cc/dd was correct
                    // and (2) the user gave us cc/dd where aa/bb/cc/dd was correct.

                    {// check the (1) above first
                        String f=fileMask;
                        while(true) {
                            int idx = findSeparator(f);
                            if(idx==-1)     break;
                            f=f.substring(idx+1);

                            if(hasMatch(dir,f))
                                return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask,f);
K
kohsuke 已提交
1530
                        }
1531
                    }
1532

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
                    {// check the (1) above next as this is more expensive.
                        // Try prepending "**/" to see if that results in a match
                        FileSet fs = Util.createFileSet(dir,"**/"+fileMask);
                        DirectoryScanner ds = fs.getDirectoryScanner(new Project());
                        if(ds.getIncludedFilesCount()!=0) {
                            // try shorter name first so that the suggestion results in least amount of changes
                            String[] names = ds.getIncludedFiles();
                            Arrays.sort(names,SHORTER_STRING_FIRST);
                            for( String f : names) {
                                // now we want to decompose f to the leading portion that matched "**"
                                // and the trailing portion that matched the file mask, so that
                                // we can suggest the user error.
                                //
                                // this is not a very efficient/clever way to do it, but it's relatively simple

                                String prefix="";
                                while(true) {
                                    int idx = findSeparator(f);
                                    if(idx==-1)     break;

                                    prefix+=f.substring(0,idx)+'/';
                                    f=f.substring(idx+1);
                                    if(hasMatch(dir,prefix+fileMask))
                                        return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, prefix+fileMask);
                                }
                            }
K
kohsuke 已提交
1559
                        }
1560
                    }
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
                    {// finally, see if we can identify any sub portion that's valid. Otherwise bail out
                        String previous = null;
                        String pattern = fileMask;

                        while(true) {
                            if(hasMatch(dir,pattern)) {
                                // found a match
                                if(previous==null)
                                    return String.format("'%s' doesn't match anything, although '%s' exists",
                                        fileMask, pattern );
                                else
                                    return String.format("'%s' doesn't match anything: '%s' exists but not '%s'",
                                        fileMask, pattern, previous );
                            }

                            int idx = findSeparator(pattern);
                            if(idx<0) {// no more path component left to go back
                                if(pattern.equals(fileMask))
                                    return String.format("'%s' doesn't match anything", fileMask );
                                else
                                    return String.format("'%s' doesn't match anything: even '%s' doesn't exist",
                                        fileMask, pattern );
                            }

                            // cut off the trailing component and try again
                            previous = pattern;
                            pattern = pattern.substring(0,idx);
                        }
K
kohsuke 已提交
1590
                    }
1591
                }
K
kohsuke 已提交
1592 1593

                return null; // no error
1594
            }
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612

            private boolean hasMatch(File dir, String pattern) {
                FileSet fs = Util.createFileSet(dir,pattern);
                DirectoryScanner ds = fs.getDirectoryScanner(new Project());

                return ds.getIncludedFilesCount()!=0 || ds.getIncludedDirsCount()!=0;
            }

            /**
             * Finds the position of the first path separator.
             */
            private int findSeparator(String pattern) {
                int idx1 = pattern.indexOf('\\');
                int idx2 = pattern.indexOf('/');
                if(idx1==-1)    return idx2;
                if(idx2==-1)    return idx1;
                return Math.min(idx1,idx2);
            }
1613 1614 1615
        });
    }

K
kohsuke 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
    /**
     * Shortcut for {@link #validateFileMask(String)} in case the left-hand side can be null.
     */
    public static FormValidation validateFileMask(FilePath pathOrNull, String value) throws IOException {
        if(pathOrNull==null) return FormValidation.ok();
        return pathOrNull.validateFileMask(value);
    }

    /**
     * Short for {@code validateFileMask(value,true)} 
     */
    public FormValidation validateFileMask(String value) throws IOException {
        return validateFileMask(value,true);
    }

    /**
     * Checks the GLOB-style file mask. See {@link #validateAntFileMask(String)} 
     * @since 1.294
     */
    public FormValidation validateFileMask(String value, boolean errorIfNotExist) throws IOException {
        value = fixEmpty(value);
        if(value==null)
            return FormValidation.ok();

        try {
            if(!exists()) // no workspace. can't check
                return FormValidation.ok();

            String msg = validateAntFileMask(value);
            if(errorIfNotExist)     return FormValidation.error(msg);
            else                    return FormValidation.warning(msg);
        } catch (InterruptedException e) {
            return FormValidation.ok();
        }
    }

    /**
     * Validates a relative file path from this {@link FilePath}.
     *
     * @param value
     *      The relative path being validated.
     * @param errorIfNotExist
     *      If true, report an error if the given relative path doesn't exist. Otherwise it's a warning.
     * @param expectingFile
     *      If true, we expect the relative path to point to a file.
     *      Otherwise, the relative path is expected to be pointing to a directory.
     */
    public FormValidation validateRelativePath(String value, boolean errorIfNotExist, boolean expectingFile) throws IOException {
        AbstractProject subject = Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class);
        subject.checkPermission(Item.CONFIGURE);

        value = fixEmpty(value);

        // none entered yet, or something is seriously wrong
        if(value==null || (AbstractProject<?,?>)subject ==null) return FormValidation.ok();

        // a common mistake is to use wildcard
        if(value.contains("*")) return FormValidation.error("Wildcard is not allowed here");

        try {
            if(!exists())    // no base directory. can't check
                return FormValidation.ok();

            FilePath path = child(value);
            if(path.exists()) {
                if (expectingFile) {
                    if(!path.isDirectory())
                        return FormValidation.ok();
                    else
                        return FormValidation.error(value+" is not a file");
                } else {
                    if(path.isDirectory())
                        return FormValidation.ok();
                    else
                        return FormValidation.error(value+" is not a directory");
                }
            }

            String msg = "No such "+(expectingFile?"file":"directory")+": " + value;
            if(errorIfNotExist)     return FormValidation.error(msg);
            else                    return FormValidation.warning(msg);
        } catch (InterruptedException e) {
            return FormValidation.ok();
        }
    }

    /**
     * A convenience method over {@link #validateRelativePath(String, boolean, boolean)}.
     */
    public FormValidation validateRelativeDirectory(String value, boolean errorIfNotExist) throws IOException {
        return validateRelativePath(value,errorIfNotExist,false);
    }

    public FormValidation validateRelativeDirectory(String value) throws IOException {
        return validateRelativeDirectory(value,true);
    }

K
kohsuke 已提交
1713 1714 1715
    @Deprecated
    public String toString() {
        // to make writing JSPs easily, return local
K
kohsuke 已提交
1716
        return remote;
K
kohsuke 已提交
1717 1718
    }

K
kohsuke 已提交
1719 1720 1721 1722 1723
    public VirtualChannel getChannel() {
        if(channel!=null)   return channel;
        else                return Hudson.MasterComputer.localChannel;
    }

K
kohsuke 已提交
1724 1725 1726 1727
    /**
     * Returns true if this {@link FilePath} represents a remote file. 
     */
    public boolean isRemote() {
K
typo.  
kohsuke 已提交
1728
        return channel!=null;
K
kohsuke 已提交
1729 1730
    }

K
kohsuke 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
    private void writeObject(ObjectOutputStream oos) throws IOException {
        Channel target = Channel.current();

        if(channel!=null && channel!=target)
            throw new IllegalStateException("Can't send a remote FilePath to a different remote channel");

        oos.defaultWriteObject();
        oos.writeBoolean(channel==null);
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        Channel channel = Channel.current();
        assert channel!=null;

        ois.defaultReadObject();
        if(ois.readBoolean()) {
            this.channel = channel;
        } else {
            this.channel = null;
        }
    }

    private static final long serialVersionUID = 1L;
1754

1755 1756
    public static int SIDE_BUFFER_SIZE = 1024;

1757 1758 1759 1760 1761
    /**
     * Adapts {@link FileCallable} to {@link Callable}.
     */
    private class FileCallableWrapper<T> implements DelegatingCallable<T,IOException> {
        private final FileCallable<T> callable;
1762
        private transient ClassLoader classLoader;
1763 1764 1765

        public FileCallableWrapper(FileCallable<T> callable) {
            this.callable = callable;
1766 1767 1768 1769 1770 1771
            this.classLoader = callable.getClass().getClassLoader();
        }

        private FileCallableWrapper(FileCallable<T> callable, ClassLoader classLoader) {
            this.callable = callable;
            this.classLoader = classLoader;
1772 1773 1774 1775 1776 1777 1778
        }

        public T call() throws IOException {
            return callable.invoke(new File(remote), Channel.current());
        }

        public ClassLoader getClassLoader() {
1779
            return classLoader;
1780 1781 1782 1783
        }

        private static final long serialVersionUID = 1L;
    }
1784 1785 1786 1787 1788 1789

    private static final Comparator<String> SHORTER_STRING_FIRST = new Comparator<String>() {
        public int compare(String o1, String o2) {
            return o1.length()-o2.length();
        }
    };
K
kohsuke 已提交
1790
}