FilePath.java 96.2 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
R
rseguy 已提交
5 6
 * Eric Lefevre-Ardant, Erik Ramfelt, Michael B. Donohue, Alan Harder,
 * Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
K
kohsuke 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 
 * 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 已提交
26 27
package hudson;

28 29
import hudson.Launcher.LocalLauncher;
import hudson.Launcher.RemoteLauncher;
30
import hudson.os.PosixAPI;
31
import jenkins.model.Jenkins;
32
import hudson.model.TaskListener;
K
kohsuke 已提交
33 34
import hudson.model.AbstractProject;
import hudson.model.Item;
K
kohsuke 已提交
35 36
import hudson.remoting.Callable;
import hudson.remoting.Channel;
37
import hudson.remoting.DelegatingCallable;
38
import hudson.remoting.Future;
K
kohsuke 已提交
39 40 41
import hudson.remoting.Pipe;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel;
K
kohsuke 已提交
42
import hudson.remoting.RemoteInputStream;
43
import hudson.remoting.Which;
44
import hudson.security.AccessControlled;
45
import hudson.util.DirScanner;
46
import hudson.util.IOException2;
47
import hudson.util.HeadBufferingStream;
K
kohsuke 已提交
48
import hudson.util.FormValidation;
K
kohsuke 已提交
49
import hudson.util.IOUtils;
K
Kohsuke Kawaguchi 已提交
50 51

import static hudson.Util.*;
K
kohsuke 已提交
52
import static hudson.FilePath.TarCompression.GZIP;
53
import hudson.org.apache.tools.tar.TarInputStream;
54 55
import hudson.util.io.Archiver;
import hudson.util.io.ArchiverFactory;
56
import jenkins.util.VirtualFile;
K
kohsuke 已提交
57
import org.apache.tools.ant.DirectoryScanner;
58
import org.apache.tools.ant.Project;
K
kohsuke 已提交
59
import org.apache.tools.ant.types.FileSet;
60
import org.apache.tools.tar.TarEntry;
K
kohsuke 已提交
61
import org.apache.commons.io.input.CountingInputStream;
62
import org.apache.commons.fileupload.FileItem;
K
kohsuke 已提交
63
import org.kohsuke.stapler.Stapler;
64
import java.io.BufferedOutputStream;
K
kohsuke 已提交
65
import java.io.File;
K
kohsuke 已提交
66 67 68 69
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
K
kohsuke 已提交
70
import java.io.IOException;
K
kohsuke 已提交
71
import java.io.InputStream;
K
Kohsuke Kawaguchi 已提交
72
import java.io.InterruptedIOException;
K
kohsuke 已提交
73 74 75 76 77
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.Writer;
K
kohsuke 已提交
78
import java.io.OutputStreamWriter;
K
Kohsuke Kawaguchi 已提交
79
import java.lang.reflect.Field;
80
import java.net.HttpURLConnection;
81
import java.net.URI;
K
kohsuke 已提交
82
import java.net.URL;
K
kohsuke 已提交
83
import java.net.URLConnection;
K
kohsuke 已提交
84 85
import java.util.ArrayList;
import java.util.List;
K
kohsuke 已提交
86
import java.util.StringTokenizer;
87 88
import java.util.Arrays;
import java.util.Comparator;
89
import java.util.logging.Level;
90
import java.util.regex.Matcher;
K
kohsuke 已提交
91
import java.util.regex.Pattern;
92
import java.util.concurrent.ExecutionException;
93 94
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
95 96
import com.jcraft.jzlib.GZIPInputStream;
import com.jcraft.jzlib.GZIPOutputStream;
K
kohsuke 已提交
97

K
kohsuke 已提交
98
import com.sun.jna.Native;
99
import hudson.os.PosixException;
100
import hudson.util.FileVisitor;
101
import java.util.Enumeration;
102
import java.util.Map;
103
import java.util.concurrent.atomic.AtomicInteger;
R
rseguy 已提交
104 105
import java.util.logging.Logger;
import org.apache.tools.ant.taskdefs.Chmod;
K
kohsuke 已提交
106

107 108 109
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipEntry;
        
K
kohsuke 已提交
110
/**
K
kohsuke 已提交
111
 * {@link File} like object with remoting support.
K
kohsuke 已提交
112 113
 *
 * <p>
K
kohsuke 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
 * 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
M
mindless 已提交
127
 * layer.
K
kohsuke 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 *
 * <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>
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 * void someMethod(FilePath file) {
 *     // make 'file' a fresh empty directory.
 *     file.act(new Freshen());
 * }
 * // if 'file' is on a different node, this FileCallable will
 * // be transferred to that node and executed there.
 * private static final class Freshen implements FileCallable&lt;Void> {
 *     private static final long serialVersionUID = 1;
 *     &#64;Override public Void invoke(File f, VirtualChannel channel) {
 *         // f and file represent the same thing
 *         f.deleteContents();
 *         f.mkdirs();
 *         return null;
 *     }
 * }
K
kohsuke 已提交
159
 * </pre>
K
kohsuke 已提交
160 161
 *
 * <p>
K
kohsuke 已提交
162
 * When {@link FileCallable} is transfered to a remote node, it will be done so
E
elefevre 已提交
163
 * by using the same Java serialization scheme that the remoting module uses.
K
kohsuke 已提交
164 165 166 167 168 169 170
 * 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 已提交
171 172
 *
 * @author Kohsuke Kawaguchi
K
Kohsuke Kawaguchi 已提交
173
 * @see VirtualFile
K
kohsuke 已提交
174
 */
K
kohsuke 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
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 已提交
192 193
    private final String remote;

K
kohsuke 已提交
194 195 196 197 198 199 200
    /**
     * 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 已提交
201
    public FilePath(VirtualChannel channel, String remote) {
202
        this.channel = channel == Jenkins.MasterComputer.localChannel ? null : channel;
203
        this.remote = normalize(remote);
K
kohsuke 已提交
204 205 206
    }

    /**
K
kohsuke 已提交
207 208 209 210 211
     * 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 已提交
212
     */
K
kohsuke 已提交
213 214
    public FilePath(File localPath) {
        this.channel = null;
215
        this.remote = normalize(localPath.getPath());
K
kohsuke 已提交
216 217
    }

K
kohsuke 已提交
218 219 220 221 222
    /**
     * 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 已提交
223
    public FilePath(FilePath base, String rel) {
K
kohsuke 已提交
224
        this.channel = base.channel;
225 226 227 228 229
        this.remote = normalize(resolvePathIfRelative(base, rel));
    }

    private String resolvePathIfRelative(FilePath base, String rel) {
        if(isAbsolute(rel)) return rel;
K
kohsuke 已提交
230
        if(base.isUnix()) {
231
            // shouldn't need this replace, but better safe than sorry
232
            return base.remote+'/'+rel.replace('\\','/');
K
kohsuke 已提交
233
        } else {
234 235
            // need this replace, see Slave.getWorkspaceFor and AbstractItem.getFullName, nested jobs on Windows
            // slaves will always have a rel containing at least one '/' character. JENKINS-13649
236
            return base.remote+'\\'+rel.replace('/','\\');
K
kohsuke 已提交
237 238 239
        }
    }

K
Kohsuke Kawaguchi 已提交
240 241 242
    /**
     * Is the given path name an absolute path?
     */
K
kohsuke 已提交
243
    private static boolean isAbsolute(String rel) {
244
        return rel.startsWith("/") || DRIVE_PATTERN.matcher(rel).matches() || UNC_PATTERN.matcher(rel).matches();
K
kohsuke 已提交
245 246
    }

247
    private static final Pattern DRIVE_PATTERN = Pattern.compile("[A-Za-z]:[\\\\/].*"),
248
            UNC_PATTERN = Pattern.compile("^\\\\\\\\.*"),
249
            ABSOLUTE_PREFIX_PATTERN = Pattern.compile("^(\\\\\\\\|(?:[A-Za-z]:)?[\\\\/])[\\\\/]*");
K
kohsuke 已提交
250

251 252 253 254 255
    /**
     * {@link File#getParent()} etc cannot handle ".." and "." in the path component very well,
     * so remove them.
     */
    private static String normalize(String path) {
256 257 258 259 260 261 262 263 264
        StringBuilder buf = new StringBuilder();
        // Check for prefix designating absolute path
        Matcher m = ABSOLUTE_PREFIX_PATTERN.matcher(path);
        if (m.find()) {
            buf.append(m.group(1));
            path = path.substring(m.end());
        }
        boolean isAbsolute = buf.length() > 0;
        // Split remaining path into tokens, trimming any duplicate or trailing separators
265
        List<String> tokens = new ArrayList<String>();
266 267 268 269 270 271 272 273 274 275 276
        int s = 0, end = path.length();
        for (int i = 0; i < end; i++) {
            char c = path.charAt(i);
            if (c == '/' || c == '\\') {
                tokens.add(path.substring(s, i));
                s = i;
                // Skip any extra separator chars
                while (++i < end && ((c = path.charAt(i)) == '/' || c == '\\')) { }
                // Add token for separator unless we reached the end
                if (i < end) tokens.add(path.substring(s, s+1));
                s = i;
277 278
            }
        }
279 280 281
        if (s < end) tokens.add(path.substring(s));
        // Look through tokens for "." or ".."
        for (int i = 0; i < tokens.size();) {
282 283 284
            String token = tokens.get(i);
            if (token.equals(".")) {
                tokens.remove(i);
285 286 287 288 289 290 291 292 293 294 295 296
                if (tokens.size() > 0)
                    tokens.remove(i > 0 ? i - 1 : i);
            } else if (token.equals("..")) {
                if (i == 0) {
                    // If absolute path, just remove: /../something
                    // If relative path, not collapsible so leave as-is
                    tokens.remove(0);
                    if (tokens.size() > 0) token += tokens.remove(0);
                    if (!isAbsolute) buf.append(token);
                } else {
                    // Normalize: remove something/.. plus separator before/after
                    i -= 2;
297
                    for (int j = 0; j < 3; j++) tokens.remove(i);
298 299 300
                    if (i > 0) tokens.remove(i-1);
                    else if (tokens.size() > 0) tokens.remove(0);
                }
301
            } else
302
                i += 2;
303
        }
304 305 306 307
        // Recombine tokens
        for (String token : tokens) buf.append(token);
        if (buf.length() == 0) buf.append('.');
        return buf.toString();
308 309
    }

