Search in sources :

Example 1 with RegularFileSnapshot

use of org.gradle.internal.snapshot.RegularFileSnapshot in project gradle by gradle.

the class TarBuildCacheEntryPacker method unpackTree.

@Nullable
private TarArchiveEntry unpackTree(String treeName, TreeType treeType, File treeRoot, TarArchiveInputStream input, TarArchiveEntry rootEntry, String childPath, boolean missing, Map<String, FileSystemLocationSnapshot> snapshots, AtomicLong entries) throws IOException {
    boolean isDirEntry = rootEntry.isDirectory();
    boolean root = Strings.isNullOrEmpty(childPath);
    if (!root) {
        throw new IllegalStateException("Root needs to be the first entry in a tree");
    }
    // We are handling the root of the tree here
    if (missing) {
        fileSystemSupport.ensureFileIsMissing(treeRoot);
        return input.getNextTarEntry();
    }
    fileSystemSupport.ensureDirectoryForTree(treeType, treeRoot);
    if (treeType == TreeType.FILE) {
        if (isDirEntry) {
            throw new IllegalStateException("Should be a file: " + treeName);
        }
        RegularFileSnapshot fileSnapshot = unpackFile(input, rootEntry, treeRoot, treeRoot.getName());
        snapshots.put(treeName, fileSnapshot);
        return input.getNextTarEntry();
    }
    if (!isDirEntry) {
        throw new IllegalStateException("Should be a directory: " + treeName);
    }
    chmodUnpackedFile(rootEntry, treeRoot);
    return unpackDirectoryTree(input, rootEntry, snapshots, entries, treeRoot, treeName);
}
Also used : RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) Nullable(javax.annotation.Nullable)

Example 2 with RegularFileSnapshot

use of org.gradle.internal.snapshot.RegularFileSnapshot in project gradle by gradle.

the class TarBuildCacheEntryPacker method unpackFile.

