Skip to content

Commit

Permalink
Merge pull request #12810 from aloubyansky/create-or-empty-dir
Browse files Browse the repository at this point in the history
Replace recursiveDeleteAndThenCreate(dir) with createOrEmptyDir(dir)
  • Loading branch information
aloubyansky authored Oct 20, 2020
2 parents a21f2bc + 311d0af commit ef410e6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void extractDevModeClasses(Path appRoot, AppModel appModel, PostEx
//not all local projects are dependencies
continue;
}
IoUtils.recursiveDeleteAndThenCreate(moduleClasses);
IoUtils.createOrEmptyDir(moduleClasses);
for (Path p : dep.getPaths()) {
if (Files.isDirectory(p)) {
Path moduleTarget = moduleClasses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AppCDSBuildStep {
public void requested(OutputTargetBuildItem outputTarget, BuildProducer<AppCDSRequestedBuildItem> producer)
throws IOException {
Path appCDSDir = outputTarget.getOutputDirectory().resolve("appcds");
IoUtils.recursiveDeleteAndThenCreate(appCDSDir);
IoUtils.createOrEmptyDir(appCDSDir);

producer.produce(new AppCDSRequestedBuildItem(outputTarget.getOutputDirectory().resolve("appcds")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private JarBuildItem buildLegacyThinJar(CurateOutcomeBuildItem curateOutcomeBuil
.resolve(outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix + ".jar");
Path libDir = outputTargetBuildItem.getOutputDirectory().resolve("lib");
Files.deleteIfExists(runnerJar);
IoUtils.recursiveDeleteAndThenCreate(libDir);
IoUtils.createOrEmptyDir(libDir);

try (FileSystem runnerZipFs = ZipUtils.newZip(runnerJar)) {

Expand Down Expand Up @@ -464,7 +464,7 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
userProviders = buildDir.resolve(packageConfig.userProvidersDirectory.get());
}
if (!rebuild) {
IoUtils.recursiveDeleteAndThenCreate(buildDir);
IoUtils.createOrEmptyDir(buildDir);
Files.createDirectories(mainLib);
Files.createDirectories(baseLib);
Files.createDirectories(appDir);
Expand All @@ -476,7 +476,7 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
Files.createFile(userProviders.resolve(".keep"));
}
} else {
IoUtils.recursiveDeleteAndThenCreate(quarkus);
IoUtils.createOrEmptyDir(quarkus);
}
Map<AppArtifactKey, List<Path>> copiedArtifacts = new HashMap<>();

Expand Down Expand Up @@ -714,7 +714,7 @@ public NativeImageSourceJarBuildItem buildNativeImageJar(CurateOutcomeBuildItem
List<UberJarRequiredBuildItem> uberJarRequired) throws Exception {
Path targetDirectory = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + "-native-image-source-jar");
IoUtils.recursiveDeleteAndThenCreate(targetDirectory);
IoUtils.createOrEmptyDir(targetDirectory);

List<GeneratedClassBuildItem> allClasses = new ArrayList<>(generatedClasses);
allClasses.addAll(nativeImageResources.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Path devOrTest(CurateOutcomeBuildItem curateOutcomeBuildItem, Laun

// Clean on non dev mode
if (!launch.getLaunchMode().equals(LaunchMode.DEVELOPMENT)) {
IoUtils.recursiveDeleteAndThenCreate(path);
IoUtils.createOrEmptyDir(path);
}

if (isEmpty(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
Expand All @@ -18,6 +18,7 @@
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.Objects;
import java.util.UUID;

/**
Expand Down Expand Up @@ -90,31 +91,27 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e)
}
}

public static void recursiveDeleteAndThenCreate(Path root) throws IOException {
recursiveDelete(root);
if (!PropertyUtils.isWindows()) {
Files.createDirectories(root);
/**
* Creates a new empty directory or empties an existing one.
*
* @param dir directory
* @throws IOException in case of a failure
*/
public static void createOrEmptyDir(Path dir) throws IOException {
Objects.requireNonNull(dir);
if (!Files.exists(dir)) {
Files.createDirectories(dir);
return;
}

// in Windows, recreating a directory right after deleting it can be problematic, see https://bugs.openjdk.java.net/browse/JDK-8029608
// so we just try the create a operation a few times and hope it works

final int maxTries = 3;
int i = 0;
while (true) {
try {
Files.createDirectories(root);
return;
} catch (AccessDeniedException e) {
if (++i == maxTries) {
throw e;
}

try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

if (!Files.isDirectory(dir)) {
throw new IllegalArgumentException(dir + " is not a directory");
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path p : stream) {
if (Files.isDirectory(p)) {
recursiveDelete(p);
} else {
Files.delete(p);
}
}
}
Expand Down

0 comments on commit ef410e6

Please sign in to comment.