11package com .github .dockerjava .core .util ;
22
33import static com .github .dockerjava .core .util .FilePathUtil .relativize ;
4- import static java .nio .file .FileVisitOption .FOLLOW_LINKS ;
54
65import java .io .BufferedInputStream ;
76import java .io .BufferedOutputStream ;
1312import java .io .OutputStream ;
1413import java .nio .file .Files ;
1514import java .nio .file .Path ;
16- import java .util .EnumSet ;
1715import java .util .zip .GZIPOutputStream ;
1816
1917import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
@@ -28,14 +26,26 @@ private CompressArchiveUtil() {
2826 // utility class
2927 }
3028
31- static void putTarEntry (TarArchiveOutputStream tarOutputStream , TarArchiveEntry tarEntry , Path file )
29+ static void addFileToTar (TarArchiveOutputStream tarArchiveOutputStream , Path file , String entryName )
3230 throws IOException {
33- tarEntry .setSize (Files .size (file ));
34- tarOutputStream .putArchiveEntry (tarEntry );
35- try (InputStream input = new BufferedInputStream (Files .newInputStream (file ))) {
36- ByteStreams .copy (input , tarOutputStream );
37- tarOutputStream .closeArchiveEntry ();
31+ if (Files .isSymbolicLink (file )) {
32+ TarArchiveEntry tarArchiveEntry = new TarArchiveEntry (entryName , TarArchiveEntry .LF_SYMLINK );
33+ tarArchiveEntry .setLinkName (Files .readSymbolicLink (file ).toString ());
34+ tarArchiveOutputStream .putArchiveEntry (tarArchiveEntry );
35+ } else {
36+ TarArchiveEntry tarArchiveEntry = (TarArchiveEntry ) tarArchiveOutputStream .createArchiveEntry (file .toFile (),
37+ entryName );
38+ if (file .toFile ().canExecute ()) {
39+ tarArchiveEntry .setMode (tarArchiveEntry .getMode () | 0755 );
40+ }
41+ tarArchiveOutputStream .putArchiveEntry (tarArchiveEntry );
42+ if (file .toFile ().isFile ()) {
43+ try (InputStream input = new BufferedInputStream (Files .newInputStream (file ))) {
44+ ByteStreams .copy (input , tarArchiveOutputStream );
45+ }
46+ }
3847 }
48+ tarArchiveOutputStream .closeArchiveEntry ();
3949 }
4050
4151 private static TarArchiveOutputStream buildTarStream (Path outputPath , boolean gZipped ) throws IOException {
@@ -69,19 +79,14 @@ public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean
6979
7080 try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream (outputPath , gZipped )) {
7181 if (!Files .isDirectory (inputPath )) {
72- TarArchiveEntry tarEntry = new TarArchiveEntry (inputPath .getFileName ().toString ());
73- if (inputPath .toFile ().canExecute ()) {
74- tarEntry .setMode (tarEntry .getMode () | 0755 );
75- }
76- putTarEntry (tarArchiveOutputStream , tarEntry , inputPath );
82+ addFileToTar (tarArchiveOutputStream , inputPath , inputPath .getFileName ().toString ());
7783 } else {
7884 Path sourcePath = inputPath ;
7985 if (!childrenOnly ) {
8086 // In order to have the dossier as the root entry
8187 sourcePath = inputPath .getParent ();
8288 }
83- Files .walkFileTree (inputPath , EnumSet .of (FOLLOW_LINKS ), Integer .MAX_VALUE ,
84- new TarDirWalker (sourcePath , tarArchiveOutputStream ));
89+ Files .walkFileTree (inputPath , new TarDirWalker (sourcePath , tarArchiveOutputStream ));
8590 }
8691 tarArchiveOutputStream .flush ();
8792 }
@@ -95,19 +100,10 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
95100 new FileOutputStream (tarFile ))))) {
96101 tos .setLongFileMode (TarArchiveOutputStream .LONGFILE_GNU );
97102 for (File file : files ) {
98- TarArchiveEntry tarEntry = new TarArchiveEntry (file );
99- tarEntry .setName (relativize (base , file ));
100-
101- if (!file .isDirectory () && file .canExecute ()) {
102- tarEntry .setMode (tarEntry .getMode () | 0755 );
103- }
104-
105- tos .putArchiveEntry (tarEntry );
106-
107- if (!file .isDirectory ()) {
108- FileUtils .copyFile (file , tos );
109- }
110- tos .closeArchiveEntry ();
103+ // relativize with method using Path otherwise method with File resolves the symlinks
104+ // and this is not want we want. If the file is a symlink, the relativized path should
105+ // keep the symlink name and not the target it points to.
106+ addFileToTar (tos , file .toPath (), relativize (base .toPath (), file .toPath ()));
111107 }
112108 }
113109
0 commit comments