private RegularFileSnapshot unpackFile(TarArchiveInputStream input, TarArchiveEntry entry, File file, String fileName) throws IOException {
    try (CountingOutputStream output = new CountingOutputStream(new FileOutputStream(file))) {
        HashCode hash = streamHasher.hashCopy(input, output);
        chmodUnpackedFile(entry, file);
        String internedAbsolutePath = stringInterner.intern(file.getAbsolutePath());
        String internedFileName = stringInterner.intern(fileName);
        return new RegularFileSnapshot(internedAbsolutePath, internedFileName, hash, DefaultFileMetadata.file(output.getCount(), file.lastModified(), DIRECT));
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) FileOutputStream(java.io.FileOutputStream)

Example 3 with RegularFileSnapshot

use of org.gradle.internal.snapshot.RegularFileSnapshot in project gradle by gradle.

the class DefaultFileSystemAccess method snapshot.

private FileSystemLocationSnapshot snapshot(String location, SnapshottingFilter filter) {
    File file = new File(location);
    FileMetadata fileMetadata = this.stat.stat(file);
    switch(fileMetadata.getType()) {
        case RegularFile:
            HashCode hash = hasher.hash(file, fileMetadata.getLength(), fileMetadata.getLastModified());
            RegularFileSnapshot regularFileSnapshot = new RegularFileSnapshot(location, file.getName(), hash, fileMetadata);
            virtualFileSystem.store(regularFileSnapshot.getAbsolutePath(), regularFileSnapshot);
            return regularFileSnapshot;
        case Missing:
            MissingFileSnapshot missingFileSnapshot = new MissingFileSnapshot(location, fileMetadata.getAccessType());
            virtualFileSystem.store(missingFileSnapshot.getAbsolutePath(), missingFileSnapshot);
            return missingFileSnapshot;
        case Directory:
            AtomicBoolean hasBeenFiltered = new AtomicBoolean(false);
            FileSystemLocationSnapshot directorySnapshot = directorySnapshotter.snapshot(location, filter.isEmpty() ? null : filter.getAsDirectoryWalkerPredicate(), hasBeenFiltered, snapshot -> virtualFileSystem.store(snapshot.getAbsolutePath(), snapshot));
            if (!hasBeenFiltered.get()) {
                virtualFileSystem.store(directorySnapshot.getAbsolutePath(), directorySnapshot);
            }
            return directorySnapshot;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) FileMetadata(org.gradle.internal.file.FileMetadata) MissingFileSnapshot(org.gradle.internal.snapshot.MissingFileSnapshot) File(java.io.File)

Example 4 with RegularFileSnapshot

use of org.gradle.internal.snapshot.RegularFileSnapshot in project gradle by gradle.

the class AbiExtractingClasspathResourceHasher method hash.

@Nullable
@Override
public HashCode hash(RegularFileSnapshotContext fileSnapshotContext) {
    RegularFileSnapshot fileSnapshot = fileSnapshotContext.getSnapshot();
    try {
        if (!isClassFile(fileSnapshot.getName())) {
            return null;
        }
        Path path = Paths.get(fileSnapshot.getAbsolutePath());
        byte[] classBytes = Files.readAllBytes(path);
        return hashClassBytes(classBytes);
    } catch (Exception e) {
        LOGGER.debug("Malformed class file '{}' found on compile classpath. Falling back to full file hash instead of ABI hashing.", fileSnapshot.getName(), e);
        return fileSnapshot.getHash();
    }
}
Also used : Path(java.nio.file.Path) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 5 with RegularFileSnapshot

use of org.gradle.internal.snapshot.RegularFileSnapshot in project gradle by gradle.

the class FileSystemSnapshotSerializer method write.

@Override
public void write(Encoder encoder, FileSystemSnapshot value) throws Exception {
    value.accept(new RootTrackingFileSystemSnapshotHierarchyVisitor() {

        @Override
        public void enterDirectory(DirectorySnapshot directorySnapshot, boolean isRoot) {
            try {
                writeEntryType(encoder, EntryType.DIR_OPEN);
                writePath(encoder, isRoot, directorySnapshot);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        @Override
        public SnapshotVisitResult visitEntry(FileSystemLocationSnapshot snapshot, boolean isRoot) {
            snapshot.accept(new FileSystemLocationSnapshot.FileSystemLocationSnapshotVisitor() {

                @Override
                public void visitRegularFile(RegularFileSnapshot fileSnapshot) {
                    try {
                        writeEntryType(encoder, EntryType.REGULAR_FILE);
                        writePath(encoder, isRoot, fileSnapshot);
                        writeAccessType(encoder, fileSnapshot.getAccessType());
                        writeHashCode(encoder, fileSnapshot.getHash());
                        FileMetadata metadata = fileSnapshot.getMetadata();
                        encoder.writeSmallLong(metadata.getLastModified());
                        encoder.writeSmallLong(metadata.getLength());
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }

                @Override
                public void visitMissing(MissingFileSnapshot missingSnapshot) {
                    try {
                        writeEntryType(encoder, EntryType.MISSING);
                        writePath(encoder, isRoot, missingSnapshot);
                        writeAccessType(encoder, missingSnapshot.getAccessType());
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }
            });
            return SnapshotVisitResult.CONTINUE;
        }

        @Override
        public void leaveDirectory(DirectorySnapshot directorySnapshot, boolean isRoot) {
            try {
                writeEntryType(encoder, EntryType.DIR_CLOSE);
                writeAccessType(encoder, directorySnapshot.getAccessType());
                writeHashCode(encoder, directorySnapshot.getHash());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    });
    encoder.writeByte((byte) EntryType.END.ordinal());
}
Also used : RootTrackingFileSystemSnapshotHierarchyVisitor(org.gradle.internal.snapshot.RootTrackingFileSystemSnapshotHierarchyVisitor) SnapshotVisitResult(org.gradle.internal.snapshot.SnapshotVisitResult) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) FileMetadata(org.gradle.internal.file.FileMetadata) DefaultFileMetadata(org.gradle.internal.file.impl.DefaultFileMetadata) MissingFileSnapshot(org.gradle.internal.snapshot.MissingFileSnapshot) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DirectorySnapshot(org.gradle.internal.snapshot.DirectorySnapshot)

Aggregations

RegularFileSnapshot (org.gradle.internal.snapshot.RegularFileSnapshot)9 HashCode (org.gradle.internal.hash.HashCode)5 FileSystemLocationSnapshot (org.gradle.internal.snapshot.FileSystemLocationSnapshot)4 MissingFileSnapshot (org.gradle.internal.snapshot.MissingFileSnapshot)4 Nullable (javax.annotation.Nullable)3 FileMetadata (org.gradle.internal.file.FileMetadata)3 File (java.io.File)2 IOException (java.io.IOException)2 DefaultFileMetadata (org.gradle.internal.file.impl.DefaultFileMetadata)2 DirectorySnapshot (org.gradle.internal.snapshot.DirectorySnapshot)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CountingOutputStream (com.google.common.io.CountingOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 FileSystemLocationFingerprint (org.gradle.internal.fingerprint.FileSystemLocationFingerprint)1