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);
}
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));
}
}
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();
}
}
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();
}
}
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());
}
Aggregations