From 0fb336373a2323e8dd63cdb28fb7cf65932627cf Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:50:57 +0200 Subject: [PATCH] fix: make fs calls synchronized --- gradle.properties | 2 +- .../topchetoeu/jscript/utils/JScriptRepl.java | 2 +- .../jscript/utils/filesystem/BaseFile.java | 8 ++-- .../jscript/utils/filesystem/File.java | 8 ++-- .../utils/filesystem/MemoryFilesystem.java | 8 ++-- .../utils/filesystem/PhysicalFilesystem.java | 8 ++-- .../utils/filesystem/RootFilesystem.java | 8 ++-- .../utils/filesystem/STDFilesystem.java | 44 ++++++++++++------- 8 files changed, 49 insertions(+), 39 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5df4e99..ff3f81e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ project_group = me.topchetoeu project_name = jscript -project_version = 0.9.6-beta +project_version = 0.9.7-beta main_class = me.topchetoeu.jscript.utils.JScriptRepl diff --git a/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java b/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java index 4dce7b4..5cdd34b 100644 --- a/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java +++ b/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java @@ -111,7 +111,7 @@ public class JScriptRepl { var fs = new RootFilesystem(PermissionsProvider.get(environment)); fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE)); fs.protocols.put("file", new PhysicalFilesystem(".")); - fs.protocols.put("std", STDFilesystem.ofStd(System.in, System.out, System.err)); + fs.protocols.put("std", new STDFilesystem(System.in, System.out, System.err)); environment.add(PermissionsProvider.KEY, PermissionsManager.ALL_PERMS); environment.add(Filesystem.KEY, fs); diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java b/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java index 5fc9c34..0253a9d 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java @@ -13,7 +13,7 @@ public abstract class BaseFile implements File { protected abstract long onSeek(long offset, int pos); protected abstract boolean onClose(); - @Override public int read(byte[] buff) { + @Override public synchronized int read(byte[] buff) { try { if (handle == null) throw new FilesystemException(ErrorReason.CLOSED); if (!mode.readable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for reading."); @@ -21,7 +21,7 @@ public abstract class BaseFile implements File { } catch (FilesystemException e) { throw e.setAction(ActionType.READ); } } - @Override public void write(byte[] buff) { + @Override public synchronized void write(byte[] buff) { try { if (handle == null) throw new FilesystemException(ErrorReason.CLOSED); if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for writting."); @@ -29,7 +29,7 @@ public abstract class BaseFile implements File { } catch (FilesystemException e) { throw e.setAction(ActionType.WRITE); } } - @Override public long seek(long offset, int pos) { + @Override public synchronized long seek(long offset, int pos) { try { if (handle == null) throw new FilesystemException(ErrorReason.CLOSED); if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking."); @@ -37,7 +37,7 @@ public abstract class BaseFile implements File { } catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); } } - @Override public boolean close() { + @Override public synchronized boolean close() { if (handle != null) { try { var res = onClose(); diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/File.java b/src/java/me/topchetoeu/jscript/utils/filesystem/File.java index 59757a3..f98c566 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/File.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/File.java @@ -66,7 +66,7 @@ public interface File { public static File ofStream(InputStream str) { return new File() { - @Override public int read(byte[] buff) { + @Override public synchronized int read(byte[] buff) { try { try { return str.read(buff); } catch (NullPointerException e) { throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); } @@ -78,7 +78,7 @@ public interface File { } public static File ofStream(OutputStream str) { return new File() { - @Override public void write(byte[] buff) { + @Override public synchronized void write(byte[] buff) { try { try { str.write(buff); } catch (NullPointerException e) {throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); } @@ -91,7 +91,7 @@ public interface File { public static File ofLineWriter(LineWriter writer) { var buff = new Buffer(); return new File() { - @Override public void write(byte[] val) { + @Override public synchronized void write(byte[] val) { try { if (val == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null."); @@ -118,7 +118,7 @@ public interface File { private byte[] prev = new byte[0]; @Override - public int read(byte[] buff) { + public synchronized int read(byte[] buff) { try { if (buff == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null."); var ptr = 0; diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/MemoryFilesystem.java b/src/java/me/topchetoeu/jscript/utils/filesystem/MemoryFilesystem.java index 8967294..68b1e4c 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/MemoryFilesystem.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/MemoryFilesystem.java @@ -20,7 +20,7 @@ public class MemoryFilesystem implements Filesystem { @Override public String normalize(String... path) { return Paths.normalize(path); } - @Override public File open(String _path, Mode perms) { + @Override public synchronized File open(String _path, Mode perms) { try { var path = realPath(_path); var pcount = path.getNameCount(); @@ -47,7 +47,7 @@ public class MemoryFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.OPEN); } } - @Override public boolean create(String _path, EntryType type) { + @Override public synchronized boolean create(String _path, EntryType type) { try { var path = realPath(_path); @@ -69,14 +69,14 @@ public class MemoryFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.CREATE); } } - @Override public FileStat stat(String _path) { + @Override public synchronized FileStat stat(String _path) { var path = realPath(_path); if (files.containsKey(path)) return new FileStat(mode, EntryType.FILE); else if (folders.contains(path)) return new FileStat(mode, EntryType.FOLDER); else return new FileStat(Mode.NONE, EntryType.NONE); } - @Override public void close() throws FilesystemException { + @Override public synchronized void close() throws FilesystemException { handles.close(); } diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java b/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java index 6793f1d..52c8323 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java @@ -24,7 +24,7 @@ public class PhysicalFilesystem implements Filesystem { @Override public String normalize(String... paths) { return Paths.normalize(paths); } - @Override public File open(String _path, Mode perms) { + @Override public synchronized File open(String _path, Mode perms) { try { var path = realPath(normalize(_path)); checkMode(path, perms); @@ -39,7 +39,7 @@ public class PhysicalFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); } } - @Override public boolean create(String _path, EntryType type) { + @Override public synchronized boolean create(String _path, EntryType type) { try { var path = realPath(_path); @@ -63,7 +63,7 @@ public class PhysicalFilesystem implements Filesystem { return true; } - @Override public FileStat stat(String _path) { + @Override public synchronized FileStat stat(String _path) { var path = realPath(_path); if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE); @@ -82,7 +82,7 @@ public class PhysicalFilesystem implements Filesystem { Files.isDirectory(path) ? EntryType.FOLDER : EntryType.FILE ); } - @Override public void close() throws FilesystemException { + @Override public synchronized void close() throws FilesystemException { try { handles.close(); } diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/RootFilesystem.java b/src/java/me/topchetoeu/jscript/utils/filesystem/RootFilesystem.java index b770050..1db7355 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/RootFilesystem.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/RootFilesystem.java @@ -53,7 +53,7 @@ public class RootFilesystem implements Filesystem { else return filename.protocol + "://" + protocol.normalize(paths); } } - @Override public File open(String path, Mode perms) throws FilesystemException { + @Override public synchronized File open(String path, Mode perms) throws FilesystemException { try { var filename = Filename.parse(path); var protocol = getProtocol(filename); @@ -63,7 +63,7 @@ public class RootFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.OPEN); } } - @Override public boolean create(String path, EntryType type) throws FilesystemException { + @Override public synchronized boolean create(String path, EntryType type) throws FilesystemException { try { var filename = Filename.parse(path); var protocol = getProtocol(filename); @@ -73,7 +73,7 @@ public class RootFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.CREATE); } } - @Override public FileStat stat(String path) throws FilesystemException { + @Override public synchronized FileStat stat(String path) throws FilesystemException { try { var filename = Filename.parse(path); var protocol = getProtocol(filename); @@ -82,7 +82,7 @@ public class RootFilesystem implements Filesystem { } catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.STAT); } } - @Override public void close() throws FilesystemException { + @Override public synchronized void close() throws FilesystemException { try { for (var protocol : protocols.values()) { protocol.close(); diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/STDFilesystem.java b/src/java/me/topchetoeu/jscript/utils/filesystem/STDFilesystem.java index 393f3e5..5b82c0e 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/STDFilesystem.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/STDFilesystem.java @@ -2,41 +2,51 @@ package me.topchetoeu.jscript.utils.filesystem; import java.io.InputStream; import java.io.OutputStream; -import java.util.HashMap; public class STDFilesystem implements Filesystem { - private final HashMap handles = new HashMap<>(); + private File in; + private File out; + private File err; @Override public String normalize(String... path) { var res = Paths.normalize(path); while (res.startsWith("/")) res = res.substring(1); + while (res.endsWith("/")) res = res.substring(0, res.length() - 1); return res; } - @Override public File open(String path, Mode mode) { + @Override public synchronized File open(String path, Mode mode) { path = normalize(path); - if (handles.containsKey(path)) return handles.get(path); + if (in != null && path.equals("in")) return in; + else if (out != null && path.equals("out")) return out; + else if (err != null && path.equals("err")) return err; else throw new FilesystemException(ErrorReason.DOESNT_EXIST).setAction(ActionType.OPEN).setPath(path); } - @Override public FileStat stat(String path) { + @Override public synchronized FileStat stat(String path) { path = normalize(path); - if (handles.containsKey(path)) return new FileStat(Mode.READ_WRITE, EntryType.FILE); + if (path.equals("in") || path.equals("out") || path.equals("err")) return new FileStat(Mode.READ_WRITE, EntryType.FILE); else return new FileStat(Mode.NONE, EntryType.NONE); } - @Override public void close() { - handles.clear(); + @Override public synchronized void close() { + in = out = err = null; } - public STDFilesystem add(String name, File handle) { - this.handles.put(name, handle); - return this; + public STDFilesystem(File in, File out, File err) { + this.in = in; + this.out = out; + this.err = err; } - - public static STDFilesystem ofStd(InputStream in, OutputStream out, OutputStream err) { - return new STDFilesystem() - .add("in", File.ofStream(in)) - .add("out", File.ofStream(out)) - .add("err", File.ofStream(err)); + public STDFilesystem(InputStream in, OutputStream out, OutputStream err) { + if (in != null) this.in = File.ofStream(in); + if (out != null) this.out = File.ofStream(out); + if (err != null) this.err = File.ofStream(err); + } + public STDFilesystem(LineReader in, LineWriter out) { + if (in != null) this.in = File.ofLineReader(in); + if (out != null) { + this.out = File.ofLineWriter(out); + this.err = File.ofLineWriter(out); + } } }