use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class TarExtractor method preserveModificationTimes.
/**
* Preserve modification timestamps of files and directories in a tar file. If a directory is not
* an entry in the tar file and reproducible timestamps are enabled then its modification
* timestamp is set to a constant value. Note that the modification timestamps of symbolic links
* are not preserved even with reproducible timestamps enabled.
*
* @param destination target root for unzipping
* @param entries list of entries in tar file
* @param enableReproducibleTimestamps whether or not reproducible timestamps should be used
* @throws IOException when I/O error occurs
*/
private static void preserveModificationTimes(Path destination, List<TarArchiveEntry> entries, boolean enableReproducibleTimestamps) throws IOException {
if (enableReproducibleTimestamps) {
FileTime epochPlusOne = FileTime.fromMillis(1000L);
new DirectoryWalker(destination).filter(Files::isDirectory).walk(path -> Files.setLastModifiedTime(path, epochPlusOne));
}
for (TarArchiveEntry entry : entries) {
// the target to change
if (!entry.isSymbolicLink()) {
Files.setLastModifiedTime(destination.resolve(entry.getName()), FileTime.from(entry.getModTime().toInstant()));
}
}
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class GradleProjectProperties method getClassFiles.
@Override
public List<Path> getClassFiles() throws IOException {
// TODO: Consolidate with createJibContainerBuilder
FileCollection classesOutputDirectories = getMainSourceSet().getOutput().getClassesDirs().filter(File::exists);
List<Path> classFiles = new ArrayList<>();
for (File classesOutputDirectory : classesOutputDirectories) {
classFiles.addAll(new DirectoryWalker(classesOutputDirectory.toPath()).walk());
}
return classFiles;
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class LayerBuilderTest method testBuild.
@Test
public void testBuild() throws URISyntaxException, IOException {
Path layerDirectory = Paths.get(Resources.getResource("layer").toURI());
Path blobA = Paths.get(Resources.getResource("blobA").toURI());
String extractionPathBase = "extract/here";
LayerBuilder layerBuilder = new LayerBuilder(new ArrayList<>(Arrays.asList(layerDirectory, blobA)), extractionPathBase, false);
// Writes the layer tar to a temporary file.
UnwrittenLayer unwrittenLayer = layerBuilder.build();
Path temporaryFile = temporaryFolder.newFile().toPath();
try (OutputStream temporaryFileOutputStream = new BufferedOutputStream(Files.newOutputStream(temporaryFile))) {
unwrittenLayer.getBlob().writeTo(temporaryFileOutputStream);
}
// Reads the file back.
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(Files.newInputStream(temporaryFile))) {
// Verifies that all the files have been added to the tarball stream.
new DirectoryWalker(layerDirectory).filter(path -> !path.equals(layerDirectory)).walk(path -> {
TarArchiveEntry header = tarArchiveInputStream.getNextTarEntry();
StringBuilder expectedExtractionPath = new StringBuilder("extract/here");
for (Path pathComponent : layerDirectory.getParent().relativize(path)) {
expectedExtractionPath.append("/").append(pathComponent);
}
// Check path-equality because there might be an appended backslash in the header
// filename.
Assert.assertEquals(Paths.get(expectedExtractionPath.toString()), Paths.get(header.getName()));
// If is a normal file, checks that the file contents match.
if (Files.isRegularFile(path)) {
String expectedFileString = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
String extractedFileString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileString, extractedFileString);
}
});
// Verifies that blobA was added.
TarArchiveEntry header = tarArchiveInputStream.getNextTarEntry();
String expectedExtractionPath = "extract/here/blobA";
Assert.assertEquals(expectedExtractionPath, header.getName());
String expectedFileString = new String(Files.readAllBytes(blobA), StandardCharsets.UTF_8);
String extractedFileString = CharStreams.toString(new InputStreamReader(tarArchiveInputStream, StandardCharsets.UTF_8));
Assert.assertEquals(expectedFileString, extractedFileString);
}
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class CacheReader method getLastModifiedTime.
/**
* @return the last modified time for the file at {@code path}. Recursively finds the most recent
* last modified time for all subfiles if the file is a directory.
*/
private static FileTime getLastModifiedTime(Path path) throws IOException {
if (Files.isDirectory(path)) {
List<Path> subFiles = new DirectoryWalker(path).walk();
FileTime maxLastModifiedTime = FileTime.from(Instant.MIN);
// Finds the max last modified time for the subfiles.
for (Path subFilePath : subFiles) {
FileTime subFileLastModifiedTime = Files.getLastModifiedTime(subFilePath);
if (subFileLastModifiedTime.compareTo(maxLastModifiedTime) > 0) {
maxLastModifiedTime = subFileLastModifiedTime;
}
}
return maxLastModifiedTime;
}
return Files.getLastModifiedTime(path);
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class LayerBuilder method build.
/**
* Builds and returns the layer.
*/
public UnwrittenLayer build() throws IOException {
List<TarArchiveEntry> filesystemEntries = new ArrayList<>();
for (Path sourceFile : sourceFiles) {
if (Files.isDirectory(sourceFile)) {
new DirectoryWalker(sourceFile).filter(path -> !path.equals(sourceFile)).walk(path -> {
/*
* Builds the same file path as in the source file for extraction. The iteration
* is necessary because the path needs to be in Unix-style.
*/
StringBuilder subExtractionPath = new StringBuilder(extractionPath);
Path sourceFileRelativePath = sourceFile.getParent().relativize(path);
for (Path sourceFileRelativePathComponent : sourceFileRelativePath) {
subExtractionPath.append('/').append(sourceFileRelativePathComponent);
}
filesystemEntries.add(new TarArchiveEntry(path.toFile(), subExtractionPath.toString()));
});
} else {
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(sourceFile.toFile(), extractionPath + "/" + sourceFile.getFileName());
filesystemEntries.add(tarArchiveEntry);
}
}
if (enableReproducibleBuilds) {
makeListReproducible(filesystemEntries);
}
// Adds all the files to a tar stream.
TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
for (TarArchiveEntry entry : filesystemEntries) {
tarStreamBuilder.addEntry(entry);
}
return new UnwrittenLayer(tarStreamBuilder.toBlob());
}
Aggregations