K
kohsuke 已提交
310 311 312
    /**
     * Checks if the remote path is Unix.
     */
313
    boolean isUnix() {
314 315 316 317
        // if the path represents a local path, there' no need to guess.
        if(!isRemote())
            return File.pathSeparatorChar!=';';
            
318 319 320 321 322 323
        // 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 已提交
324 325 326 327 328
        // Windows can handle '/' as a path separator but Unix can't,
        // so err on Unix side
        return remote.indexOf("\\")==-1;
    }

J
Jørgen P. Tjernø 已提交
329 330 331 332
    /**
     * Gets the full path of the file on the remote machine.
     *
     */
K
kohsuke 已提交
333 334 335 336
    public String getRemote() {
        return remote;
    }

337 338
    /**
     * Creates a zip file from this directory or a file and sends that to the given output stream.
339 340
     *
     * @deprecated as of 1.315. Use {@link #zip(OutputStream)} that has more consistent name.
341 342
     */
    public void createZipArchive(OutputStream os) throws IOException, InterruptedException {
343 344
        zip(os);
    }
345

346 347 348 349 350 351
    /**
     * Creates a zip file from this directory or a file and sends that to the given output stream.
     */
    public void zip(OutputStream os) throws IOException, InterruptedException {
        zip(os,(FileFilter)null);
    }
352

K
Kohsuke Kawaguchi 已提交
353 354 355 356 357 358 359 360 361
    public void zip(FilePath dst) throws IOException, InterruptedException {
        OutputStream os = dst.write();
        try {
            zip(os);
        } finally {
            os.close();
        }
    }
    
362 363 364 365 366 367 368 369 370 371
    /**
     * Creates a zip file from this directory by using the specified filter,
     * and sends the result to the given output stream.
     *
     * @param filter
     *      Must be serializable since it may be executed remotely. Can be null to add all files.
     *
     * @since 1.315
     */
    public void zip(OutputStream os, FileFilter filter) throws IOException, InterruptedException {
372
        archive(ArchiverFactory.ZIP,os,filter);
373 374
    }

K
kohsuke 已提交
375 376 377 378 379 380 381 382
    /**
     * 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
383 384
     * @deprecated as of 1.315
     *      Use {@link #zip(OutputStream,String)} that has more consistent name.
K
kohsuke 已提交
385 386
     */
    public void createZipArchive(OutputStream os, final String glob) throws IOException, InterruptedException {
387
        archive(ArchiverFactory.ZIP,os,glob);
388 389 390 391 392 393 394
    }

    /**
     * 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
395
     *      works like {@link #createZipArchive(OutputStream)}, inserting a top-level directory into the ZIP.
396 397 398 399
     *
     * @since 1.315
     */
    public void zip(OutputStream os, final String glob) throws IOException, InterruptedException {
400
        archive(ArchiverFactory.ZIP,os,glob);
401 402
    }

403 404 405 406
    /**
     * Uses the given scanner on 'this' directory to list up files and then archive it to a zip stream.
     */
    public int zip(OutputStream out, DirScanner scanner) throws IOException, InterruptedException {
407
        return archive(ArchiverFactory.ZIP, out, scanner);
408 409
    }

410 411 412 413 414 415 416 417
    /**
     * Archives this directory into the specified archive format, to the given {@link OutputStream}, by using
     * {@link DirScanner} to choose what files to include.
     *
     * @return
     *      number of files/directories archived. This is only really useful to check for a situation where nothing
     *      is archived.
     */
418
    public int archive(final ArchiverFactory factory, OutputStream os, final DirScanner scanner) throws IOException, InterruptedException {
K
kohsuke 已提交
419
        final OutputStream out = (channel!=null)?new RemoteOutputStream(os):os;
420 421 422 423 424 425 426
        return act(new FileCallable<Integer>() {
            public Integer invoke(File f, VirtualChannel channel) throws IOException {
                Archiver a = factory.create(out);
                try {
                    scanner.scan(f,a);
                } finally {
                    a.close();
K
kohsuke 已提交
427
                }
428
                return a.countEntries();
K
kohsuke 已提交
429 430 431 432 433 434
            }

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

K
Kohsuke Kawaguchi 已提交
435
    public int archive(final ArchiverFactory factory, OutputStream os, final FileFilter filter) throws IOException, InterruptedException {
436 437 438
        return archive(factory,os,new DirScanner.Filter(filter));
    }

K
Kohsuke Kawaguchi 已提交
439
    public int archive(final ArchiverFactory factory, OutputStream os, final String glob) throws IOException, InterruptedException {
440 441 442
        return archive(factory,os,new DirScanner.Glob(glob,null));
    }

K
kohsuke 已提交
443 444 445 446 447 448
    /**
     * 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 已提交
449
     * @see #unzipFrom(InputStream)
K
kohsuke 已提交
450
     */
K
kohsuke 已提交
451
    public void unzip(final FilePath target) throws IOException, InterruptedException {
K
kohsuke 已提交
452
        target.act(new FileCallable<Void>() {
453

K
kohsuke 已提交
454
            public Void invoke(File dir, VirtualChannel channel) throws IOException {
455 456 457 458
                if (FilePath.this.isRemote())
                    unzip(dir, FilePath.this.read()); // use streams
                else
                    unzip(dir, new File(FilePath.this.getRemote())); // shortcut to local file
K
kohsuke 已提交
459 460
                return null;
            }
K
kohsuke 已提交
461 462 463
            private static final long serialVersionUID = 1L;
        });
    }
K
kohsuke 已提交
464

K
kohsuke 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    /**
     * 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 已提交
485 486 487 488 489 490
    /**
     * 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 已提交
491
     * @see #unzip(FilePath)
K
kohsuke 已提交
492 493 494 495 496 497 498 499
     */
    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 已提交
500 501 502 503
            private static final long serialVersionUID = 1L;
        });
    }

504
    private static void unzip(File dir, InputStream in) throws IOException {
505 506
        File tmpFile = File.createTempFile("tmpzip", null); // uses java.io.tmpdir
        try {
507
            // TODO why does this not simply use ZipInputStream?
508 509 510 511 512 513 514 515
            IOUtils.copy(in, tmpFile);
            unzip(dir,tmpFile);
        }
        finally {
            tmpFile.delete();
        }
    }

516
    static private void unzip(File dir, File zipFile) throws IOException {
K
kohsuke 已提交
517
        dir = dir.getAbsoluteFile();    // without absolutization, getParentFile below seems to fail
518
        ZipFile zip = new ZipFile(zipFile);
C
Christoph Kutzinski 已提交
519
        @SuppressWarnings("unchecked")
520
        Enumeration<ZipEntry> entries = zip.getEntries();
K
kohsuke 已提交
521 522

        try {
523 524 525 526
            while (entries.hasMoreElements()) {
                ZipEntry e = entries.nextElement();
                File f = new File(dir, e.getName());
                if (e.isDirectory()) {
K
kohsuke 已提交
527 528 529
                    f.mkdirs();
                } else {
                    File p = f.getParentFile();
530 531 532
                    if (p != null) {
                        p.mkdirs();
                    }
533 534 535 536 537 538
                    InputStream input = zip.getInputStream(e);
                    try {
                        IOUtils.copy(input, f);
                    } finally {
                        input.close();
                    }
539 540
                    try {
                        FilePath target = new FilePath(f);
541 542 543
                        int mode = e.getUnixMode();
                        if (mode!=0)    // Ant returns 0 if the archive doesn't record the access mode
                            target.chmod(mode);
544 545 546
                    } catch (InterruptedException ex) {
                        LOGGER.log(Level.WARNING, "unable to set permissions", ex);
                    }
547
                    f.setLastModified(e.getTime());
K
kohsuke 已提交
548 549 550 551 552 553 554
                }
            }
        } finally {
            zip.close();
        }
    }

K
kohsuke 已提交
555 556 557 558 559
    /**
     * Absolutizes this {@link FilePath} and returns the new one.
     */
    public FilePath absolutize() throws IOException, InterruptedException {
        return new FilePath(channel,act(new FileCallable<String>() {
560
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
561 562 563 564 565 566
            public String invoke(File f, VirtualChannel channel) throws IOException {
                return f.getAbsolutePath();
            }
        }));
    }

K
Kohsuke Kawaguchi 已提交
567 568 569 570 571 572 573 574 575 576 577
    /**
     * Creates a symlink to the specified target.
     *
     * @param target
     *      The file that the symlink should point to.
     * @param listener
     *      If symlink creation requires a help of an external process, the error will be reported here.
     * @since 1.456
     */
    public void symlinkTo(final String target, final TaskListener listener) throws IOException, InterruptedException {
        act(new FileCallable<Void>() {
578
            private static final long serialVersionUID = 1L;
K
Kohsuke Kawaguchi 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
            public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
                Util.createSymlink(f.getParentFile(),target,f.getName(),listener);
                return null;
            }
        });
    }
    
    /**
     * Resolves symlink, if the given file is a symlink. Otherwise return null.
     * <p>
     * If the resolution fails, report an error.
     *
     * @since 1.456
     */
    public String readLink() throws IOException, InterruptedException {
        return act(new FileCallable<String>() {
595
            private static final long serialVersionUID = 1L;
K
Kohsuke Kawaguchi 已提交
596 597 598 599 600 601
            public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
                return Util.resolveSymlink(f);
            }
        });
    }

