Search in sources :

Example 6 with RegularFileSnapshot

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

the class FileSystemSnapshotSerializer method read.

@Override
public FileSystemSnapshot read(Decoder decoder) throws Exception {
    SnapshotStack stack = new SnapshotStack();
    stack.push();
    Deque<String> pathTracker = new ArrayDeque<>();
    while (true) {
        EntryType type = readEntryType(decoder);
        if (type == EntryType.END) {
            break;
        }
        if (type != EntryType.DIR_CLOSE) {
            String path = decoder.readString();
            String internedPath = stringInterner.intern(path);
            pathTracker.addLast(internedPath);
            if (type == EntryType.DIR_OPEN) {
                stack.push();
                continue;
            }
        }
        String internedAbsolutePath;
        String internedName;
        String path = pathTracker.removeLast();
        if (pathTracker.isEmpty()) {
            internedAbsolutePath = path;
            internedName = stringInterner.intern(PathUtil.getFileName(internedAbsolutePath));
        } else {
            internedAbsolutePath = stringInterner.intern(toAbsolutePath(pathTracker, path));
            internedName = path;
        }
        FileMetadata.AccessType accessType = readAccessType(decoder);
        switch(type) {
            case REGULAR_FILE:
                HashCode contentHash = readHashCode(decoder);
                long lastModified = decoder.readSmallLong();
                long length = decoder.readSmallLong();
                stack.add(new RegularFileSnapshot(internedAbsolutePath, internedName, contentHash, DefaultFileMetadata.file(lastModified, length, accessType)));
                break;
            case MISSING:
                stack.add(new MissingFileSnapshot(internedAbsolutePath, internedName, accessType));
                break;
            case DIR_CLOSE:
                HashCode merkleHash = readHashCode(decoder);
                List<FileSystemLocationSnapshot> children = stack.pop();
                stack.add(new DirectorySnapshot(internedAbsolutePath, internedName, accessType, merkleHash, children));
                break;
            default:
                throw new AssertionError();
        }
    }
    return CompositeFileSystemSnapshot.of(stack.pop());
}
Also used : FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) FileMetadata(org.gradle.internal.file.FileMetadata) DefaultFileMetadata(org.gradle.internal.file.impl.DefaultFileMetadata) ArrayDeque(java.util.ArrayDeque) DirectorySnapshot(org.gradle.internal.snapshot.DirectorySnapshot) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) MissingFileSnapshot(org.gradle.internal.snapshot.MissingFileSnapshot)

Example 7 with RegularFileSnapshot

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

the class IgnoredPathFingerprintingStrategy method collectFingerprints.

@Override
public Map<String, FileSystemLocationFingerprint> collectFingerprints(FileSystemSnapshot roots) {
    ImmutableMap.Builder<String, FileSystemLocationFingerprint> builder = ImmutableMap.builder();
    HashSet<String> processedEntries = new HashSet<>();
    roots.accept(snapshot -> {
        snapshot.accept(new FileSystemLocationSnapshotVisitor() {

            @Override
            public void visitRegularFile(RegularFileSnapshot fileSnapshot) {
                visitNonDirectoryEntry(snapshot);
            }

            @Override
            public void visitMissing(MissingFileSnapshot missingSnapshot) {
                visitNonDirectoryEntry(snapshot);
            }

            private void visitNonDirectoryEntry(FileSystemLocationSnapshot snapshot) {
                String absolutePath = snapshot.getAbsolutePath();
                if (processedEntries.add(absolutePath)) {
                    HashCode normalizedContentHash = getNormalizedContentHash(snapshot, normalizedContentHasher);
                    if (normalizedContentHash != null) {
                        builder.put(absolutePath, IgnoredPathFileSystemLocationFingerprint.create(snapshot.getType(), normalizedContentHash));
                    }
                }
            }
        });
        return SnapshotVisitResult.CONTINUE;
    });
    return builder.build();
}
Also used : FileSystemLocationSnapshotVisitor(org.gradle.internal.snapshot.FileSystemLocationSnapshot.FileSystemLocationSnapshotVisitor) FileSystemLocationFingerprint(org.gradle.internal.fingerprint.FileSystemLocationFingerprint) HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) MissingFileSnapshot(org.gradle.internal.snapshot.MissingFileSnapshot) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Example 8 with RegularFileSnapshot

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

the class FileSystemSnapshotBuilder method addFile.

public void addFile(File file, String[] segments, String fileName, FileMetadata metadata) {
    checkNoRootFileSnapshot("another root file", file);
    HashCode contentHash = fileHasher.hash(file, metadata.getLength(), metadata.getLastModified());
    RegularFileSnapshot fileSnapshot = new RegularFileSnapshot(stringInterner.intern(file.getAbsolutePath()), fileName, contentHash, metadata);
    if (segments.length == 0) {
        rootFileSnapshot = fileSnapshot;
    } else {
        DirectoryBuilder rootDir = getOrCreateRootDir(file, segments);
        rootDir.addFile(segments, 0, fileSnapshot);
    }
}
Also used : HashCode(org.gradle.internal.hash.HashCode) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot)

Example 9 with RegularFileSnapshot

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

the class TarBuildCacheEntryPacker method unpackDirectoryTree.

@Nullable
private TarArchiveEntry unpackDirectoryTree(TarArchiveInputStream input, TarArchiveEntry rootEntry, Map<String, FileSystemLocationSnapshot> snapshots, AtomicLong entries, File treeRoot, String treeName) throws IOException {
    RelativePathParser parser = new RelativePathParser(rootEntry.getName());
    DirectorySnapshotBuilder builder = MerkleDirectorySnapshotBuilder.noSortingRequired();
    builder.enterDirectory(DIRECT, stringInterner.intern(treeRoot.getAbsolutePath()), stringInterner.intern(treeRoot.getName()), INCLUDE_EMPTY_DIRS);
    TarArchiveEntry entry;
    while ((entry = input.getNextTarEntry()) != null) {
        boolean isDir = entry.isDirectory();
        boolean outsideOfRoot = parser.nextPath(entry.getName(), isDir, builder::leaveDirectory);
        if (outsideOfRoot) {
            break;
        }
        entries.incrementAndGet();
        File file = new File(treeRoot, parser.getRelativePath());
        if (isDir) {
            FileUtils.forceMkdir(file);
            chmodUnpackedFile(entry, file);
            String internedAbsolutePath = stringInterner.intern(file.getAbsolutePath());
            String internedName = stringInterner.intern(parser.getName());
            builder.enterDirectory(DIRECT, internedAbsolutePath, internedName, INCLUDE_EMPTY_DIRS);
        } else {
            RegularFileSnapshot fileSnapshot = unpackFile(input, entry, file, parser.getName());
            builder.visitLeafElement(fileSnapshot);
        }
    }
    parser.exitToRoot(builder::leaveDirectory);
    builder.leaveDirectory();
    snapshots.put(treeName, builder.getResult());
    return entry;
}
Also used : DirectorySnapshotBuilder(org.gradle.internal.snapshot.DirectorySnapshotBuilder) MerkleDirectorySnapshotBuilder(org.gradle.internal.snapshot.MerkleDirectorySnapshotBuilder) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Nullable(javax.annotation.Nullable)

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