1414import java .nio .file .attribute .BasicFileAttributes ;
1515import java .util .ArrayList ;
1616import java .util .Collections ;
17+ import java .util .Comparator ;
1718import java .util .List ;
1819import java .util .zip .GZIPOutputStream ;
1920
@@ -32,7 +33,7 @@ public final class LayerBuilder {
3233 private static final int DIRECTORY_MODE = 0755 ;
3334 private static final int BUFFER_BYTES = 64 * 1024 ;
3435
35- public static Layer fromDirectory (Path source , String pathInImage , Path outputFile ) throws IOException {
36+ public static Layer fromDirectory (final Path source , String pathInImage , Path outputFile ) throws IOException {
3637 final List <Path > files = new ArrayList <Path >();
3738 Files .walkFileTree (source , new SimpleFileVisitor <Path >() {
3839 @ Override
@@ -41,7 +42,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
4142 return FileVisitResult .CONTINUE ;
4243 }
4344 });
44- Collections .sort (files );
45+ Collections .sort (files , new Comparator <Path >() {
46+ @ Override
47+ public int compare (Path a , Path b ) {
48+ return archiveName (source , a ).compareTo (archiveName (source , b ));
49+ }
50+ });
4551
4652 String prefix = pathInImage .startsWith ("/" ) ? pathInImage .substring (1 ) : pathInImage ;
4753 if (!prefix .endsWith ("/" )) {
@@ -60,7 +66,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
6066 tar .closeArchiveEntry ();
6167 }
6268 for (Path file : files ) {
63- String name = prefix + source . relativize ( file ). toString (). replace ( '\\' , '/' );
69+ String name = prefix + archiveName ( source , file );
6470 long size = Files .size (file );
6571 TarArchiveEntry entry = new TarArchiveEntry (name );
6672 pin (entry , isUnderBin (name ) ? EXECUTABLE_MODE : REGULAR_MODE , size );
@@ -83,14 +89,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
8389 * would be non-executable exactly on the machine this project is developed on.
8490 */
8591 static boolean isUnderBin (String nameInArchive ) {
86- return nameInArchive .contains ("/bin/" );
92+ return nameInArchive .contains ("/bin/" ) || nameInArchive . startsWith ( "bin/" ) ;
8793 }
8894
8995 static List <String > directoriesFor (String prefix , Path source , List <Path > files ) {
9096 List <String > directories = new ArrayList <String >();
9197 java .util .Set <String > seen = new java .util .TreeSet <String >();
9298 for (Path file : files ) {
93- String relative = source . relativize ( file ). toString (). replace ( '\\' , '/' );
99+ String relative = archiveName ( source , file );
94100 int slash = relative .indexOf ('/' );
95101 while (slash >= 0 ) {
96102 seen .add (prefix + relative .substring (0 , slash ) + "/" );
@@ -107,12 +113,23 @@ static List<String> directoriesFor(String prefix, Path source, List<Path> files)
107113 return directories ;
108114 }
109115
116+ /**
117+ * @implNote {@code Path} natural ordering is case-insensitive on Windows and case-sensitive on
118+ * Linux, so sorting by {@code Path} directly would order two names differing only by case
119+ * differently depending on the building platform, changing the digest with it. This is the
120+ * string that is actually written to the archive, so sorting by it is platform-independent.
121+ */
122+ static String archiveName (Path source , Path file ) {
123+ return source .relativize (file ).toString ().replace ('\\' , '/' );
124+ }
125+
110126 /**
111127 * @implNote the uid, gid and owner name calls are redundant with commons-compress 1.24.0's own
112- * defaults for the {@code TarArchiveEntry(String)} constructor used above, and are kept
113- * deliberately: the library sources {@code userName} from {@code System.getProperty("user.name")}
114- * on another of its constructor paths, so a future constructor change here would otherwise leak
115- * the building machine's identity into the layer and silently break cross-machine determinism.
128+ * defaults for the {@code TarArchiveEntry(String)} constructor used above: it reads
129+ * {@code userName} from {@code System.getProperty("user.name")} during construction and resets
130+ * it to {@code ""} immediately afterward, in that same overload's own body. The calls stay to
131+ * guard against a future change to which constructor is used, which would otherwise leak the
132+ * building machine's identity into the layer and silently break cross-machine determinism.
116133 */
117134 private static void pin (TarArchiveEntry entry , int mode , long size ) {
118135 entry .setMode ((entry .isDirectory () ? TarArchiveEntry .DEFAULT_DIR_MODE : 0 ) | mode );
0 commit comments