fix that a broken configure file is left over when something goes wrong

Fri, 05 Jan 2024 21:02:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Jan 2024 21:02:37 +0100
changeset 93
df47e083139b
parent 92
5a32534f2604
child 94
002497e6c27d

fix that a broken configure file is left over when something goes wrong

src/main/java/de/unixwork/uwproj/Main.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/unixwork/uwproj/Main.java	Fri Jan 05 20:53:42 2024 +0100
+++ b/src/main/java/de/unixwork/uwproj/Main.java	Fri Jan 05 21:02:37 2024 +0100
@@ -2,15 +2,15 @@
 
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
-import org.slf4j.LoggerFactory;
 
 import java.io.*;
-import java.nio.file.*;
-import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.PosixFilePermissions;
 import java.util.Arrays;
 import java.util.Objects;
-import java.util.Set;
 
 public class Main {
 
@@ -173,8 +173,13 @@
         System.out.println("In:  " + inFileName);
         System.out.println("Tpl: " + tplFileName);
 
-        try (var out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileName)))) {
-            writeConfigureScript(out, tplFileName, loadProjectFile(inFileName));
+        final var outFilePath = Path.of(outFileName);
+        try {
+            final var tmp = File.createTempFile("uwproj", null);
+            try (var out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmp)))) {
+                writeConfigureScript(out, tplFileName, loadProjectFile(inFileName));
+            }
+            Files.copy(tmp.toPath(), outFilePath, StandardCopyOption.REPLACE_EXISTING);
         } catch (Throwable t) {
             System.err.printf("Unexpected error: %s\n", t.getMessage());
             abort();
@@ -182,7 +187,7 @@
 
         System.out.println("Out: " + outFileName);
         try {
-            Files.setPosixFilePermissions(Path.of(outFileName), PosixFilePermissions.fromString("rwxr-xr-x"));
+            Files.setPosixFilePermissions(outFilePath, PosixFilePermissions.fromString("rwxr-xr-x"));
         } catch (Throwable t) {
             System.err.printf("WARN: Setting file permissions failed: %s\n", t.getMessage());
         }

mercurial