K
kohsuke 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        FilePath that = (FilePath) o;

        if (channel != null ? !channel.equals(that.channel) : that.channel != null) return false;
        return remote.equals(that.remote);

    }

    @Override
    public int hashCode() {
        return 31 * (channel != null ? channel.hashCode() : 0) + remote.hashCode();
    }
    
K
kohsuke 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    /**
     * 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 {
635
                    return new GZIPInputStream(in, 8192, true);
K
kohsuke 已提交
636 637 638 639 640 641 642
                } 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 {
643
                return new GZIPOutputStream(new BufferedOutputStream(out));
K
kohsuke 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
            }
        };

        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 {
665
                    readFromTar("input stream",dir, compression.extract(in));
K
kohsuke 已提交
666 667 668 669 670 671 672 673 674
                    return null;
                }
                private static final long serialVersionUID = 1L;
            });
        } finally {
            IOUtils.closeQuietly(_in);
        }
    }

K
kohsuke 已提交
675
    /**
K
kohsuke 已提交
676
     * Given a tgz/zip file, extracts it to the given target directory, if necessary.
K
kohsuke 已提交
677 678 679 680 681 682
     *
     * <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 已提交
683
     * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}.
K
kohsuke 已提交
684
     * <li>The timestamp of the .tgz file is left in the installation directory upon extraction.
K
kohsuke 已提交
685 686
     * <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 已提交
687
     * <li>If the connection is refused but the target directory already exists, it is left alone.
K
kohsuke 已提交
688 689
     * </ul>
     *
K
kohsuke 已提交
690 691
     * @param archive
     *      The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header.
K
kohsuke 已提交
692 693 694 695 696 697 698 699 700
     *      (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 已提交
701
    public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message) throws IOException, InterruptedException {
K
kohsuke 已提交
702
        try {
703
            FilePath timestamp = this.child(".timestamp");
704 705
            URLConnection con;
            try {
706
                con = ProxyConfiguration.open(archive);
707 708 709
                if (timestamp.exists()) {
                    con.setIfModifiedSince(timestamp.lastModified());
                }
710 711 712 713 714 715 716 717 718 719
                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 已提交
720 721
                }
            }
722 723 724 725 726 727

            if (con instanceof HttpURLConnection
                    && ((HttpURLConnection)con).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
                return false;
            }

728
            long sourceTimestamp = con.getLastModified();
K
kohsuke 已提交
729

730 731 732 733 734 735 736
            if(this.exists()) {
                if(timestamp.exists() && sourceTimestamp ==timestamp.lastModified())
                    return false;   // already up to date
                this.deleteContents();
            } else {
                this.mkdirs();
            }
K
kohsuke 已提交
737

738 739 740
            if(listener!=null)
                listener.getLogger().println(message);

741 742 743 744 745 746 747 748 749 750 751 752 753
            if (isRemote()) {
                // First try to download from the slave machine.
                try {
                    act(new Unpack(archive));
                    timestamp.touch(sourceTimestamp);
                    return true;
                } catch (IOException x) {
                    if (listener != null) {
                        x.printStackTrace(listener.error("Failed to download " + archive + " from slave; will retry from master"));
                    }
                }
            }

754
            // for HTTP downloads, enable automatic retry for added resilience
755
            InputStream in = archive.getProtocol().startsWith("http") ? ProxyConfiguration.getInputStream(archive) : con.getInputStream();
756 757 758 759
            CountingInputStream cis = new CountingInputStream(in);
            try {
                if(archive.toExternalForm().endsWith(".zip"))
                    unzipFrom(cis);
760 761
                else
                    untarFrom(cis,GZIP);
762 763 764 765 766 767
            } catch (IOException e) {
                throw new IOException2(String.format("Failed to unpack %s (%d bytes read of total %d)",
                        archive,cis.getByteCount(),con.getContentLength()),e);
            }
            timestamp.touch(sourceTimestamp);
            return true;
K
kohsuke 已提交
768
        } catch (IOException e) {
769
            throw new IOException2("Failed to install "+archive+" to "+remote,e);
K
kohsuke 已提交
770
        }
K
kohsuke 已提交
771 772
    }

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
    private static final class Unpack implements FileCallable<Void> {
        private final URL archive;
        Unpack(URL archive) {
            this.archive = archive;
        }
        @Override public Void invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
            InputStream in = archive.openStream();
            try {
                CountingInputStream cis = new CountingInputStream(in);
                try {
                    if (archive.toExternalForm().endsWith(".zip")) {
                        unzip(dir, cis);
                    } else {
                        readFromTar("input stream", dir, GZIP.extract(cis));
                    }
                } catch (IOException x) {
                    throw new IOException2(String.format("Failed to unpack %s (%d bytes read)", archive, cis.getByteCount()), x);
                }
            } finally {
                in.close();
            }
            return null;
        }
    }

K
kohsuke 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    /**
     * 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 已提交
826 827

    /**
C
Christoph Kutzinski 已提交
828
     * Convenience method to call {@link FilePath#copyTo(FilePath)}.
K
kohsuke 已提交
829 830 831 832 833 834
     * 
     * @since 1.311
     */
    public void copyFrom(FilePath src) throws IOException, InterruptedException {
        src.copyTo(this);
    }
K
kohsuke 已提交
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    /**
     * 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 {
854 855 856 857 858
                try {
                    o.close();
                } finally {
                    i.close();
                }
859 860 861 862
            }
        }
    }

K
kohsuke 已提交
863 864 865
    /**
     * Code that gets executed on the machine where the {@link FilePath} is local.
     * Used to act on {@link FilePath}.
866
     * <strong>Warning:</code> implementations must be serializable, so prefer a static nested class to an inner class.
K
kohsuke 已提交
867 868
     * @see FilePath#act(FileCallable)
     */
869
    public interface FileCallable<T> extends Serializable {
K
kohsuke 已提交
870 871 872
        /**
         * Performs the computational task on the node where the data is located.
         *
873 874 875
         * <p>
         * All the exceptions are forwarded to the caller.
         *
K
kohsuke 已提交
876 877 878 879 880 881
         * @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.
         */
882
        T invoke(File f, VirtualChannel channel) throws IOException, InterruptedException;
K
kohsuke 已提交
883 884 885 886 887 888 889
    }

    /**
     * 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 {
890 891 892 893
        return act(callable,callable.getClass().getClassLoader());
    }

    private <T> T act(final FileCallable<T> callable, ClassLoader cl) throws IOException, InterruptedException {
K
kohsuke 已提交
894 895 896
        if(channel!=null) {
            // run this on a remote system
            try {
897
                DelegatingCallable<T,IOException> wrapper = new FileCallableWrapper<T>(callable, cl);
898 899 900 901 902 903
                Jenkins instance = Jenkins.getInstance();
                if (instance != null) { // this happens during unit tests
                    ExtensionList<FileCallableWrapperFactory> factories = instance.getExtensionList(FileCallableWrapperFactory.class);
                    for (FileCallableWrapperFactory factory : factories) {
                        wrapper = factory.wrap(wrapper);
                    }
904 905 906
                }

                return channel.call(wrapper);
907
            } catch (TunneledInterruptedException e) {
908
                throw (InterruptedException)new InterruptedException(e.getMessage()).initCause(e);
K
kohsuke 已提交
909 910
            } catch (AbortException e) {
                throw e;    // pass through so that the caller can catch it as AbortException
K
kohsuke 已提交
911 912
            } catch (IOException e) {
                // wrap it into a new IOException so that we get the caller's stack trace as well.
K
kohsuke 已提交
913
                throw new IOException2("remote file operation failed: "+remote+" at "+channel,e);
K
kohsuke 已提交
914 915 916
            }
        } else {
            // the file is on the local machine.
917
            return callable.invoke(new File(remote), Jenkins.MasterComputer.localChannel);
K
kohsuke 已提交
918 919 920
        }
    }

921 922
    /**
     * This extension point allows to contribute a wrapper around a fileCallable so that a plugin can "intercept" a
923 924 925
     * call.
     * <p>The {@link #wrap(hudson.remoting.DelegatingCallable)} method itself will be executed on master
     * (and may collect contextual data if needed) and the returned wrapper will be executed on remote.
926 927 928
     *
     * @since 1.482
     * @see AbstractInterceptorCallableWrapper
929
     */
930
    public static abstract class FileCallableWrapperFactory implements ExtensionPoint {
931 932 933 934 935

        public abstract <T> DelegatingCallable<T,IOException> wrap(DelegatingCallable<T,IOException> callable);

    }

936 937 938
    /**
     * Abstract {@link DelegatingCallable} that exposes an Before/After pattern for
     * {@link hudson.FilePath.FileCallableWrapperFactory} that want to implement AOP-style interceptors
939
     * @since 1.482
940
     */
941
    public static abstract class AbstractInterceptorCallableWrapper<T> implements DelegatingCallable<T, IOException> {
942
        private static final long serialVersionUID = 1L;
943

944
        private final DelegatingCallable<T, IOException> callable;
945 946 947 948 949

        public AbstractInterceptorCallableWrapper(DelegatingCallable<T, IOException> callable) {
            this.callable = callable;
        }

950 951
        @Override
        public final ClassLoader getClassLoader() {
952 953 954
            return callable.getClassLoader();
        }

955
        public final T call() throws IOException {
956 957 958 959 960 961 962 963
            before();
            try {
                return callable.call();
            } finally {
                after();
            }
        }

964 965 966
        /**
         * Executed before the actual FileCallable is invoked. This code will run on remote
         */
967
        protected void before() {}
968 969

        /**
970
         * Executed after the actual FileCallable is invoked (even if this one failed). This code will run on remote
971
         */
972 973 974 975
        protected void after() {}
    }


