Skip to content

Commit

Permalink
fix (jkube-kit) : lastModified timestamps should be preserved while p…
Browse files Browse the repository at this point in the history
…reparing assembly

We don't seem to be preserving lastModified timestamps of files that are
being copied while preparing the assembly for image. Make sure we're
copying all the metadata of files/directories as well while copying
them.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Sep 29, 2023
1 parent d62c0c5 commit 3a26d30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
Expand Down Expand Up @@ -201,7 +202,7 @@ public static void copy(File sourceFile, File targetFile) throws IOException {
}

public static void copy(Path sourcePath, Path targetPath) throws IOException {
Files.copy(sourcePath, targetPath, REPLACE_EXISTING);
Files.copy(sourcePath, targetPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
}

public static void copyDirectoryIfNotExists(File sourceDir, File targetDir) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -151,6 +154,23 @@ void testGetRelativePath() throws IOException {
assertThat(relativeFile.getPath()).isEqualTo("foo" + File.separator + "fileInFoo1");
}

@Test
void copy_whenFileCopied_shouldPreserveLastModifiedTimestamp() throws IOException {
// Given
File sourceFile = new File(folder, "source");
Files.write(sourceFile.toPath(), "testdata".getBytes(StandardCharsets.UTF_8));
long originalTimestamp = new Date().getTime() - 10;
assertThat(sourceFile.setLastModified(originalTimestamp)).isTrue();
Path targetFilePath = folder.toPath().resolve("target");

// When
FileUtil.copy(sourceFile.toPath(), targetFilePath);

// Then
assertThat(targetFilePath.toFile()).hasSameTextualContentAs(sourceFile);
assertThat(targetFilePath.toFile().lastModified()).isEqualTo(originalTimestamp);
}

private void prepareDirectory() throws IOException {
final File dir1 = Files.createDirectories(folder.toPath().resolve("foo")).toFile();
assertThat(new File(dir1, "fileInFoo1").createNewFile()).isTrue();
Expand Down

0 comments on commit 3a26d30

Please sign in to comment.