Search in sources :

Example 11 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarTaskOutputPacker method storeDirectoryProperty.

private void storeDirectoryProperty(String propertyPath, File directory, final TarOutputStream outputStream) throws IOException {
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException(String.format("Expected '%s' to be a directory", directory));
    }
    final String propertyRoot = propertyPath + "/";
    outputStream.putNextEntry(new TarEntry(propertyRoot));
    outputStream.closeEntry();
    FileVisitor visitor = new FileVisitor() {

        @Override
        public void visitDir(FileVisitDetails dirDetails) {
            try {
                storeDirectoryEntry(dirDetails, propertyRoot, outputStream);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            try {
                String path = propertyRoot + fileDetails.getRelativePath().getPathString();
                storeFileEntry(fileDetails.getFile(), path, fileDetails.getLastModified(), fileDetails.getSize(), fileDetails.getMode(), outputStream);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    };
    directoryWalkerFactory.create().walkDir(directory, RelativePath.EMPTY_ROOT, visitor, Specs.satisfyAll(), new AtomicBoolean(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileVisitDetails(org.gradle.api.file.FileVisitDetails) TarEntry(org.apache.tools.tar.TarEntry) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 12 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarTaskOutputPacker method unpack.

private void unpack(TaskOutputsInternal taskOutputs, TarInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException {
    Map<String, TaskOutputFilePropertySpec> propertySpecs = Maps.uniqueIndex(taskOutputs.getFileProperties(), new Function<TaskFilePropertySpec, String>() {

        @Override
        public String apply(TaskFilePropertySpec propertySpec) {
            return propertySpec.getPropertyName();
        }
    });
    boolean originSeen = false;
    TarEntry entry;
    while ((entry = tarInput.getNextEntry()) != null) {
        String name = entry.getName();
        if (name.equals(METADATA_PATH)) {
            // handle origin metadata
            originSeen = true;
            readOriginAction.execute(new CloseShieldInputStream(tarInput));
        } else {
            // handle output property
            Matcher matcher = PROPERTY_PATH.matcher(name);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached result format error, invalid contents: " + name);
            }
            String propertyName = matcher.group(2);
            CacheableTaskOutputFilePropertySpec propertySpec = (CacheableTaskOutputFilePropertySpec) propertySpecs.get(propertyName);
            if (propertySpec == null) {
                throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
            }
            boolean outputMissing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            unpackPropertyEntry(propertySpec, tarInput, entry, childPath, outputMissing);
        }
    }
    if (!originSeen) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
}
Also used : CacheableTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.CacheableTaskOutputFilePropertySpec) TaskOutputFilePropertySpec(org.gradle.api.internal.tasks.TaskOutputFilePropertySpec) Matcher(java.util.regex.Matcher) TaskFilePropertySpec(org.gradle.api.internal.tasks.TaskFilePropertySpec) TarEntry(org.apache.tools.tar.TarEntry) CacheableTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.CacheableTaskOutputFilePropertySpec) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 13 with TarEntry

use of org.apache.tools.tar.TarEntry in project gradle by gradle.

the class TarTaskOutputPacker method storeMissingProperty.

private void storeMissingProperty(String propertyPath, TarOutputStream outputStream) throws IOException {
    TarEntry entry = new TarEntry("missing-" + propertyPath);
    outputStream.putNextEntry(entry);
    outputStream.closeEntry();
}
Also used : TarEntry(org.apache.tools.tar.TarEntry)

Example 14 with TarEntry

use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.

the class TarArchiver method visit.

public void visit(File file, String relativePath) throws IOException {
    if (Functions.isWindows())
        relativePath = relativePath.replace('\\', '/');
    if (file.isDirectory())
        relativePath += '/';
    TarEntry te = new TarEntry(relativePath);
    te.setModTime(file.lastModified());
    if (!file.isDirectory())
        te.setSize(file.length());
    tar.putNextEntry(te);
    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();
        }
    }
    tar.closeEntry();
    entriesWritten++;
}
Also used : TarEntry(org.apache.tools.tar.TarEntry) FileInputStream(java.io.FileInputStream)

Example 15 with TarEntry

use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.

the class FilePath method readFromTar.

/**
     * Reads from a tar stream and stores obtained files to the base dir.
     */
private static void readFromTar(String name, File baseDir, InputStream in) throws IOException {
    TarInputStream t = new TarInputStream(in);
    try {
        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();
                IOUtils.copy(t, f);
                f.setLastModified(te.getModTime().getTime());
                int mode = te.getMode() & 0777;
                if (// be defensive
                mode != 0 && !Functions.isWindows())
                    _chmod(f, mode);
            }
        }
    } catch (IOException e) {
        throw new IOException2("Failed to extract " + name, e);
    } finally {
        t.close();
    }
}
Also used : TarInputStream(hudson.org.apache.tools.tar.TarInputStream) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) File(java.io.File) IOException2(hudson.util.IOException2)

Aggregations

TarEntry (org.apache.tools.tar.TarEntry)16 File (java.io.File)5 IOException (java.io.IOException)4 TarInputStream (org.apache.tools.tar.TarInputStream)3 IOException2 (hudson.util.IOException2)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 TextField (org.apache.lucene.document.TextField)1 TarOutputStream (org.apache.tools.tar.TarOutputStream)1