976 977 978 979 980 981
    /**
     * 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 {
W
wolfgarnet 已提交
982 983 984 985 986 987 988 989 990
            DelegatingCallable<T,IOException> wrapper = new FileCallableWrapper<T>(callable);
            Jenkins instance = Jenkins.getInstance();
            if (instance != null) { // this happens during unit tests
                ExtensionList<FileCallableWrapperFactory> factories = instance.getExtensionList(FileCallableWrapperFactory.class);
                for (FileCallableWrapperFactory factory : factories) {
                    wrapper = factory.wrap(wrapper);
                }
            }

991
            return (channel!=null ? channel : Jenkins.MasterComputer.localChannel)
W
wolfgarnet 已提交
992
                .callAsync(wrapper);
993 994 995 996 997 998
        } 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 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    /**
     * 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();
        }
    }

K
Kohsuke Kawaguchi 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    /**
     * Takes a {@link FilePath}+{@link FileCallable} pair and returns the equivalent {@link Callable}.
     * When executing the resulting {@link Callable}, it executes {@link FileCallable#act(FileCallable)}
     * on this {@link FilePath}.
     *
     * @since 1.522
     */
    public <V> Callable<V,IOException> asCallableWith(final FileCallable<V> task) {
        return new Callable<V,IOException>() {
            @Override
            public V call() throws IOException {
                try {
                    return act(task);
                } catch (InterruptedException e) {
                    throw (IOException)new InterruptedIOException().initCause(e);
                }
            }
            private static final long serialVersionUID = 1L;
        };
    }

K
kohsuke 已提交
1034 1035 1036 1037 1038 1039
    /**
     * 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>() {
1040
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1041 1042 1043 1044 1045 1046
            public URI invoke(File f, VirtualChannel channel) {
                return f.toURI();
            }
        });
    }

1047 1048
    /**
     * Gets the {@link VirtualFile} representation of this {@link FilePath}
K
Kohsuke Kawaguchi 已提交
1049 1050
     *
     * @since 1.532
1051 1052 1053 1054 1055
     */
    public VirtualFile toVirtualFile() {
        return VirtualFile.forFilePath(this);
    }

K
kohsuke 已提交
1056 1057 1058
    /**
     * Creates this directory.
     */
K
kohsuke 已提交
1059
    public void mkdirs() throws IOException, InterruptedException {
1060
        if(!act(new FileCallable<Boolean>() {
1061
            private static final long serialVersionUID = 1L;
1062
            public Boolean invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
1063 1064 1065 1066
                if(f.mkdirs() || f.exists())
                    return true;    // OK

                // following Ant <mkdir> task to avoid possible race condition.
1067
                Thread.sleep(10);
1068 1069

                return f.mkdirs() || f.exists();
K
kohsuke 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
            }
        }))
            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>() {
1080
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1081 1082 1083 1084 1085
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Util.deleteRecursive(f);
                return null;
            }
        });
K
kohsuke 已提交
1086 1087 1088 1089 1090
    }

    /**
     * Deletes all the contents of this directory, but not the directory itself
     */
K
kohsuke 已提交
1091 1092
    public void deleteContents() throws IOException, InterruptedException {
        act(new FileCallable<Void>() {
1093
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1094 1095 1096 1097 1098
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Util.deleteContentsRecursive(f);
                return null;
            }
        });
K
kohsuke 已提交
1099 1100
    }

K
kohsuke 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    /**
     * Gets the file name portion except the extension.
     *
     * For example, "foo" for "foo.txt" and "foo.tar" for "foo.tar.gz".
     */
    public String getBaseName() {
        String n = getName();
        int idx = n.lastIndexOf('.');
        if (idx<0)  return n;
        return n.substring(0,idx);
    }
K
kohsuke 已提交
1112
    /**
K
Kohsuke Kawaguchi 已提交
1113
     * Gets just the file name portion without directories.
K
kohsuke 已提交
1114
     *
K
Kohsuke Kawaguchi 已提交
1115
     * For example, "foo.txt" for "../abc/foo.txt"
K
kohsuke 已提交
1116 1117
     */
    public String getName() {
1118 1119 1120 1121 1122
        String r = remote;
        if(r.endsWith("\\") || r.endsWith("/"))
            r = r.substring(0,r.length()-1);

        int len = r.length()-1;
K
kohsuke 已提交
1123
        while(len>=0) {
1124
            char ch = r.charAt(len);
K
kohsuke 已提交
1125 1126 1127 1128 1129
            if(ch=='\\' || ch=='/')
                break;
            len--;
        }

1130
        return r.substring(len+1);
K
kohsuke 已提交
1131 1132
    }

K
kohsuke 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    /**
     * Short for {@code getParent().child(rel)}. Useful for getting other files in the same directory. 
     */
    public FilePath sibling(String rel) {
        return getParent().child(rel);
    }

    /**
     * Returns a {@link FilePath} by adding the given suffix to this path name.
     */
    public FilePath withSuffix(String suffix) {
        return new FilePath(channel,remote+suffix);
    }

K
kohsuke 已提交
1147
    /**
K
kohsuke 已提交
1148
     * The same as {@link FilePath#FilePath(FilePath,String)} but more OO.
1149
     * @param relOrAbsolute a relative or absolute path
K
kohsuke 已提交
1150
     * @return a file on the same channel
K
kohsuke 已提交
1151
     */
1152 1153
    public FilePath child(String relOrAbsolute) {
        return new FilePath(this,relOrAbsolute);
K
kohsuke 已提交
1154 1155 1156 1157
    }

    /**
     * Gets the parent file.
1158
     * @return parent FilePath or null if there is no parent
K
kohsuke 已提交
1159 1160
     */
    public FilePath getParent() {
1161 1162 1163
        int i = remote.length() - 2;
        for (; i >= 0; i--) {
            char ch = remote.charAt(i);
K
kohsuke 已提交
1164 1165 1166 1167
            if(ch=='\\' || ch=='/')
                break;
        }

1168
        return i >= 0 ? new FilePath( channel, remote.substring(0,i+1) ) : null;
K
kohsuke 已提交
1169 1170 1171
    }

    /**
K
kohsuke 已提交
1172
     * Creates a temporary file in the directory that this {@link FilePath} object designates.
J
Jørgen P. Tjernø 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
     *
     * @param prefix
     *      The prefix string to be used in generating the file's name; must be
     *      at least three characters long
     * @param suffix
     *      The suffix string to be used in generating the file's name; may be
     *      null, in which case the suffix ".tmp" will be used
     * @return
     *      The new FilePath pointing to the temporary file
     * @see File#createTempFile(String, String)
K
kohsuke 已提交
1183
     */
