Skip to content

Commit

Permalink
Gate usage of ensureDirectory with a lock
Browse files Browse the repository at this point in the history
- Use a single lock to ensure that concurrent execution of tasks don't race on the creation of directories
- Call Proxies.setProxyPropertiesFromEnv() in StagerMojo to support proxy from environment
  • Loading branch information
romain-grecourt committed Oct 8, 2024
1 parent 53bd7fc commit 0a1a07b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import io.helidon.build.common.Maps;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Copy an artifact to a given target location.
*/
Expand Down Expand Up @@ -56,7 +54,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
ctx.logInfo("Copying %s to %s", resolvedGav, resolveTarget);
Path artifact = ctx.resolve(resolvedGav);
Path targetFile = dir.resolve(resolveTarget);
ensureDirectory(targetFile.getParent());
ctx.ensureDirectory(targetFile.getParent());
Files.copy(artifact, targetFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.helidon.build.common.NetworkConnection;
import io.helidon.build.common.Strings;

import static io.helidon.build.common.FileUtils.ensureDirectory;
import static io.helidon.build.common.FileUtils.measuredSize;

/**
Expand Down Expand Up @@ -63,7 +62,7 @@ protected CompletableFuture<Void> execBody(StagingContext ctx, Path dir, Map<Str
protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars) throws IOException {
String path = resolveVar(target(), vars);
Path file = dir.resolve(path).normalize();
ensureDirectory(file.getParent());
ctx.ensureDirectory(file.getParent());
URL url = new URL(resolveVar(this.url, vars));
download(ctx, url, file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.List;
import java.util.Map;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Generate or copy a file to a given target location.
*/
Expand Down Expand Up @@ -70,7 +68,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
String resolvedSource = resolveVar(source, vars);
String resolvedContent = resolveVar(content, vars);
Path targetFile = dir.resolve(resolvedTarget).normalize();
ensureDirectory(targetFile.getParent());
ctx.ensureDirectory(targetFile.getParent());
if (resolvedSource != null && !resolvedSource.isEmpty()) {
Path sourceFile = ctx.resolve(resolvedSource);
if (!Files.exists(sourceFile)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import io.helidon.build.common.Proxies;
import io.helidon.build.common.Unchecked;

import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -168,6 +169,7 @@ public void execute() {
factory = new StagingElementFactory();
}

Proxies.setProxyPropertiesFromEnv();
setProxyFromSettings();
StagingTasks tasks = StagingAction.fromConfiguration(directories, factory);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ default Path createTempFile(String suffix) throws IOException {
return Files.createTempFile(null, suffix);
}

/**
* Ensure a directory exists.
*
* @param directory directory
*/
default void ensureDirectory(Path directory) {
throw new UnsupportedOperationException();
}

/**
* Log an info message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;

import io.helidon.build.common.FileUtils;

import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.UnArchiver;
Expand All @@ -46,6 +50,7 @@
*/
final class StagingContextImpl implements StagingContext {

private final Lock lock = new ReentrantLock();
private final Log log;
private final File baseDir;
private final File outputDir;
Expand Down Expand Up @@ -182,6 +187,20 @@ public Path createTempFile(String suffix) throws IOException {
return Files.createTempFile(outputDir.toPath(), null, suffix);
}

@Override
public void ensureDirectory(Path dir) {
try {
lock.lockInterruptibly();
try {
FileUtils.ensureDirectory(dir);
} finally {
lock.unlock();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public void logInfo(String msg, Object... args) {
log.info(String.format(msg, args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Generate a directory using a set of actions.
*/
Expand All @@ -39,7 +37,7 @@ public CompletionStage<Void> execute(StagingContext ctx, Path dir, Map<String, S
Path targetDir = dir.resolve(target());
ctx.logInfo("Staging %s", targetDir);
try {
ensureDirectory(targetDir);
ctx.ensureDirectory(targetDir);
} catch (Throwable ex) {
return CompletableFuture.failedFuture(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import io.helidon.build.common.Strings;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Create a symlink.
*/
Expand Down Expand Up @@ -56,7 +54,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
Path link = dir.resolve(resolveVar(target(), vars));
Path linkTarget = link.getParent().relativize(dir.resolve(resolveVar(source, vars)));
ctx.logInfo("Creating symlink source: %s, target: %s", link, linkTarget);
ensureDirectory(link.getParent());
ctx.ensureDirectory(link.getParent());
Files.createSymbolicLink(link, linkTarget);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.util.DecoratedCollection;

import static io.helidon.build.common.FileUtils.ensureDirectory;
import static java.util.stream.Collectors.toMap;

/**
Expand Down Expand Up @@ -75,7 +74,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
throw new IllegalStateException(sourceFile + " does not exist");
}
Path targetFile = dir.resolve(resolvedTarget).normalize();
ensureDirectory(targetFile.getParent());
ctx.ensureDirectory(targetFile.getParent());
try (Reader reader = Files.newBufferedReader(sourceFile);
Writer writer = Files.newBufferedWriter(targetFile,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.nio.file.Path;
import java.util.Map;

import static io.helidon.build.common.FileUtils.ensureDirectory;

/**
* Unpack an artifact to a given target location.
*/
Expand Down Expand Up @@ -74,7 +72,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
Path artifact = ctx.resolve(resolvedGav);
Path targetDir = dir.resolve(resolvedTarget).normalize();
ctx.logInfo("Unpacking %s to %s", artifact, targetDir);
ensureDirectory(targetDir);
ctx.ensureDirectory(targetDir);
ctx.unpack(artifact, targetDir, excludes, includes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import io.helidon.build.common.Strings;

import static io.helidon.build.common.FileUtils.ensureDirectory;
import static io.helidon.build.common.FileUtils.fileExt;
import static io.helidon.build.maven.stager.DownloadTask.download;

Expand Down Expand Up @@ -90,7 +89,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
String resolvedTarget = resolveVar(target(), vars);
Path targetDir = dir.resolve(resolvedTarget).normalize();
ctx.logInfo("Unpacking %s to %s", tempFile, targetDir);
ensureDirectory(targetDir);
ctx.ensureDirectory(targetDir);
ctx.unpack(tempFile, targetDir, excludes, includes);
}
}

0 comments on commit 0a1a07b

Please sign in to comment.