Skip to content

Commit

Permalink
fix fabric8io#4910: simplifying the optional check for copyDir
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 28, 2023
1 parent 57d8d70 commit 40c49fe
Showing 1 changed file with 43 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,20 @@ public PodOperationsImpl dir(String dir) {

@Override
public boolean copy(Path destination) {
try {
if (Utils.isNotNullOrEmpty(getContext().getFile())) {
copyFile(getContext().getFile(), destination.toFile());
return true;
} else if (Utils.isNotNullOrEmpty(getContext().getDir())) {
copyDir(getContext().getDir(), destination.toFile());
return true;
return wrapRunWithOptionalDependency(() -> {
try {
if (Utils.isNotNullOrEmpty(getContext().getFile())) {
copyFile(getContext().getFile(), destination.toFile());
return true;
} else if (Utils.isNotNullOrEmpty(getContext().getDir())) {
copyDir(getContext().getDir(), destination.toFile());
return true;
}
throw new IllegalStateException("No file or dir has been specified");
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
throw new IllegalStateException("No file or dir has been specified");
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}, "TarArchiveInputStream class is provided by commons-compress");
}

@Override
Expand Down Expand Up @@ -495,53 +497,40 @@ private InputStream read(String... command) {
return watch.getOutput();
}

private void copyDir(String source, File target) throws Exception {
//Let's wrap the code to a runnable inner class to avoid NoClassDef on Option classes.
try {
new Runnable() {
@Override
public void run() {
File destination = target;
if (!destination.isDirectory() && !destination.mkdirs())

{
throw KubernetesClientException.launderThrowable(new IOException("Failed to create directory: " + destination));
private void copyDir(String source, File target) {
File destination = target;
if (!destination.isDirectory() && !destination.mkdirs()) {
throw KubernetesClientException.launderThrowable(new IOException("Failed to create directory: " + destination));
}
try (
InputStream is = readTar(source);
org.apache.commons.compress.archivers.tar.TarArchiveInputStream tis = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(
is))

{
for (org.apache.commons.compress.archivers.ArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis
.getNextEntry()) {
if (tis.canReadEntryData(entry)) {
final String normalizedEntryName = FilenameUtils.normalize(entry.getName());
if (normalizedEntryName == null) {
throw new IOException("Tar entry '" + entry.getName() + "' has an invalid name");
}
try (
InputStream is = readTar(source);
org.apache.commons.compress.archivers.tar.TarArchiveInputStream tis = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(
is))

{
for (org.apache.commons.compress.archivers.ArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis
.getNextEntry()) {
if (tis.canReadEntryData(entry)) {
final String normalizedEntryName = FilenameUtils.normalize(entry.getName());
if (normalizedEntryName == null) {
throw new IOException("Tar entry '" + entry.getName() + "' has an invalid name");
}
File f = new File(destination, normalizedEntryName);
if (entry.isDirectory()) {
if (!f.isDirectory() && !f.mkdirs()) {
throw new IOException("Failed to create directory: " + f);
}
} else {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory: " + f);
}
Files.copy(tis, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
File f = new File(destination, normalizedEntryName);
if (entry.isDirectory()) {
if (!f.isDirectory() && !f.mkdirs()) {
throw new IOException("Failed to create directory: " + f);
}
} else {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("Failed to create directory: " + f);
}
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
Files.copy(tis, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}.run();
} catch (NoClassDefFoundError e) {
throw new KubernetesClientException(
"TarArchiveInputStream class is provided by commons-compress, an optional dependency. To use the read/copy functionality you must explicitly add this dependency to the classpath.");
}
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}

Expand Down

0 comments on commit 40c49fe

Please sign in to comment.