K
kohsuke 已提交
1184 1185 1186
    public FilePath createTempFile(final String prefix, final String suffix) throws IOException, InterruptedException {
        try {
            return new FilePath(this,act(new FileCallable<String>() {
1187
                private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
                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);
        }
    }

    /**
J
Jørgen P. Tjernø 已提交
1199
     * Creates a temporary file in this directory and set the contents to the
K
kohsuke 已提交
1200
     * given text (encoded in the platform default encoding)
J
Jørgen P. Tjernø 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
     *
     * @param prefix
     *      The prefix string to be used in generating the file's name; must be
     *      at least three characters long
     * @param suffix
     *      The suffix string to be used in generating the file's name; may be
     *      null, in which case the suffix ".tmp" will be used
     * @param contents
     *      The initial contents of the temporary file.
     * @return
     *      The new FilePath pointing to the temporary file
     * @see File#createTempFile(String, String)
K
kohsuke 已提交
1213 1214
     */
    public FilePath createTextTempFile(final String prefix, final String suffix, final String contents) throws IOException, InterruptedException {
1215 1216 1217 1218
        return createTextTempFile(prefix,suffix,contents,true);
    }

    /**
J
Jørgen P. Tjernø 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
     * Creates a temporary file in this directory (or the system temporary
     * directory) and set the contents to the given text (encoded in the
     * platform default encoding)
     *
     * @param prefix
     *      The prefix string to be used in generating the file's name; must be
     *      at least three characters long
     * @param suffix
     *      The suffix string to be used in generating the file's name; may be
     *      null, in which case the suffix ".tmp" will be used
     * @param contents
     *      The initial contents of the temporary file.
     * @param inThisDirectory
     *      If true, then create this temporary in the directory pointed to by
     *      this.
     *      If false, then the temporary file is created in the system temporary
     *      directory (java.io.tmpdir)
     * @return
     *      The new FilePath pointing to the temporary file
     * @see File#createTempFile(String, String)
1239 1240
     */
    public FilePath createTextTempFile(final String prefix, final String suffix, final String contents, final boolean inThisDirectory) throws IOException, InterruptedException {
K
kohsuke 已提交
1241
        try {
K
kohsuke 已提交
1242
            return new FilePath(channel,act(new FileCallable<String>() {
1243
                private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1244
                public String invoke(File dir, VirtualChannel channel) throws IOException {
1245
                    if(!inThisDirectory)
K
kohsuke 已提交
1246
                        dir = new File(System.getProperty("java.io.tmpdir"));
1247 1248
                    else
                        dir.mkdirs();
1249 1250 1251 1252 1253 1254 1255

                    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 已提交
1256 1257

                    Writer w = new FileWriter(f);
S
ssogabe 已提交
1258 1259 1260 1261 1262
                    try {
                        w.write(contents);
                    } finally {
                        w.close();
                    }
K
kohsuke 已提交
1263

K
kohsuke 已提交
1264
                    return f.getAbsolutePath();
K
kohsuke 已提交
1265 1266
                }
            }));
K
kohsuke 已提交
1267
        } catch (IOException e) {
K
kohsuke 已提交
1268
            throw new IOException2("Failed to create a temp file on "+remote,e);
K
kohsuke 已提交
1269
        }
K
kohsuke 已提交
1270 1271
    }

K
kohsuke 已提交
1272 1273
    /**
     * Creates a temporary directory inside the directory represented by 'this'
J
Jørgen P. Tjernø 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282
     *
     * @param prefix
     *      The prefix string to be used in generating the directory's name;
     *      must be at least three characters long
     * @param suffix
     *      The suffix string to be used in generating the directory's name; may
     *      be null, in which case the suffix ".tmp" will be used
     * @return
     *      The new FilePath pointing to the temporary directory
K
kohsuke 已提交
1283
     * @since 1.311
J
Jørgen P. Tjernø 已提交
1284
     * @see File#createTempFile(String, String)
K
kohsuke 已提交
1285 1286 1287 1288
     */
    public FilePath createTempDir(final String prefix, final String suffix) throws IOException, InterruptedException {
        try {
            return new FilePath(this,act(new FileCallable<String>() {
1289
                private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
                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 已提交
1302 1303
    /**
     * Deletes this file.
1304 1305
     * @throws IOException if it exists but could not be successfully deleted
     * @return true, for a modicum of compatibility
K
kohsuke 已提交
1306
     */
K
kohsuke 已提交
1307
    public boolean delete() throws IOException, InterruptedException {
1308
        act(new FileCallable<Void>() {
1309
            private static final long serialVersionUID = 1L;
1310 1311 1312
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Util.deleteFile(f);
                return null;
K
kohsuke 已提交
1313 1314
            }
        });
1315
        return true;
K
kohsuke 已提交
1316 1317 1318 1319 1320 1321 1322
    }

    /**
     * Checks if the file exists.
     */
    public boolean exists() throws IOException, InterruptedException {
        return act(new FileCallable<Boolean>() {
1323
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1324 1325 1326 1327
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
                return f.exists();
            }
        });
K
kohsuke 已提交
1328 1329
    }

K
kohsuke 已提交
1330 1331 1332 1333 1334
    /**
     * 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 已提交
1335
     * @see #touch(long)
K
kohsuke 已提交
1336 1337 1338
     */
    public long lastModified() throws IOException, InterruptedException {
        return act(new FileCallable<Long>() {
1339
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1340 1341 1342 1343
            public Long invoke(File f, VirtualChannel channel) throws IOException {
                return f.lastModified();
            }
        });
K
kohsuke 已提交
1344 1345
    }

K
kohsuke 已提交
1346 1347 1348 1349 1350 1351 1352
    /**
     * 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>() {
C
Christoph Kutzinski 已提交
1353
            private static final long serialVersionUID = -5094638816500738429L;
K
kohsuke 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362
            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;
            }
        });
    }
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    
    private void setLastModifiedIfPossible(final long timestamp) throws IOException, InterruptedException {
        String message = act(new FileCallable<String>() {
            private static final long serialVersionUID = -828220335793641630L;
            public String invoke(File f, VirtualChannel channel) throws IOException {
                if(!f.setLastModified(timestamp)) {
                    if (Functions.isWindows()) {
                        // On Windows this seems to fail often. See JENKINS-11073
                        // Therefore don't fail, but just log a warning
                        return "Failed to set the timestamp of "+f+" to "+timestamp;
                    } else {
                        throw new IOException("Failed to set the timestamp of "+f+" to "+timestamp);
                    }
                }
                return null;
            }
        });

        if (message!=null) {
            LOGGER.warning(message);
        }
    }
K
kohsuke 已提交
1385

K
kohsuke 已提交
1386 1387 1388 1389 1390
    /**
     * Checks if the file is a directory.
     */
    public boolean isDirectory() throws IOException, InterruptedException {
        return act(new FileCallable<Boolean>() {
1391
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1392 1393 1394 1395
            public Boolean invoke(File f, VirtualChannel channel) throws IOException {
                return f.isDirectory();
            }
        });
K
kohsuke 已提交
1396
    }
K
kohsuke 已提交
1397 1398 1399
    
    /**
     * Returns the file size in bytes.
K
kohsuke 已提交
1400 1401
     *
     * @since 1.129
K
kohsuke 已提交
1402 1403 1404
     */
    public long length() throws IOException, InterruptedException {
        return act(new FileCallable<Long>() {
1405
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1406 1407 1408 1409 1410
            public Long invoke(File f, VirtualChannel channel) throws IOException {
                return f.length();
            }
        });
    }
K
kohsuke 已提交
1411

K
kohsuke 已提交
1412 1413 1414 1415 1416
    /**
     * Sets the file permission.
     *
     * On Windows, no-op.
     *
K
kohsuke 已提交
1417 1418 1419
     * @param mask
     *      File permission mask. To simplify the permission copying,
     *      if the parameter is -1, this method becomes no-op.
1420 1421 1422 1423
     *      <p>
     *      please note mask is expected to be an octal if you use <a href="http://en.wikipedia.org/wiki/Chmod">chmod command line values</a>,
     *      so preceded by a '0' in java notation, ie <code>chmod(0644)</code>
     *
K
kohsuke 已提交
1424
     * @since 1.303
K
kohsuke 已提交
1425
     * @see #mode()
K
kohsuke 已提交
1426 1427
     */
    public void chmod(final int mask) throws IOException, InterruptedException {
K
kohsuke 已提交
1428
        if(!isUnix() || mask==-1)   return;
K
kohsuke 已提交
1429
        act(new FileCallable<Void>() {
1430
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1431
            public Void invoke(File f, VirtualChannel channel) throws IOException {
1432
                _chmod(f, mask);
R
rseguy 已提交
1433

K
kohsuke 已提交
1434 1435 1436 1437 1438
                return null;
            }
        });
    }

1439
    /**
1440
     * Run chmod via jnr-posix
1441 1442 1443 1444
     */
    private static void _chmod(File f, int mask) throws IOException {
        if (Functions.isWindows())  return; // noop

1445
        PosixAPI.jnr().chmod(f.getAbsolutePath(),mask);
1446 1447 1448 1449
    }

    private static boolean CHMOD_WARNED = false;

K
kohsuke 已提交
1450 1451 1452 1453 1454 1455 1456 1457
    /**
     * Gets the file permission bit mask.
     *
     * @return
     *      -1 on Windows, since such a concept doesn't make sense.
     * @since 1.311
     * @see #chmod(int)
     */
1458
    public int mode() throws IOException, InterruptedException, PosixException {
K
kohsuke 已提交
1459 1460
        if(!isUnix())   return -1;
        return act(new FileCallable<Integer>() {
1461
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1462
            public Integer invoke(File f, VirtualChannel channel) throws IOException {
1463
                return IOUtils.mode(f);
K
kohsuke 已提交
1464 1465 1466 1467
            }
        });
    }

K
kohsuke 已提交
1468
    /**
K
kohsuke 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477
     * 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 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    /**
     * 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 已提交
1494 1495
    /**
     * List up files in this directory, just like {@link File#listFiles(FileFilter)}.
K
kohsuke 已提交
1496 1497 1498 1499 1500 1501
     *
     * @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 已提交
1502
     */
K
kohsuke 已提交
1503
    public List<FilePath> list(final FileFilter filter) throws IOException, InterruptedException {
1504 1505 1506
        if (filter != null && !(filter instanceof Serializable)) {
            throw new IllegalArgumentException("Non-serializable filter of " + filter.getClass());
        }
K
kohsuke 已提交
1507
        return act(new FileCallable<List<FilePath>>() {
1508
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
            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;
            }
1519
        }, (filter!=null?filter:this).getClass().getClassLoader());
K
kohsuke 已提交
1520 1521
    }

K
kohsuke 已提交
1522 1523 1524 1525
    /**
     * List up files in this directory that matches the given Ant-style filter.
     *
     * @param includes
K
kohsuke 已提交
1526
     *      See {@link FileSet} for the syntax. String like "foo/*.zip" or "foo/*&#42;/*.xml"
K
kohsuke 已提交
1527 1528
     * @return
     *      can be empty but always non-null.
K
kohsuke 已提交
1529 1530
     */
    public FilePath[] list(final String includes) throws IOException, InterruptedException {
B
bap2000 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
        return list(includes, null);
    }

    /**
     * List up files in this directory that matches the given Ant-style filter.
     *
     * @param includes
     * @param excludes
     *      See {@link FileSet} for the syntax. String like "foo/*.zip" or "foo/*&#42;/*.xml"
     * @return
     *      can be empty but always non-null.
1542
     * @since 1.407
B
bap2000 已提交
1543 1544
     */
    public FilePath[] list(final String includes, final String excludes) throws IOException, InterruptedException {
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
        return list(includes, excludes, true);
    }

    /**
     * List up files in this directory that matches the given Ant-style filter.
     *
     * @param includes
     * @param excludes
     *      See {@link FileSet} for the syntax. String like "foo/*.zip" or "foo/*&#42;/*.xml"
     * @param defaultExcludes whether to use the ant default excludes
     * @return
     *      can be empty but always non-null.
     * @since 1.465
     */
    public FilePath[] list(final String includes, final String excludes, final boolean defaultExcludes) throws IOException, InterruptedException {
K
kohsuke 已提交
1560
        return act(new FileCallable<FilePath[]>() {
1561
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1562
            public FilePath[] invoke(File f, VirtualChannel channel) throws IOException {
1563
                String[] files = glob(f, includes, excludes, defaultExcludes);
K
kohsuke 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

                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 已提交
1574 1575
    /**
     * Runs Ant glob expansion.
K
kohsuke 已提交
1576 1577 1578
     *
     * @return
     *      A set of relative file names from the base directory.
K
kohsuke 已提交
1579
     */
1580
    private static String[] glob(File dir, String includes, String excludes, boolean defaultExcludes) throws IOException {
K
kohsuke 已提交
1581
        if(isAbsolute(includes))
W
wyukawa 已提交
1582
            throw new IOException("Expecting Ant GLOB pattern, but saw '"+includes+"'. See http://ant.apache.org/manual/Types/fileset.html for syntax");
B
bap2000 已提交
1583
        FileSet fs = Util.createFileSet(dir,includes,excludes);
1584
        fs.setDefaultexcludes(defaultExcludes);
K
kohsuke 已提交
1585 1586 1587 1588 1589
        DirectoryScanner ds = fs.getDirectoryScanner(new Project());
        String[] files = ds.getIncludedFiles();
        return files;
    }

K
kohsuke 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598
    /**
     * 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>() {
1599
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1600
            public Void call() throws IOException {
1601 1602 1603 1604 1605 1606 1607 1608 1609
                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 已提交
1610 1611 1612 1613 1614 1615
            }
        });

        return p.getIn();
    }

K
kohsuke 已提交
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    /**
     * 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 已提交
1628 1629 1630
    /**
     * Writes to this file.
     * If this file already exists, it will be overwritten.
K
kohsuke 已提交
1631
     * If the directory doesn't exist, it will be created.
K
Kohsuke Kawaguchi 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
     *
     * <P>
     * I/O operation to remote {@link FilePath} happens asynchronously, meaning write operations to the returned
     * {@link OutputStream} will return without receiving a confirmation from the remote that the write happened.
     * I/O operations also happens asynchronously from the {@link Channel#call(Callable)} operations, so if
     * you write to a remote file and then execute {@link Channel#call(Callable)} and try to access the newly copied
     * file, it might not be fully written yet.
     *
     * <p>
     *
K
kohsuke 已提交
1642
     */
1643
    public OutputStream write() throws IOException, InterruptedException {
1644
        if(channel==null) {
1645
            File f = new File(remote).getAbsoluteFile();
1646 1647 1648
            f.getParentFile().mkdirs();
            return new FileOutputStream(f);
        }
K
kohsuke 已提交
1649

1650
        return channel.call(new Callable<OutputStream,IOException>() {
1651
            private static final long serialVersionUID = 1L;
1652
            public OutputStream call() throws IOException {
1653
                File f = new File(remote).getAbsoluteFile();
K
kohsuke 已提交
1654 1655
                f.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(f);
1656
                return new RemoteOutputStream(fos);
K
kohsuke 已提交
1657 1658 1659 1660
            }
        });
    }

K
kohsuke 已提交
1661 1662 1663 1664 1665 1666 1667 1668
    /**
     * 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 已提交
1669
        act(new FileCallable<Void>() {
1670
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1671
            public Void invoke(File f, VirtualChannel channel) throws IOException {
K
kohsuke 已提交
1672 1673
                f.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(f);
K
kohsuke 已提交
1674
                Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos);
K
kohsuke 已提交
1675 1676 1677
                try {
                    w.write(content);
                } finally {
K
kohsuke 已提交
1678
                    w.close();
K
kohsuke 已提交
1679 1680 1681 1682 1683 1684
                }
                return null;
            }
        });
    }

K
kohsuke 已提交
1685 1686
    /**
     * Computes the MD5 digest of the file in hex string.
1687
     * @see Util#getDigestOf(File)
K
kohsuke 已提交
1688 1689 1690
     */
    public String digest() throws IOException, InterruptedException {
        return act(new FileCallable<String>() {
1691
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1692
            public String invoke(File f, VirtualChannel channel) throws IOException {
1693
                return Util.getDigestOf(f);
K
kohsuke 已提交
1694 1695 1696 1697
            }
        });
    }

1698 1699 1700 1701 1702 1703 1704 1705 1706
    /**
     * 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>() {
1707
            private static final long serialVersionUID = 1L;
1708 1709 1710
            public Void invoke(File f, VirtualChannel channel) throws IOException {
            	f.renameTo(new File(target.remote));
                return null;
K
kohsuke 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
            }
        });
    }

    /**
     * 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>() {
1725
            private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
            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;
1736 1737 1738 1739
            }
        });
    }

K
kohsuke 已提交
1740 1741 1742 1743 1744
    /**
     * Copies this file to the specified target.
     */
    public void copyTo(FilePath target) throws IOException, InterruptedException {
        try {
K
kohsuke 已提交
1745 1746 1747 1748 1749 1750 1751 1752
            OutputStream out = target.write();
            try {
                copyTo(out);
            } finally {
                out.close();
            }
        } catch (IOException e) {
            throw new IOException2("Failed to copy "+this+" to "+target,e);
K
kohsuke 已提交
1753 1754 1755
        }
    }

K
kohsuke 已提交
1756
    /**
1757
     * Copies this file to the specified target, with file permissions and other meta attributes intact.
K
kohsuke 已提交
1758 1759 1760
     * @since 1.311
     */
    public void copyToWithPermission(FilePath target) throws IOException, InterruptedException {
K
kohsuke 已提交
1761
        copyTo(target);
K
kohsuke 已提交
1762 1763
        // copy file permission
        target.chmod(mode());
1764
        target.setLastModifiedIfPossible(lastModified());
K
kohsuke 已提交
1765 1766
    }

K
kohsuke 已提交
1767 1768 1769 1770 1771 1772 1773
    /**
     * 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>() {
C
Christoph Kutzinski 已提交
1774
            private static final long serialVersionUID = 4088559042349254141L;
K
kohsuke 已提交
1775
            public Void invoke(File f, VirtualChannel channel) throws IOException {
1776 1777 1778 1779 1780 1781 1782 1783 1784
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(f);
                    Util.copyStream(fis,out);
                    return null;
                } finally {
                    IOUtils.closeQuietly(fis);
                    IOUtils.closeQuietly(out);
                }
K
kohsuke 已提交
1785 1786
            }
        });
1787

K
Kohsuke Kawaguchi 已提交
1788 1789
        // make sure the writes fully got delivered to 'os' before we return.
        // this is needed because I/O operation is asynchronous
1790 1791 1792
        syncIO();
    }

K
Kohsuke Kawaguchi 已提交
1793 1794 1795 1796 1797
    /**
     * With fix to JENKINS-11251 (remoting 2.15), this is no longer necessary.
     * But I'm keeping it for a while so that users who manually deploy slave.jar has time to deploy new version
     * before this goes away.
     */
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
    private void syncIO() throws InterruptedException {
        try {
            if (channel!=null)
                _syncIO();
        } catch (AbstractMethodError e) {
            // legacy slave.jar. Handle this gracefully
            try {
                LOGGER.log(Level.WARNING,"Looks like an old slave.jar. Please update "+ Which.jarFile(Channel.class)+" to the new version",e);
            } catch (IOException _) {
                // really ignore this time
            }
        }
    }

    /**
     * A pointless function to work around what appears to be a HotSpot problem. See JENKINS-5756 and bug 6933067
     * on BugParade for more details.
     */
    private void _syncIO() throws InterruptedException {
        channel.syncLocalIO();
K
kohsuke 已提交
1818 1819 1820 1821 1822 1823 1824 1825
    }

    /**
     * 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 已提交
1826 1827 1828 1829
        /**
         * @param fileName
         *      relative path name to the output file. Path separator must be '/'.
         */
K
kohsuke 已提交
1830 1831 1832 1833 1834
        void open(String fileName) throws IOException;
        void write(byte[] buf, int len) throws IOException;
        void close() throws IOException;
    }

K
kohsuke 已提交
1835 1836
    /**
     * Copies the contents of this directory recursively into the specified target directory.
C
Christoph Kutzinski 已提交
1837 1838 1839
     * 
     * @return
     *      the number of files copied.
K
kohsuke 已提交
1840 1841 1842 1843 1844 1845
     * @since 1.312 
     */
    public int copyRecursiveTo(FilePath target) throws IOException, InterruptedException {
        return copyRecursiveTo("**/*",target);
    }

C
Christoph Kutzinski 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    /**
     * Copies the files that match the given file mask to the specified target node.
     *
     * @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.
     * @return
     *      the number of files copied.
     */
K
kohsuke 已提交
1857 1858 1859 1860
    public int copyRecursiveTo(String fileMask, FilePath target) throws IOException, InterruptedException {
        return copyRecursiveTo(fileMask,null,target);
    }

K
kohsuke 已提交
1861 1862 1863
    /**
     * Copies the files that match the given file mask to the specified target node.
     *
K
kohsuke 已提交
1864 1865 1866 1867 1868
     * @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 已提交
1869 1870
     * @param excludes
     *      Files to be excluded. Can be null.
K
kohsuke 已提交
1871 1872 1873
     * @return
     *      the number of files copied.
     */
K
kohsuke 已提交
1874
    public int copyRecursiveTo(final String fileMask, final String excludes, final FilePath target) throws IOException, InterruptedException {
1875 1876 1877 1878 1879
        return copyRecursiveTo(new DirScanner.Glob(fileMask, excludes), target, fileMask);
    }

    /**
     * Copies files according to a specified scanner to a target node.
J
Jesse Glick 已提交
1880
     * @param scanner a way of enumerating some files (must be serializable for possible delivery to remote side)
1881 1882 1883
     * @param target the destination basedir
     * @param description a description of the fileset, for logging purposes
     * @return the number of files copied
K
Kohsuke Kawaguchi 已提交
1884
     * @since 1.532
1885 1886
     */
    public int copyRecursiveTo(final DirScanner scanner, final FilePath target, final String description) throws IOException, InterruptedException {
K
kohsuke 已提交
1887 1888 1889
        if(this.channel==target.channel) {
            // local to local copy.
            return act(new FileCallable<Integer>() {
1890
                private static final long serialVersionUID = 1L;
K
kohsuke 已提交
1891
                public Integer invoke(File base, VirtualChannel channel) throws IOException {
1892
                    if(!base.exists())  return 0;
K
kohsuke 已提交
1893
                    assert target.channel==null;
1894 1895 1896 1897 1898 1899 1900 1901 1902
                    final File dest = new File(target.remote);
                    final AtomicInteger count = new AtomicInteger();
                    scanner.scan(base, new FileVisitor() {
                        @Override public void visit(File f, String relativePath) throws IOException {
                            if (f.isFile()) {
                                File target = new File(dest, relativePath);
                                target.getParentFile().mkdirs();
                                Util.copyFile(f, target);
                                count.incrementAndGet();
K
kohsuke 已提交
1903
                            }
1904 1905 1906 1907 1908 1909 1910 1911
                        }
                        @Override public boolean understandsSymlink() {
                            return true;
                        }
                        @Override public void visitSymlink(File link, String target, String relativePath) throws IOException {
                            try {
                                Util.createSymlink(dest, target, relativePath, TaskListener.NULL);
                            } catch (InterruptedException x) {
J
Jesse Glick 已提交
1912
                                throw (IOException) new IOException(x.toString()).initCause(x);
K
kohsuke 已提交
1913
                            }
1914
                            count.incrementAndGet();
K
kohsuke 已提交
1915
                        }
1916 1917
                    });
                    return count.get();
K
kohsuke 已提交
1918 1919
                }
            });
1920 1921 1922 1923 1924 1925
        } else
        if(this.channel==null) {
            // local -> remote copy
            final Pipe pipe = Pipe.createLocalToRemote();

            Future<Void> future = target.actAsync(new FileCallable<Void>() {
1926
                private static final long serialVersionUID = 1L;
1927
                public Void invoke(File f, VirtualChannel channel) throws IOException {
1928
                    try {
1929
                        readFromTar(remote + '/' + description, f,TarCompression.GZIP.extract(pipe.getIn()));
1930 1931 1932 1933
                        return null;
                    } finally {
                        pipe.getIn().close();
                    }
1934 1935
                }
            });
1936 1937 1938
            Future<Integer> future2 = actAsync(new FileCallable<Integer>() {
                private static final long serialVersionUID = 1L;
                @Override public Integer invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
1939
                    return writeToTar(new File(remote), scanner, TarCompression.GZIP.compress(pipe.getOut()));
1940 1941
                }
            });
1942
            try {
1943
                // JENKINS-9540 in case the reading side failed, report that error first
1944
                future.get();
1945
                return future2.get();
1946 1947 1948
            } catch (ExecutionException e) {
                throw new IOException2(e);
            }
K
kohsuke 已提交
1949
        } else {
1950 1951
            // remote -> local copy
            final Pipe pipe = Pipe.createRemoteToLocal();
K
kohsuke 已提交
1952

1953
            Future<Integer> future = actAsync(new FileCallable<Integer>() {
1954
                private static final long serialVersionUID = 1L;
1955
                public Integer invoke(File f, VirtualChannel channel) throws IOException {
1956
                    try {
1957
                        return writeToTar(f, scanner, TarCompression.GZIP.compress(pipe.getOut()));
1958 1959 1960
                    } finally {
                        pipe.getOut().close();
                    }
K
kohsuke 已提交
1961 1962
                }
            });
1963
            try {
1964
                readFromTar(remote + '/' + description,new File(target.remote),TarCompression.GZIP.extract(pipe.getIn()));
1965 1966 1967 1968 1969 1970
            } 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
1971
                    throw new IOException2(Functions.printThrowable(e),x);
1972
                } catch (TimeoutException _) {
1973 1974 1975 1976
                    // remote is hanging
                    throw e;
                }
            }
1977 1978 1979 1980 1981 1982 1983 1984
            try {
                return future.get();
            } catch (ExecutionException e) {
                throw new IOException2(e);
            }
        }
    }

1985 1986 1987 1988 1989 1990 1991 1992

    /**
     * Writes files in 'this' directory to a tar stream.
     *
     * @param glob
     *      Ant file pattern mask, like "**&#x2F;*.java".
     */
    public int tar(OutputStream out, final String glob) throws IOException, InterruptedException {
1993
        return archive(ArchiverFactory.TAR, out, glob);
1994 1995 1996
    }

    public int tar(OutputStream out, FileFilter filter) throws IOException, InterruptedException {
1997
        return archive(ArchiverFactory.TAR, out, filter);
1998 1999
    }

2000 2001 2002 2003
    /**
     * Uses the given scanner on 'this' directory to list up files and then archive it to a tar stream.
     */
    public int tar(OutputStream out, DirScanner scanner) throws IOException, InterruptedException {
2004
        return archive(ArchiverFactory.TAR, out, scanner);
2005 2006
    }

2007 2008 2009 2010 2011 2012
    /**
     * Writes to a tar stream and stores obtained files to the base dir.
     *
     * @return
     *      number of files/directories that are written.
     */
2013
    private static Integer writeToTar(File baseDir, DirScanner scanner, OutputStream out) throws IOException {
2014
        Archiver tw = ArchiverFactory.TAR.create(out);
2015
        try {
2016
            scanner.scan(baseDir,tw);
2017 2018 2019
        } finally {
            tw.close();
        }
2020
        return tw.countEntries();
2021 2022 2023 2024 2025
    }

    /**
     * Reads from a tar stream and stores obtained files to the base dir.
     */
K
kohsuke 已提交
2026 2027
    private static void readFromTar(String name, File baseDir, InputStream in) throws IOException {
        TarInputStream t = new TarInputStream(in);
2028
        try {
K
kohsuke 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037
            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();

K
Kohsuke Kawaguchi 已提交
2038 2039 2040 2041 2042
                    byte linkFlag = (Byte) LINKFLAG_FIELD.get(te);
                    if (linkFlag==TarEntry.LF_SYMLINK) {
                        new FilePath(f).symlinkTo(te.getLinkName(), TaskListener.NULL);
                    } else {
                        IOUtils.copy(t,f);
2043 2044 2045 2046 2047

                        f.setLastModified(te.getModTime().getTime());
                        int mode = te.getMode()&0777;
                        if(mode!=0 && !Functions.isWindows()) // be defensive
                            _chmod(f,mode);
K
Kohsuke Kawaguchi 已提交
2048
                    }
K
kohsuke 已提交
2049 2050 2051 2052
                }
            }
        } catch(IOException e) {
            throw new IOException2("Failed to extract "+name,e);
K
Kohsuke Kawaguchi 已提交
2053 2054 2055 2056 2057
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // process this later
            throw new IOException2("Failed to extract "+name,e);
        } catch (IllegalAccessException e) {
            throw new IOException2("Failed to extract "+name,e);
K
kohsuke 已提交
2058 2059
        } finally {
            t.close();
2060
        }
K
kohsuke 已提交
2061 2062
    }

2063 2064
    /**
     * Creates a {@link Launcher} for starting processes on the node
K
typo.  
kohsuke 已提交
2065
     * that has this file.
2066
     * @since 1.89
2067
     */
2068
    public Launcher createLauncher(TaskListener listener) throws IOException, InterruptedException {
2069 2070 2071
        if(channel==null)
            return new LocalLauncher(listener);
        else
2072 2073 2074 2075 2076 2077 2078 2079
            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;
2080 2081
    }

2082
    /**
K
kohsuke 已提交
2083
     * Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar")
2084 2085
     * against this directory, and try to point out the problem.
     *
K
kohsuke 已提交
2086
     * <p>
K
kohsuke 已提交
2087
     * This is useful in conjunction with {@link FormValidation}.
K
kohsuke 已提交
2088
     *
2089
     * @return
2090
     *      null if no error was found. Otherwise returns a human readable error message.
2091
     * @since 1.90
K
kohsuke 已提交
2092
     * @see #validateFileMask(FilePath, String)
2093
     */
K
kohsuke 已提交
2094
    public String validateAntFileMask(final String fileMasks) throws IOException, InterruptedException {
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
        return validateAntFileMask(fileMasks, Integer.MAX_VALUE);
    }

    /**
     * Like {@link #validateAntFileMask(String)} but performing only a bounded number of operations.
     * <p>Whereas the unbounded overload is appropriate for calling from cancelable, long-running tasks such as build steps,
     * this overload should be used when an answer is needed quickly, such as for {@link #validateFileMask(String)}
     * or anything else returning {@link FormValidation}.
     * <p>If a positive match is found, {@code null} is returned immediately.
     * A message is returned in case the file pattern can definitely be determined to not match anything in the directory within the alloted time.
     * If the time runs out without finding a match but without ruling out the possibility that there might be one, {@link InterruptedException} is thrown,
     * in which case the calling code should give the user the benefit of the doubt and use {@link hudson.util.FormValidation.Kind#OK} (with or without a message).
     * @param bound a maximum number of negative operations (deliberately left vague) to perform before giving up on a precise answer; 10_000 is a reasonable pick
     * @throws InterruptedException not only in case of a channel failure, but also if too many operations were performed without finding any matches
     * @since 1.484
     */
    public String validateAntFileMask(final String fileMasks, final int bound) throws IOException, InterruptedException {
2112
        return act(new FileCallable<String>() {
2113 2114
            private static final long serialVersionUID = 1;
            public String invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
2115 2116 2117
                if(fileMasks.startsWith("~"))
                    return Messages.FilePath_TildaDoesntWork();

K
kohsuke 已提交
2118
                StringTokenizer tokens = new StringTokenizer(fileMasks,",");
K
kohsuke 已提交
2119 2120 2121

                while(tokens.hasMoreTokens()) {
                    final String fileMask = tokens.nextToken().trim();
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
                    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 已提交
2148
                        }
2149
                    }
2150

2151
                    {// check the (2) above next as this is more expensive.
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
                        // 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 已提交
2177
                        }
2178
                    }
2179

2180 2181 2182 2183 2184 2185 2186 2187
                    {// 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)
S
sogabe 已提交
2188
                                    return Messages.FilePath_validateAntFileMask_portionMatchAndSuggest(fileMask,pattern);
2189
                                else
S
sogabe 已提交
2190
                                    return Messages.FilePath_validateAntFileMask_portionMatchButPreviousNotMatchAndSuggest(fileMask,pattern,previous);
2191 2192 2193 2194 2195
                            }

                            int idx = findSeparator(pattern);
                            if(idx<0) {// no more path component left to go back
                                if(pattern.equals(fileMask))
S
sogabe 已提交
2196
                                    return Messages.FilePath_validateAntFileMask_doesntMatchAnything(fileMask);
2197
                                else
S
sogabe 已提交
2198
                                    return Messages.FilePath_validateAntFileMask_doesntMatchAnythingAndSuggest(fileMask,pattern);
2199 2200 2201 2202 2203 2204
                            }

                            // cut off the trailing component and try again
                            previous = pattern;
                            pattern = pattern.substring(0,idx);
                        }
K
kohsuke 已提交
2205
                    }
2206
                }
