Browse Source

file api fix

Kajetan Johannes Hammerle 2 years ago
parent
commit
eef302a32b

+ 15 - 8
src/me/hammerle/snuviscript/code/FunctionRegistry.java

@@ -5,7 +5,11 @@ import java.io.File;
 import java.nio.charset.StandardCharsets;
 import java.lang.reflect.Array;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.attribute.FileAttribute;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
 import java.time.ZonedDateTime;
 import java.util.List;
 import java.util.ArrayList;
@@ -24,6 +28,9 @@ import me.hammerle.snuviscript.config.SnuviConfig;
 import me.hammerle.snuviscript.inputprovider.Variable;
 
 public class FunctionRegistry {
+    public static final FileAttribute<Set<PosixFilePermission>> FILE_ACCESS =
+            PosixFilePermissions.asFileAttribute(
+                    PosixFilePermissions.fromString("rwxrwxrwx"));
     private static final HashMap<String, Object> GLOBAL_VARS = new HashMap<>();
     private static final HashMap<String, NamedFunction> FUNCTIONS = new HashMap<>();
 
@@ -354,16 +361,16 @@ public class FunctionRegistry {
                 (sc, in) -> Files.readAllLines(((File) in[0].get(sc)).toPath()));
         registerConsumer("file.write", (sc, in) -> {
             File f = (File) in[0].get(sc);
-            if(f.getParentFile() != null) {
-                f.getParentFile().mkdirs();
-            }
+            Path p = Paths.get(f.toURI());
             if(!f.exists()) {
-                f.createNewFile();
+                Files.createFile(p, FILE_ACCESS);
             }
-            Files.write(
-                    Paths.get(f.toURI()), ((List<Object>) in[1].get(sc)).stream()
-                            .map(o -> String.valueOf(o)).collect(Collectors.toList()),
-                    StandardCharsets.UTF_8);
+            Files.write(p, ((List<Object>) in[1].get(sc)).stream().map(o -> String.valueOf(o))
+                    .collect(Collectors.toList()), StandardCharsets.UTF_8);
+        });
+        registerConsumer("file.createfolder", (sc, in) -> {
+            File f = (File) in[0].get(sc);
+            Files.createDirectory(Paths.get(f.toURI()), FILE_ACCESS);
         });
         registerFunction("config.new",
                 (sc, in) -> new SnuviConfig(in[0].getString(sc), in[1].getString(sc)));

+ 5 - 5
src/me/hammerle/snuviscript/config/SnuviConfig.java

@@ -5,9 +5,11 @@ import java.io.IOException;
 import java.nio.charset.MalformedInputException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.TreeMap;
 import java.util.stream.Collectors;
+import me.hammerle.snuviscript.code.FunctionRegistry;
 import me.hammerle.snuviscript.code.Script;
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.hammerle.snuviscript.code.SnuviUtils;
@@ -87,18 +89,16 @@ public class SnuviConfig {
         }
         dirty = false;
         try {
-            if(file.getParentFile() != null) {
-                file.getParentFile().mkdirs();
-            }
+            Path p = Paths.get(file.toURI());
             if(!file.exists()) {
                 try {
-                    file.createNewFile();
+                    Files.createFile(p, FunctionRegistry.FILE_ACCESS);
                 } catch(IOException ex) {
                     print(sc, "'" + file.getPath() + "' cannot be created", ex);
                     return false;
                 }
             }
-            Files.write(Paths.get(file.toURI()), conf.entrySet().stream().map(e -> {
+            Files.write(p, conf.entrySet().stream().map(e -> {
                 if(e.getValue().getClass() == String.class) {
                     return String.format("%s=\"%s\"", e.getKey(),
                             e.getValue().toString().replaceAll("\n", "\\n"));