Search in sources :

Example 1 with FileHashSnapshot

use of org.gradle.api.internal.changedetection.state.FileHashSnapshot in project gradle by gradle.

the class TarTaskOutputPacker method unpackPropertyEntry.

private void unpackPropertyEntry(ResolvedTaskOutputFilePropertySpec propertySpec, InputStream input, TarArchiveEntry entry, String childPath, boolean missing, ImmutableMultimap.Builder<String, FileSnapshot> fileSnapshots) throws IOException {
    File propertyRoot = propertySpec.getOutputFile();
    String propertyName = propertySpec.getPropertyName();
    if (propertyRoot == null) {
        throw new IllegalStateException("Optional property should have a value: " + propertyName);
    }
    File outputFile;
    boolean isDirEntry = entry.isDirectory();
    boolean root = Strings.isNullOrEmpty(childPath);
    if (root) {
        // We are handling the root of the property here
        if (missing) {
            if (!makeDirectory(propertyRoot.getParentFile())) {
                // Make sure output is removed if it exists already
                if (propertyRoot.exists()) {
                    FileUtils.forceDelete(propertyRoot);
                }
            }
            return;
        }
        OutputType outputType = propertySpec.getOutputType();
        if (isDirEntry) {
            if (outputType != OutputType.DIRECTORY) {
                throw new IllegalStateException("Property should be an output directory property: " + propertyName);
            }
        } else {
            if (outputType == OutputType.DIRECTORY) {
                throw new IllegalStateException("Property should be an output file property: " + propertyName);
            }
        }
        ensureDirectoryForProperty(outputType, propertyRoot);
        outputFile = propertyRoot;
    } else {
        outputFile = new File(propertyRoot, childPath);
    }
    String internedPath = stringInterner.intern(outputFile.getAbsolutePath());
    RelativePath relativePath = root ? RelativePath.parse(!isDirEntry, outputFile.getName()) : RelativePath.parse(!isDirEntry, childPath);
    if (isDirEntry) {
        FileUtils.forceMkdir(outputFile);
        fileSnapshots.put(propertyName, new DirectoryFileSnapshot(internedPath, relativePath, root));
    } else {
        OutputStream output = new FileOutputStream(outputFile);
        HashCode hash;
        try {
            hash = streamHasher.hashCopy(input, output);
        } finally {
            IOUtils.closeQuietly(output);
        }
        FileHashSnapshot contentSnapshot = new FileHashSnapshot(hash, outputFile.lastModified());
        fileSnapshots.put(propertyName, new RegularFileSnapshot(internedPath, relativePath, root, contentSnapshot));
    }
    fileSystem.chmod(outputFile, entry.getMode() & FILE_PERMISSION_MASK);
}
Also used : DirectoryFileSnapshot(org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot) RelativePath(org.gradle.api.file.RelativePath) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.api.internal.changedetection.state.RegularFileSnapshot) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileHashSnapshot(org.gradle.api.internal.changedetection.state.FileHashSnapshot) File(java.io.File) OutputType(org.gradle.api.internal.tasks.OutputType)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)1 RelativePath (org.gradle.api.file.RelativePath)1 DirectoryFileSnapshot (org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot)1 FileHashSnapshot (org.gradle.api.internal.changedetection.state.FileHashSnapshot)1 RegularFileSnapshot (org.gradle.api.internal.changedetection.state.RegularFileSnapshot)1 OutputType (org.gradle.api.internal.tasks.OutputType)1 HashCode (org.gradle.internal.hash.HashCode)1