TarArchiver.java 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * The MIT License
 *
 * Copyright (c) 2010, InfraDNA, Inc.
 *
 * 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.
 */

package hudson.util.io;

import hudson.Functions;
import hudson.org.apache.tools.tar.TarOutputStream;
29
import hudson.os.PosixException;
30
import hudson.util.FileVisitor;
31
import hudson.util.IOUtils;
32 33 34 35 36 37 38
import org.apache.tools.tar.TarEntry;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
K
kohsuke 已提交
39 40 41
import java.lang.reflect.Field;

import static org.apache.tools.tar.TarConstants.LF_SYMLINK;
42 43 44 45 46 47 48 49

/**
 * {@link FileVisitor} that creates a tar archive.
 *
 * @see ArchiverFactory#TAR
 */
final class TarArchiver extends Archiver {
    private final byte[] buf = new byte[8192];
50
    private final TarOutputStream tar;
51 52

    TarArchiver(OutputStream out) {
53
        tar = new TarOutputStream(new BufferedOutputStream(out) {
54 55 56 57 58 59 60
            // TarOutputStream uses TarBuffer internally,
            // which flushes the stream for each block. this creates unnecessary
            // data stream fragmentation, and flush request to a remote, which slows things down.
            @Override
            public void flush() throws IOException {
                // so don't do anything in flush
            }
61 62
        });
        tar.setLongFileMode(TarOutputStream.LONGFILE_GNU);
63 64
    }

K
kohsuke 已提交
65 66
    @Override
    public void visitSymlink(File link, String target, String relativePath) throws IOException {
67
        TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
68 69 70 71 72 73 74 75
        try {
            int mode = IOUtils.mode(link);
            if (mode != -1) {
                e.setMode(mode);
            }
        } catch (PosixException x) {
            // ignore
        }
K
kohsuke 已提交
76

77 78 79 80 81 82 83 84 85
        try {
            StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e);
            linkName.setLength(0);
            linkName.append(target);
        } catch (IllegalAccessException x) {
            throw new IOException("Failed to set linkName", x);
        }

        tar.putNextEntry(e);
K
kohsuke 已提交
86 87 88 89 90 91 92 93
        entriesWritten++;
    }

    @Override
    public boolean understandsSymlink() {
        return true;
    }

94 95 96 97 98 99
    public void visit(File file, String relativePath) throws IOException {
        if(Functions.isWindows())
            relativePath = relativePath.replace('\\','/');

        if(file.isDirectory())
            relativePath+='/';
100
        TarEntry te = new TarEntry(relativePath);
101 102
        int mode = IOUtils.mode(file);
        if (mode!=-1)   te.setMode(mode);
103 104 105 106
        te.setModTime(file.lastModified());
        if(!file.isDirectory())
            te.setSize(file.length());

107
        tar.putNextEntry(te);
108 109 110 111 112 113 114 115 116 117 118 119

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

120
        tar.closeEntry();
121 122 123 124 125 126
        entriesWritten++;
    }

    public void close() throws IOException {
        tar.close();
    }
127 128 129 130 131 132 133 134 135 136 137 138 139 140

    private static final Field LINKNAME_FIELD = getTarEntryLinkNameField();

    private static Field getTarEntryLinkNameField() {
        try {
            Field f = TarEntry.class.getDeclaredField("linkName");
            f.setAccessible(true);
            return f;
        } catch (SecurityException e) {
            throw new AssertionError(e);
        } catch (NoSuchFieldException e) {
            throw new AssertionError(e);
        }
    }
141
}