From c0bc52f53e41f0e25c090e555d4a4b6627c42eb2 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Fri, 29 Jan 2016 12:51:08 +0100 Subject: [PATCH 1/3] Remove unused dependency --- build.sbt | 3 --- 1 file changed, 3 deletions(-) diff --git a/build.sbt b/build.sbt index c3778ad4b..e8184b8ed 100644 --- a/build.sbt +++ b/build.sbt @@ -205,9 +205,6 @@ lazy val cli = project else Seq("com.github.alexarchambault" %% "case-app" % "1.0.0-M2") }, - libraryDependencies ++= Seq( - "ch.qos.logback" % "logback-classic" % "1.1.3" - ), resourceGenerators in Compile += packageBin.in(bootstrap).in(Compile).map { jar => Seq(jar) }.taskValue From 71878ce3e983549730a894fc698006153e639f72 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Fri, 29 Jan 2016 12:51:08 +0100 Subject: [PATCH 2/3] Slightly better output from boostrap JARs --- .../src/main/java/coursier/Bootstrap.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/bootstrap/src/main/java/coursier/Bootstrap.java b/bootstrap/src/main/java/coursier/Bootstrap.java index a0494fa00..e531768d6 100644 --- a/bootstrap/src/main/java/coursier/Bootstrap.java +++ b/bootstrap/src/main/java/coursier/Bootstrap.java @@ -131,7 +131,7 @@ public class Bootstrap { return new File(jarDir, fileName); } - static List getLocalURLs(List urls, final File jarDir, String bootstrapProtocol) { + static List getLocalURLs(List urls, final File jarDir, String bootstrapProtocol) throws MalformedURLException { ThreadFactory threadFactory = new ThreadFactory() { // from scalaz Strategy.DefaultDaemonThreadFactory @@ -149,6 +149,7 @@ public class Bootstrap { new ExecutorCompletionService<>(pool); List localURLs = new ArrayList<>(); + List missingURLs = new ArrayList<>(); for (URL url : urls) { @@ -157,40 +158,51 @@ public class Bootstrap { if (protocol.equals("file") || protocol.equals(bootstrapProtocol)) { localURLs.add(url); } else { - final URL url0 = url; + File dest = localFile(jarDir, url); - completionService.submit(new Callable() { - @Override - public URL call() throws Exception { - File dest = localFile(jarDir, url0); - - if (!dest.exists()) { - System.err.println("Downloading " + url0); - try { - URLConnection conn = url0.openConnection(); - long lastModified = conn.getLastModified(); - InputStream s = conn.getInputStream(); - byte[] b = readFullySync(s); - Files.write(dest.toPath(), b); - dest.setLastModified(lastModified); - } catch (Exception e) { - System.err.println("Error while downloading " + url0 + ": " + e.getMessage() + ", ignoring it"); - throw e; - } - } - - return dest.toURI().toURL(); - } - }); + if (dest.exists()) { + localURLs.add(dest.toURI().toURL()); + } else { + missingURLs.add(url); + } } } + for (final URL url : missingURLs) { + completionService.submit(new Callable() { + @Override + public URL call() throws Exception { + File dest = localFile(jarDir, url); + + if (!dest.exists()) { + try { + URLConnection conn = url.openConnection(); + long lastModified = conn.getLastModified(); + InputStream s = conn.getInputStream(); + byte[] b = readFullySync(s); + Files.write(dest.toPath(), b); + dest.setLastModified(lastModified); + } catch (Exception e) { + System.err.println("Error while downloading " + url + ": " + e.getMessage() + ", ignoring it"); + throw e; + } + } + + return dest.toURI().toURL(); + } + }); + } + try { while (localURLs.size() < urls.size()) { Future future = completionService.take(); try { URL url = future.get(); localURLs.add(url); + int nowMissing = urls.size() - localURLs.size(); + String clearLine = "\033[2K"; + String up = "\033[1A"; + System.err.print(clearLine + "Downloaded " + (missingURLs.size() - nowMissing) + " missing file(s) / " + missingURLs.size() + "\n" + up); } catch (ExecutionException ex) { // Error message already printed from the Callable above System.exit(255); From 75e53790bc6c493bcbe23834ca29645a7ccecdbd Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Fri, 29 Jan 2016 12:51:09 +0100 Subject: [PATCH 3/3] Safer downloading from bootstraps --- bootstrap/src/main/java/coursier/Bootstrap.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bootstrap/src/main/java/coursier/Bootstrap.java b/bootstrap/src/main/java/coursier/Bootstrap.java index e531768d6..220ed7284 100644 --- a/bootstrap/src/main/java/coursier/Bootstrap.java +++ b/bootstrap/src/main/java/coursier/Bootstrap.java @@ -5,6 +5,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.*; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.security.CodeSource; import java.security.ProtectionDomain; import java.util.*; @@ -168,6 +169,7 @@ public class Bootstrap { } } + final Random random = new Random(); for (final URL url : missingURLs) { completionService.submit(new Callable() { @Override @@ -180,7 +182,10 @@ public class Bootstrap { long lastModified = conn.getLastModified(); InputStream s = conn.getInputStream(); byte[] b = readFullySync(s); - Files.write(dest.toPath(), b); + File tmpDest = new File(dest.getParentFile(), dest.getName() + "-" + random.nextInt()); + tmpDest.deleteOnExit(); + Files.write(tmpDest.toPath(), b); + Files.move(tmpDest.toPath(), dest.toPath(), StandardCopyOption.ATOMIC_MOVE); dest.setLastModified(lastModified); } catch (Exception e) { System.err.println("Error while downloading " + url + ": " + e.getMessage() + ", ignoring it");