K
kohsuke 已提交
2207 2208

                return null; // no error
2209
            }
2210

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
            private boolean hasMatch(File dir, String pattern) throws InterruptedException {
                class Cancel extends RuntimeException {}
                DirectoryScanner ds = bound == Integer.MAX_VALUE ? new DirectoryScanner() : new DirectoryScanner() {
                    int ticks;
                    @Override public synchronized boolean isCaseSensitive() {
                        if (!filesIncluded.isEmpty() || !dirsIncluded.isEmpty() || ticks++ > bound) {
                            throw new Cancel();
                        }
                        return super.isCaseSensitive();
                    }
                };
                ds.setBasedir(dir);
                ds.setIncludes(new String[] {pattern});
                try {
                    ds.scan();
                } catch (Cancel c) {
                    if (ds.getIncludedFilesCount()!=0 || ds.getIncludedDirsCount()!=0) {
                        return true;
                    } else {
                        throw new InterruptedException("no matches found within " + bound);
                    }
                }
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
                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);
            }
2246 2247 2248
        });
    }

K
kohsuke 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
    /**
     * 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);
    }

    /**
2265
     * Checks the GLOB-style file mask. See {@link #validateAntFileMask(String)}.
2266 2267
     * Requires configure permission on ancestor AbstractProject object in request,
     * or admin permission if no such ancestor is found.
K
kohsuke 已提交
2268 2269 2270
     * @since 1.294
     */
    public FormValidation validateFileMask(String value, boolean errorIfNotExist) throws IOException {
2271
        checkPermissionForValidate();
2272

K
kohsuke 已提交
2273 2274 2275 2276 2277 2278 2279 2280
        value = fixEmpty(value);
        if(value==null)
            return FormValidation.ok();

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

2281
            String msg = validateAntFileMask(value, 10000);
K
kohsuke 已提交
2282 2283 2284
            if(errorIfNotExist)     return FormValidation.error(msg);
            else                    return FormValidation.warning(msg);
        } catch (InterruptedException e) {
2285
            return FormValidation.ok(Messages.FilePath_did_not_manage_to_validate_may_be_too_sl(value));
K
kohsuke 已提交
2286 2287 2288 2289 2290
        }
    }

    /**
     * Validates a relative file path from this {@link FilePath}.
2291 2292
     * Requires configure permission on ancestor AbstractProject object in request,
     * or admin permission if no such ancestor is found.
K
kohsuke 已提交
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
     *
     * @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 {
2303
        checkPermissionForValidate();
K
kohsuke 已提交
2304 2305 2306 2307

        value = fixEmpty(value);

        // none entered yet, or something is seriously wrong
2308
        if(value==null) return FormValidation.ok();
K
kohsuke 已提交
2309 2310

        // a common mistake is to use wildcard
S
sogabe 已提交
2311
        if(value.contains("*")) return FormValidation.error(Messages.FilePath_validateRelativePath_wildcardNotAllowed());
K
kohsuke 已提交
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

        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
S
sogabe 已提交
2323
                        return FormValidation.error(Messages.FilePath_validateRelativePath_notFile(value));
K
kohsuke 已提交
2324 2325 2326 2327
                } else {
                    if(path.isDirectory())
                        return FormValidation.ok();
                    else
S
sogabe 已提交
2328
                        return FormValidation.error(Messages.FilePath_validateRelativePath_notDirectory(value));
K
kohsuke 已提交
2329 2330 2331
                }
            }

S
sogabe 已提交
2332 2333
            String msg = expectingFile ? Messages.FilePath_validateRelativePath_noSuchFile(value) : 
                Messages.FilePath_validateRelativePath_noSuchDirectory(value);
K
kohsuke 已提交
2334 2335 2336 2337 2338 2339 2340
            if(errorIfNotExist)     return FormValidation.error(msg);
            else                    return FormValidation.warning(msg);
        } catch (InterruptedException e) {
            return FormValidation.ok();
        }
    }

2341 2342 2343
    private static void checkPermissionForValidate() {
        AccessControlled subject = Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class);
        if (subject == null)
2344
            Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
2345 2346 2347 2348
        else
            subject.checkPermission(Item.CONFIGURE);
    }

K
kohsuke 已提交
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
    /**
     * 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);
    }

2360
    @Deprecated @Override
K
kohsuke 已提交
2361 2362
    public String toString() {
        // to make writing JSPs easily, return local
K
kohsuke 已提交
2363
        return remote;
K
kohsuke 已提交
2364 2365
    }

K
kohsuke 已提交
2366 2367
    public VirtualChannel getChannel() {
        if(channel!=null)   return channel;
2368
        else                return Jenkins.MasterComputer.localChannel;
K
kohsuke 已提交
2369 2370
    }

K
kohsuke 已提交
2371 2372 2373 2374
    /**
     * Returns true if this {@link FilePath} represents a remote file. 
     */
    public boolean isRemote() {
K
typo.  
kohsuke 已提交
2375
        return channel!=null;
K
kohsuke 已提交
2376 2377
    }

