|
16 | 16 |
|
17 | 17 | package com.google.cloud.tools.jib.cache; |
18 | 18 |
|
| 19 | +import com.google.cloud.tools.jib.image.DescriptorDigest; |
19 | 20 | import com.google.cloud.tools.jib.image.ImageLayers; |
| 21 | +import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.UncheckedIOException; |
| 24 | +import java.nio.file.Files; |
20 | 25 | import java.nio.file.Path; |
21 | 26 | import java.nio.file.attribute.FileTime; |
22 | 27 | import java.time.Instant; |
23 | 28 | import java.util.List; |
| 29 | +import java.util.Optional; |
24 | 30 |
|
25 | 31 | /** Reads image content from the cache. */ |
26 | 32 | public class CacheReader { |
27 | 33 |
|
| 34 | + /** |
| 35 | + * @return the last modified time for the file at {@code path}. Recursively finds the most recent |
| 36 | + * last modified time for all subfiles if the file is a directory. |
| 37 | + */ |
| 38 | + private static FileTime getLastModifiedTime(Path path) throws IOException { |
| 39 | + FileTime lastModifiedTime = Files.getLastModifiedTime(path); |
| 40 | + |
| 41 | + if (Files.isReadable(path)) { |
| 42 | + try { |
| 43 | + Optional<FileTime> maxLastModifiedTime = |
| 44 | + Files.walk(path) |
| 45 | + .map( |
| 46 | + subFilePath -> { |
| 47 | + try { |
| 48 | + return Files.getLastModifiedTime(subFilePath); |
| 49 | + |
| 50 | + } catch (IOException ex) { |
| 51 | + throw new UncheckedIOException(ex); |
| 52 | + } |
| 53 | + }) |
| 54 | + .max(FileTime::compareTo); |
| 55 | + |
| 56 | + if (!maxLastModifiedTime.isPresent()) { |
| 57 | + throw new IllegalStateException( |
| 58 | + "Could not get last modified time for all files in directory '" + path + "'"); |
| 59 | + } |
| 60 | + if (maxLastModifiedTime.get().compareTo(lastModifiedTime) > 0) { |
| 61 | + lastModifiedTime = maxLastModifiedTime.get(); |
| 62 | + } |
| 63 | + |
| 64 | + } catch (UncheckedIOException ex) { |
| 65 | + throw ex.getCause(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return lastModifiedTime; |
| 70 | + } |
| 71 | + |
28 | 72 | private final Cache cache; |
29 | 73 |
|
30 | 74 | public CacheReader(Cache cache) { |
31 | 75 | this.cache = cache; |
32 | 76 | } |
33 | 77 |
|
| 78 | + /** @return the cached layer with digest {@code layerDigest} */ |
| 79 | + public CachedLayer getLayer(DescriptorDigest layerDigest) throws LayerPropertyNotFoundException { |
| 80 | + return cache.getMetadata().getLayers().get(layerDigest); |
| 81 | + } |
| 82 | + |
34 | 83 | /** |
35 | 84 | * Finds the file that stores the content BLOB for an application layer. |
36 | 85 | * |
@@ -66,4 +115,41 @@ public Path getLayerFile(CachedLayerType layerType, List<Path> sourceFiles) |
66 | 115 | throw new UnsupportedOperationException("Can only find layer files for application layers"); |
67 | 116 | } |
68 | 117 | } |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets an up-to-date layer that is built from the {@code sourceFiles}. |
| 121 | + * |
| 122 | + * <p>The method returns the first up-to-date layer found. This is safe because the source files |
| 123 | + * will not have been modified since creation of any up-to-date layer (ie. all up-to-date layers |
| 124 | + * should have the same file contents) |
| 125 | + */ |
| 126 | + public CachedLayer getUpToDateLayerBySourceFiles(List<Path> sourceFiles) |
| 127 | + throws IOException, CacheMetadataCorruptedException { |
| 128 | + // Grabs all the layers that have matching source files. |
| 129 | + ImageLayers<CachedLayerWithMetadata> cachedLayersWithSourceFiles = |
| 130 | + cache.getMetadata().filterLayers().bySourceFiles(sourceFiles).filter(); |
| 131 | + if (cachedLayersWithSourceFiles.isEmpty()) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + // Determines the latest modification time for the source files. |
| 136 | + FileTime sourceFilesLastModifiedTime = FileTime.from(Instant.MIN); |
| 137 | + for (Path path : sourceFiles) { |
| 138 | + FileTime lastModifiedTime = getLastModifiedTime(path); |
| 139 | + if (lastModifiedTime.compareTo(sourceFilesLastModifiedTime) > 0) { |
| 140 | + sourceFilesLastModifiedTime = lastModifiedTime; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Checks if at least one of the matched layers is up-to-date. |
| 145 | + for (CachedLayerWithMetadata cachedLayer : cachedLayersWithSourceFiles) { |
| 146 | + if (sourceFilesLastModifiedTime.compareTo(cachedLayer.getMetadata().getLastModifiedTime()) |
| 147 | + <= 0) { |
| 148 | + // This layer is an up-to-date layer. |
| 149 | + return cachedLayer; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return null; |
| 154 | + } |
69 | 155 | } |
0 commit comments