use of org.apache.commons.io.input.CloseShieldInputStream in project gradle by gradle.
the class TarBuildCacheEntryPacker method unpack.
private UnpackResult unpack(CacheableEntity entity, TarArchiveInputStream tarInput, OriginReader readOriginAction) throws IOException {
ImmutableMap.Builder<String, CacheableTree> treesBuilder = ImmutableMap.builder();
entity.visitOutputTrees((name, type, root) -> treesBuilder.put(name, new CacheableTree(type, root)));
ImmutableMap<String, CacheableTree> treesByName = treesBuilder.build();
TarArchiveEntry tarEntry;
OriginMetadata originMetadata = null;
Map<String, FileSystemLocationSnapshot> snapshots = new HashMap<>();
tarEntry = tarInput.getNextTarEntry();
AtomicLong entries = new AtomicLong();
while (tarEntry != null) {
entries.incrementAndGet();
String path = tarEntry.getName();
if (path.equals(METADATA_PATH)) {
// handle origin metadata
originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
tarEntry = tarInput.getNextTarEntry();
} else {
// handle tree
Matcher matcher = TREE_PATH.matcher(path);
if (!matcher.matches()) {
throw new IllegalStateException("Cached entry format error, invalid contents: " + path);
}
String treeName = unescape(matcher.group(2));
CacheableTree tree = treesByName.get(treeName);
if (tree == null) {
throw new IllegalStateException(String.format("No tree '%s' registered", treeName));
}
boolean missing = matcher.group(1) != null;
String childPath = matcher.group(3);
tarEntry = unpackTree(treeName, tree.getType(), tree.getRoot(), tarInput, tarEntry, childPath, missing, snapshots, entries);
}
}
if (originMetadata == null) {
throw new IllegalStateException("Cached result format error, no origin metadata was found.");
}
return new UnpackResult(originMetadata, entries.get(), snapshots);
}
Aggregations