K
kohsuke 已提交
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
    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;
2401

2402 2403
    public static int SIDE_BUFFER_SIZE = 1024;

R
rseguy 已提交
2404 2405
    private static final Logger LOGGER = Logger.getLogger(FilePath.class.getName());

2406 2407 2408 2409 2410
    /**
     * Adapts {@link FileCallable} to {@link Callable}.
     */
    private class FileCallableWrapper<T> implements DelegatingCallable<T,IOException> {
        private final FileCallable<T> callable;
2411
        private transient ClassLoader classLoader;
2412 2413 2414

        public FileCallableWrapper(FileCallable<T> callable) {
            this.callable = callable;
2415 2416 2417 2418 2419 2420
            this.classLoader = callable.getClass().getClassLoader();
        }

        private FileCallableWrapper(FileCallable<T> callable, ClassLoader classLoader) {
            this.callable = callable;
            this.classLoader = classLoader;
2421 2422 2423
        }

        public T call() throws IOException {
2424 2425 2426 2427 2428
            try {
                return callable.invoke(new File(remote), Channel.current());
            } catch (InterruptedException e) {
                throw new TunneledInterruptedException(e);
            }
2429 2430 2431
        }

        public ClassLoader getClassLoader() {
2432
            return classLoader;
2433 2434 2435 2436
        }

        private static final long serialVersionUID = 1L;
    }
