From 40c49fefa3367077edc004c351b4fff6a5f55d2b Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Tue, 28 Mar 2023 10:20:58 -0400 Subject: [PATCH] fix #4910: simplifying the optional check for copyDir --- .../internal/core/v1/PodOperationsImpl.java | 97 ++++++++----------- 1 file changed, 43 insertions(+), 54 deletions(-) diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java index a0866c186bf..36dd08cf76a 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java @@ -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 @@ -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); } }