Skip to content

Commit a8512d6

Browse files
committed
HADOOP-18136. Verify FileUtils.unTar() handling of missing .tar files.
Contributed by Steve Loughran Change-Id: Id6f3f2b0eeeb186a757d8b6ee5a0584cb4114469
1 parent 5066722 commit a8512d6

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,10 +886,13 @@ private static void unTarUsingTar(InputStream inputStream, File untarDir,
886886
private static void unTarUsingTar(File inFile, File untarDir,
887887
boolean gzipped) throws IOException {
888888
StringBuffer untarCommand = new StringBuffer();
889+
// not using canonical path here; this postpones relative path
890+
// resolution until bash is executed.
891+
final String source = "'" + FileUtil.makeSecureShellPath(inFile) + "'";
889892
if (gzipped) {
890-
untarCommand.append(" gzip -dc '");
891-
untarCommand.append(FileUtil.makeSecureShellPath(inFile));
892-
untarCommand.append("' | (");
893+
untarCommand.append(" gzip -dc ")
894+
.append(source)
895+
.append(" | (");
893896
}
894897
untarCommand.append("cd '");
895898
untarCommand.append(FileUtil.makeSecureShellPath(untarDir));
@@ -899,15 +902,17 @@ private static void unTarUsingTar(File inFile, File untarDir,
899902
if (gzipped) {
900903
untarCommand.append(" -)");
901904
} else {
902-
untarCommand.append(FileUtil.makeSecureShellPath(inFile));
905+
untarCommand.append(source);
903906
}
907+
LOG.debug("executing [{}]", untarCommand);
904908
String[] shellCmd = { "bash", "-c", untarCommand.toString() };
905909
ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
906910
shexec.execute();
907911
int exitcode = shexec.getExitCode();
908912
if (exitcode != 0) {
909913
throw new IOException("Error untarring file " + inFile +
910-
". Tar process exited with exit code " + exitcode);
914+
". Tar process exited with exit code " + exitcode
915+
+ " from command " + untarCommand);
911916
}
912917
}
913918

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.fs;
1919

20+
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
2021
import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows;
2122
import static org.junit.Assert.assertArrayEquals;
2223
import static org.junit.Assert.assertEquals;
@@ -1164,6 +1165,38 @@ public void testUntar() throws IOException {
11641165
doUntarAndVerify(new File(tarFileName), untarDir);
11651166
}
11661167

1168+
/**
1169+
* Verify we can't unTar a file which isn't there.
1170+
* This will test different codepaths on Windows from unix,
1171+
* but both MUST throw an IOE of some kind.
1172+
*/
1173+
@Test(timeout = 30000)
1174+
public void testUntarMissingFile() throws Throwable {
1175+
File dataDir = GenericTestUtils.getTestDir();
1176+
File tarFile = new File(dataDir, "missing; true");
1177+
File untarDir = new File(dataDir, "untarDir");
1178+
intercept(IOException.class, () ->
1179+
FileUtil.unTar(tarFile, untarDir));
1180+
}
1181+
1182+
/**
1183+
* Verify we can't unTar a file which isn't there
1184+
* through the java untar code.
1185+
* This is how {@code FileUtil.unTar(File, File}
1186+
* will behave on Windows,
1187+
*/
1188+
@Test(timeout = 30000)
1189+
public void testUntarMissingFileThroughJava() throws Throwable {
1190+
File dataDir = GenericTestUtils.getTestDir();
1191+
File tarFile = new File(dataDir, "missing; true");
1192+
File untarDir = new File(dataDir, "untarDir");
1193+
// java8 on unix throws java.nio.file.NoSuchFileException here;
1194+
// leaving as an IOE intercept in case windows throws something
1195+
// else.
1196+
intercept(IOException.class, () ->
1197+
FileUtil.unTarUsingJava(tarFile, untarDir, false));
1198+
}
1199+
11671200
@Test (timeout = 30000)
11681201
public void testCreateJarWithClassPath() throws Exception {
11691202
// setup test directory for files

0 commit comments

Comments
 (0)