2437

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
    /**
     * Used to tunnel {@link InterruptedException} over a Java signature that only allows {@link IOException}
     */
    private static class TunneledInterruptedException extends IOException2 {
        private TunneledInterruptedException(InterruptedException cause) {
            super(cause);
        }
        private static final long serialVersionUID = 1L;
    }

2448 2449 2450 2451 2452
    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 Kawaguchi 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466

    private static final Field LINKFLAG_FIELD = getTarEntryLinkFlagField();

    private static Field getTarEntryLinkFlagField() {
        try {
            Field f = TarEntry.class.getDeclaredField("linkFlag");
            f.setAccessible(true);
            return f;
        } catch (SecurityException e) {
            throw new AssertionError(e);
        } catch (NoSuchFieldException e) {
            throw new AssertionError(e);
        }
    }
K
Kohsuke Kawaguchi 已提交
2467 2468 2469 2470 2471 2472

    /**
     * Gets the {@link FilePath} representation of the "~" directory
     * (User's home directory in the Unix sense) of the given channel.
     */
    public static FilePath getHomeDirectory(VirtualChannel ch) throws InterruptedException, IOException {
S
ssogabe 已提交
2473
        return ch.call(new Callable<FilePath,IOException>() {
K
Kohsuke Kawaguchi 已提交
2474 2475 2476 2477 2478
            public FilePath call() throws IOException {
                return new FilePath(new File(System.getProperty("user.home")));
            }
        });
    }
2479 2480 2481

    /**
     * Helper class to make it easy to send an explicit list of files using {@link FilePath} methods.
K
Kohsuke Kawaguchi 已提交
2482
     * @since 1.532
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
     */
    public static final class ExplicitlySpecifiedDirScanner extends DirScanner {

        private static final long serialVersionUID = 1;

        private final Map<String,String> files;

        /**
         * Create a “scanner” (it actually does no scanning).
         * @param files a map from logical relative paths as per {@link FileVisitor#visit}, to actual relative paths within the scanned directory
         */
        public ExplicitlySpecifiedDirScanner(Map<String,String> files) {
            this.files = files;
        }

        @Override public void scan(File dir, FileVisitor visitor) throws IOException {
            for (Map.Entry<String,String> entry : files.entrySet()) {
                String archivedPath = entry.getKey();
                assert archivedPath.indexOf('\\') == -1;
                String workspacePath = entry.getValue();
                assert workspacePath.indexOf('\\') == -1;
                scanSingle(new File(dir, workspacePath), archivedPath, visitor);
            }
        }

    }

K
kohsuke 已提交
2510
}