Skip to content

Commit

Permalink
HADOOP-18136. Verify FileUtils.unTar() handling of missing .tar files.
Browse files Browse the repository at this point in the history
Contributed by Steve Loughran

Change-Id: I73af19d2e2e41f4ba686c470726a80c3903a1950
  • Loading branch information
steveloughran committed Feb 21, 2022
1 parent 007c201 commit cae749b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,13 @@ private static void unTarUsingTar(InputStream inputStream, File untarDir,
private static void unTarUsingTar(File inFile, File untarDir,
boolean gzipped) throws IOException {
StringBuffer untarCommand = new StringBuffer();
// not using canonical path here; this postpones relative path
// resolution until bash is executed.
final String source = "'" + FileUtil.makeSecureShellPath(inFile) + "'";
if (gzipped) {
untarCommand.append(" gzip -dc '")
.append(FileUtil.makeSecureShellPath(inFile))
.append("' | (");
untarCommand.append(" gzip -dc ")
.append(source)
.append(" | (");
}
untarCommand.append("cd '")
.append(FileUtil.makeSecureShellPath(untarDir))
Expand All @@ -901,15 +904,17 @@ private static void unTarUsingTar(File inFile, File untarDir,
if (gzipped) {
untarCommand.append(" -)");
} else {
untarCommand.append(FileUtil.makeSecureShellPath(inFile));
untarCommand.append(source);
}
LOG.debug("executing [{}]", untarCommand);
String[] shellCmd = { "bash", "-c", untarCommand.toString() };
ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
shexec.execute();
int exitcode = shexec.getExitCode();
if (exitcode != 0) {
throw new IOException("Error untarring file " + inFile +
". Tar process exited with exit code " + exitcode);
". Tar process exited with exit code " + exitcode
+ " from command " + untarCommand);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.fs;

import static org.apache.hadoop.test.LambdaTestUtils.intercept;
import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -1135,6 +1136,38 @@ public void testUntar() throws IOException {
doUntarAndVerify(new File(tarFileName), untarDir);
}

/**
* Verify we can't unTar a file which isn't there.
* This will test different codepaths on Windows from unix,
* but both MUST throw an IOE of some kind.
*/
@Test(timeout = 30000)
public void testUntarMissingFile() throws Throwable {
File dataDir = GenericTestUtils.getTestDir();
File tarFile = new File(dataDir, "missing; true");
File untarDir = new File(dataDir, "untarDir");
intercept(IOException.class, () ->
FileUtil.unTar(tarFile, untarDir));
}

/**
* Verify we can't unTar a file which isn't there
* through the java untar code.
* This is how {@code FileUtil.unTar(File, File}
* will behave on Windows,
*/
@Test(timeout = 30000)
public void testUntarMissingFileThroughJava() throws Throwable {
File dataDir = GenericTestUtils.getTestDir();
File tarFile = new File(dataDir, "missing; true");
File untarDir = new File(dataDir, "untarDir");
// java8 on unix throws java.nio.file.NoSuchFileException here;
// leaving as an IOE intercept in case windows throws something
// else.
intercept(IOException.class, () ->
FileUtil.unTarUsingJava(tarFile, untarDir, false));
}

@Test (timeout = 30000)
public void testCreateJarWithClassPath() throws Exception {
// create files expected to match a wildcard
Expand Down

0 comments on commit cae749b

Please sign in to comment.