use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.
the class JavaContainerBuilder method addDirectoryContentsToLayer.
private void addDirectoryContentsToLayer(Map<LayerType, FileEntriesLayer.Builder> layerBuilders, LayerType layerType, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
if (!layerBuilders.containsKey(layerType)) {
layerBuilders.put(layerType, FileEntriesLayer.builder());
}
FileEntriesLayer.Builder builder = layerBuilders.get(layerType);
new DirectoryWalker(sourceRoot).filterRoot().filter(path -> Files.isDirectory(path) || pathFilter.test(path)).walk(path -> {
AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
builder.addEntry(path, pathOnContainer, modificationTime);
});
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project quarkus by quarkusio.
the class ContainerBuilderHelper method extraDirectoryLayerConfiguration.
/**
* Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
*
* @param sourceDirectory the source extra directory path
* @param targetDirectory the root directory on the container to place the files in
* @param extraDirectoryPermissions map from path on container to file permissions
* @param modificationTimeProvider file modification time provider
* @return a {@link FileEntriesLayer} for adding the extra directory to the container
* @throws IOException if walking the extra directory fails
*/
public static FileEntriesLayer extraDirectoryLayerConfiguration(Path sourceDirectory, AbsoluteUnixPath targetDirectory, Map<String, FilePermissions> extraDirectoryPermissions, BiFunction<Path, AbsoluteUnixPath, Instant> modificationTimeProvider) throws IOException {
FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(JavaContainerBuilder.LayerType.EXTRA_FILES.getName());
Map<PathMatcher, FilePermissions> pathMatchers = new LinkedHashMap<>();
for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
pathMatchers.put(FileSystems.getDefault().getPathMatcher("glob:" + entry.getKey()), entry.getValue());
}
new DirectoryWalker(sourceDirectory).filterRoot().walk(localPath -> {
AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
Instant modificationTime = modificationTimeProvider.apply(localPath, pathOnContainer);
FilePermissions permissions = extraDirectoryPermissions.get(pathOnContainer.toString());
if (permissions == null) {
// Check for matching globs
Path containerPath = Paths.get(pathOnContainer.toString());
for (Map.Entry<PathMatcher, FilePermissions> entry : pathMatchers.entrySet()) {
if (entry.getKey().matches(containerPath)) {
builder.addEntry(localPath, pathOnContainer, entry.getValue(), modificationTime);
return;
}
}
// Add with default permissions
if (localPath.toFile().canExecute()) {
// make sure the file or directory can be executed
builder.addEntry(localPath, pathOnContainer, FilePermissions.fromOctalString("755"), modificationTime);
} else {
builder.addEntry(localPath, pathOnContainer, modificationTime);
}
} else {
// Add with explicit permissions
builder.addEntry(localPath, pathOnContainer, permissions, modificationTime);
}
});
return builder.build();
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.
the class TestProject method copyProject.
/**
* Copies test project {@code projectName} to {@code destination} folder.
*/
private static void copyProject(String projectName, Path destination) throws IOException, URISyntaxException {
Path projectPathInResources = Paths.get(Resources.getResource(PROJECTS_PATH_IN_RESOURCES + projectName).toURI());
new DirectoryWalker(projectPathInResources).filterRoot().walk(path -> {
// Creates the same path in the destDir.
Path destPath = destination.resolve(projectPathInResources.relativize(path));
if (Files.isDirectory(path)) {
Files.createDirectory(destPath);
} else {
Files.copy(path, destPath);
}
});
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class CacheReaderTest method testGetUpToDateLayerBySourceFiles.
@Test
public void testGetUpToDateLayerBySourceFiles() throws URISyntaxException, IOException, CacheMetadataCorruptedException {
// The two last modified times to use. Must be in thousands as most file time granularity is in
// seconds.
FileTime olderLastModifiedTime = FileTime.fromMillis(1000);
FileTime newerLastModifiedTime = FileTime.fromMillis(2000);
// Copies test files to a modifiable temporary folder.
Path resourceSourceFiles = Paths.get(Resources.getResource("layer").toURI());
Path testSourceFiles = temporaryFolder.newFolder().toPath();
copyDirectory(resourceSourceFiles, testSourceFiles);
// Walk the files in reverse order so that the subfiles are changed before the parent
// directories are.
List<Path> paths = new DirectoryWalker(testSourceFiles).walk();
paths.sort(Comparator.reverseOrder());
for (Path path : paths) {
Files.setLastModifiedTime(path, olderLastModifiedTime);
}
// Sets the metadata source file to the new temporary folder.
CachedLayerWithMetadata classesCachedLayer;
try (Cache cache = Cache.init(testCacheFolder)) {
ImageLayers<CachedLayerWithMetadata> cachedLayers = cache.getMetadata().filterLayers().filter();
Assert.assertEquals(3, cachedLayers.size());
classesCachedLayer = cachedLayers.get(2);
classesCachedLayer.getMetadata().setSourceFiles(Collections.singletonList(testSourceFiles.toString()));
}
try (Cache cache = Cache.init(testCacheFolder)) {
CacheReader cacheReader = new CacheReader(cache);
Assert.assertEquals(classesCachedLayer.getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(testSourceFiles)).getBlobDescriptor());
// Changes a file and checks that the change is detected.
Files.setLastModifiedTime(testSourceFiles.resolve("a").resolve("b").resolve("bar"), newerLastModifiedTime);
Assert.assertNull(cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(testSourceFiles)));
// Any non-cached directory should be deemed modified.
Assert.assertNull(cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(resourceSourceFiles)));
}
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class CacheReaderTest method copyDirectory.
// TODO: Replace with filesystem.DirectoryWalker.
private static void copyDirectory(Path source, Path destination) throws IOException {
new DirectoryWalker(source).filter(path -> !path.equals(source)).walk(path -> {
Path newPath = destination.resolve(source.relativize(path));
Files.copy(path, newPath);
});
}
Aggregations