Skip to content

Commit

Permalink
fix: don't copy classpath files more than once (quarkusio#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse authored Mar 13, 2022
1 parent b44d783 commit 6337f4e
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import dev.jbang.cli.BaseCommand;
import dev.jbang.cli.ExitException;
Expand Down Expand Up @@ -57,14 +56,18 @@ private static ResourceRef getClasspathResource(String cpResource) {
}

// We couldn't read the file directly from the class path so let's make a copy
try (InputStream is = url.openStream()) {
Path to = Util.getUrlCache(cpResource);
Files.createDirectories(to.getParent());
Files.copy(is, to, StandardCopyOption.REPLACE_EXISTING);
return ResourceRef.forCachedResource(cpResource, to.toFile());
} catch (IOException e) {
throw new ExitException(BaseCommand.EXIT_GENERIC_ERROR,
"Resource could not be copied from class path: " + ref, e);
Path to = Util.getUrlCache(cpResource);
if (!Files.exists(to)) {
try (InputStream is = url.openStream()) {
Files.createDirectories(to.getParent());
Files.copy(is, to);
} catch (IOException e) {
Util.deletePath(to, true);
throw new ExitException(BaseCommand.EXIT_GENERIC_ERROR,
"Resource could not be copied from class path: " + ref, e);
}
}

return ResourceRef.forCachedResource(cpResource, to.toFile());
}
}

0 comments on commit 6337f4e

Please sign in to comment.