Skip to content

Commit

Permalink
4.x: improve exception messages in stager plugin (#1081)
Browse files Browse the repository at this point in the history
* Detect files that are broken links for better ex message
* Add additional exception info in DownloadTask.download
  • Loading branch information
barchetta authored Oct 29, 2024
1 parent 5980e9a commit 1dad467
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public static Path ensureDirectory(Path path, FileAttribute<?>... attrs) {
return requireDirectory(path);
} else {
try {
if (Files.isSymbolicLink(path)) {
// The File.exists check will follow symbolic links. If it returns false because
// path is a broken link, then we want to catch that here.
throw new IOException("Broken link: " + path);
}
return Files.createDirectories(path, attrs);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,33 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
static void download(StagingContext ctx, URL url, Path file)
throws IOException {

try (BufferedInputStream bis = new BufferedInputStream(open(url, ctx))) {
try (OutputStream fos = Files.newOutputStream(file)) {
int n;
long startTime = System.currentTimeMillis();
long progressTime = startTime;
long currentTime = startTime;
long totalSize = 0;
long totalTime = 1;
byte[] buffer = new byte[1024];
while ((n = bis.read(buffer, 0, buffer.length)) >= 0) {
fos.write(buffer, 0, n);
totalSize += n;
currentTime = System.currentTimeMillis();
if (currentTime - progressTime >= 1000) {
totalTime = (currentTime - startTime) / 1000;
progressTime = currentTime;
ctx.logInfo("Downloading %s to %s (%s at %s/s)",
url, file, measuredSize(totalSize), measuredSize(totalSize / totalTime));
}
}
try (BufferedInputStream bis = new BufferedInputStream(open(url, ctx));
OutputStream fos = Files.newOutputStream(file)) {
int n;
long startTime = System.currentTimeMillis();
long progressTime = startTime;
long currentTime = startTime;
long totalSize = 0;
long totalTime = 1;
byte[] buffer = new byte[1024];
while ((n = bis.read(buffer, 0, buffer.length)) >= 0) {
fos.write(buffer, 0, n);
totalSize += n;
currentTime = System.currentTimeMillis();
if (currentTime - progressTime >= 1000) {
totalTime = (currentTime - startTime) / 1000;
progressTime = currentTime;
ctx.logInfo("Downloading %s to %s (%s at %s/s)",
url, file, measuredSize(totalSize), measuredSize(totalSize / totalTime));
}
ctx.logInfo("Downloaded %s to %s (%s at %s/s)",
url, file, measuredSize(totalSize), measuredSize(totalSize / totalTime));
}
if (currentTime - progressTime >= 1000) {
totalTime = (currentTime - startTime) / 1000;
}
ctx.logInfo("Downloaded %s to %s (%s at %s/s)",
url, file, measuredSize(totalSize), measuredSize(totalSize / totalTime));
} catch (IOException ex) {
throw new IOException("Failed to download " + url + " to " + file, ex);
}
}

Expand Down

0 comments on commit 1dad467

Please sign in to comment.