Skip to content

Commit 55d19ca

Browse files
committed
Merge pull request #47 from gesellix-docker/compress-archive-executable
let CompressArchiveUtil preserve executable flags
2 parents 3bc63a6 + 57b7932 commit 55d19ca

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public static File archiveTARFiles(File base, Iterable<File> files, String archi
1717
TarArchiveEntry tarEntry = new TarArchiveEntry(file);
1818
tarEntry.setName(relativize(base, file));
1919

20+
if (!file.isDirectory()) {
21+
if (file.canExecute()) {
22+
tarEntry.setMode(tarEntry.getMode() | 0755);
23+
}
24+
}
25+
2026
tos.putArchiveEntry(tarEntry);
2127

2228
if (!file.isDirectory()) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.dockerjava.core;
2+
3+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
4+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
5+
import org.apache.commons.io.FileUtils;
6+
import org.apache.commons.io.IOUtils;
7+
import org.testng.annotations.Test;
8+
9+
import java.io.File;
10+
import java.io.FileInputStream;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
14+
import static java.util.Arrays.asList;
15+
import static org.hamcrest.CoreMatchers.equalTo;
16+
import static org.hamcrest.CoreMatchers.is;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
19+
public class CompressArchiveUtilTest {
20+
21+
@Test
22+
public void testExecutableFlagIsPreserved() throws Exception {
23+
File executableFile = createExecutableFile();
24+
File archive = CompressArchiveUtil.archiveTARFiles(executableFile.getParentFile(), asList(executableFile), "archive");
25+
File expectedFile = extractFileByName(archive, "executableFile.sh.result");
26+
27+
assertThat("should be executable", expectedFile.canExecute());
28+
}
29+
30+
private File createExecutableFile() throws IOException {
31+
File baseDir = new File(FileUtils.getTempDirectoryPath());
32+
File executableFile = new File(baseDir, "executableFile.sh");
33+
executableFile.createNewFile();
34+
executableFile.setExecutable(true);
35+
assertThat(executableFile.canExecute(), is(true));
36+
return executableFile;
37+
}
38+
39+
private File extractFileByName(File archive, String filenameToExtract) throws IOException {
40+
File baseDir = new File(FileUtils.getTempDirectoryPath());
41+
File expectedFile = new File(baseDir, filenameToExtract);
42+
expectedFile.delete();
43+
assertThat(expectedFile.exists(), is(false));
44+
45+
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(archive));
46+
TarArchiveEntry entry;
47+
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
48+
String individualFiles = entry.getName();
49+
// there should be only one file in this archive
50+
assertThat(individualFiles, equalTo("executableFile.sh"));
51+
IOUtils.copy(tarArchiveInputStream, new FileOutputStream(expectedFile));
52+
if ((entry.getMode() & 0755) == 0755) {
53+
expectedFile.setExecutable(true);
54+
}
55+
}
56+
tarArchiveInputStream.close();
57+
return expectedFile;
58+
}
59+
}

0 commit comments

Comments
 (0)