fix: make fs calls synchronized
This commit is contained in:
parent
b33325a98d
commit
0fb336373a
@ -1,4 +1,4 @@
|
|||||||
project_group = me.topchetoeu
|
project_group = me.topchetoeu
|
||||||
project_name = jscript
|
project_name = jscript
|
||||||
project_version = 0.9.6-beta
|
project_version = 0.9.7-beta
|
||||||
main_class = me.topchetoeu.jscript.utils.JScriptRepl
|
main_class = me.topchetoeu.jscript.utils.JScriptRepl
|
||||||
|
@ -111,7 +111,7 @@ public class JScriptRepl {
|
|||||||
var fs = new RootFilesystem(PermissionsProvider.get(environment));
|
var fs = new RootFilesystem(PermissionsProvider.get(environment));
|
||||||
fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE));
|
fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE));
|
||||||
fs.protocols.put("file", new PhysicalFilesystem("."));
|
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(PermissionsProvider.KEY, PermissionsManager.ALL_PERMS);
|
||||||
environment.add(Filesystem.KEY, fs);
|
environment.add(Filesystem.KEY, fs);
|
||||||
|
@ -13,7 +13,7 @@ public abstract class BaseFile<T> implements File {
|
|||||||
protected abstract long onSeek(long offset, int pos);
|
protected abstract long onSeek(long offset, int pos);
|
||||||
protected abstract boolean onClose();
|
protected abstract boolean onClose();
|
||||||
|
|
||||||
@Override public int read(byte[] buff) {
|
@Override public synchronized int read(byte[] buff) {
|
||||||
try {
|
try {
|
||||||
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
||||||
if (!mode.readable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for reading.");
|
if (!mode.readable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for reading.");
|
||||||
@ -21,7 +21,7 @@ public abstract class BaseFile<T> implements File {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setAction(ActionType.READ); }
|
catch (FilesystemException e) { throw e.setAction(ActionType.READ); }
|
||||||
}
|
}
|
||||||
@Override public void write(byte[] buff) {
|
@Override public synchronized void write(byte[] buff) {
|
||||||
try {
|
try {
|
||||||
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
||||||
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for writting.");
|
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for writting.");
|
||||||
@ -29,7 +29,7 @@ public abstract class BaseFile<T> implements File {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setAction(ActionType.WRITE); }
|
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 {
|
try {
|
||||||
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
if (handle == null) throw new FilesystemException(ErrorReason.CLOSED);
|
||||||
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking.");
|
if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking.");
|
||||||
@ -37,7 +37,7 @@ public abstract class BaseFile<T> implements File {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); }
|
catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); }
|
||||||
}
|
}
|
||||||
@Override public boolean close() {
|
@Override public synchronized boolean close() {
|
||||||
if (handle != null) {
|
if (handle != null) {
|
||||||
try {
|
try {
|
||||||
var res = onClose();
|
var res = onClose();
|
||||||
|
@ -66,7 +66,7 @@ public interface File {
|
|||||||
|
|
||||||
public static File ofStream(InputStream str) {
|
public static File ofStream(InputStream str) {
|
||||||
return new File() {
|
return new File() {
|
||||||
@Override public int read(byte[] buff) {
|
@Override public synchronized int read(byte[] buff) {
|
||||||
try {
|
try {
|
||||||
try { return str.read(buff); }
|
try { return str.read(buff); }
|
||||||
catch (NullPointerException e) { throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
|
catch (NullPointerException e) { throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
|
||||||
@ -78,7 +78,7 @@ public interface File {
|
|||||||
}
|
}
|
||||||
public static File ofStream(OutputStream str) {
|
public static File ofStream(OutputStream str) {
|
||||||
return new File() {
|
return new File() {
|
||||||
@Override public void write(byte[] buff) {
|
@Override public synchronized void write(byte[] buff) {
|
||||||
try {
|
try {
|
||||||
try { str.write(buff); }
|
try { str.write(buff); }
|
||||||
catch (NullPointerException e) {throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
|
catch (NullPointerException e) {throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, e.getMessage()); }
|
||||||
@ -91,7 +91,7 @@ public interface File {
|
|||||||
public static File ofLineWriter(LineWriter writer) {
|
public static File ofLineWriter(LineWriter writer) {
|
||||||
var buff = new Buffer();
|
var buff = new Buffer();
|
||||||
return new File() {
|
return new File() {
|
||||||
@Override public void write(byte[] val) {
|
@Override public synchronized void write(byte[] val) {
|
||||||
try {
|
try {
|
||||||
if (val == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null.");
|
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];
|
private byte[] prev = new byte[0];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] buff) {
|
public synchronized int read(byte[] buff) {
|
||||||
try {
|
try {
|
||||||
if (buff == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null.");
|
if (buff == null) throw new FilesystemException(ErrorReason.ILLEGAL_ARGS, "Given buffer is null.");
|
||||||
var ptr = 0;
|
var ptr = 0;
|
||||||
|
@ -20,7 +20,7 @@ public class MemoryFilesystem implements Filesystem {
|
|||||||
@Override public String normalize(String... path) {
|
@Override public String normalize(String... path) {
|
||||||
return Paths.normalize(path);
|
return Paths.normalize(path);
|
||||||
}
|
}
|
||||||
@Override public File open(String _path, Mode perms) {
|
@Override public synchronized File open(String _path, Mode perms) {
|
||||||
try {
|
try {
|
||||||
var path = realPath(_path);
|
var path = realPath(_path);
|
||||||
var pcount = path.getNameCount();
|
var pcount = path.getNameCount();
|
||||||
@ -47,7 +47,7 @@ public class MemoryFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.OPEN); }
|
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 {
|
try {
|
||||||
var path = realPath(_path);
|
var path = realPath(_path);
|
||||||
|
|
||||||
@ -69,14 +69,14 @@ public class MemoryFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setPath(_path).setAction(ActionType.CREATE); }
|
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);
|
var path = realPath(_path);
|
||||||
|
|
||||||
if (files.containsKey(path)) return new FileStat(mode, EntryType.FILE);
|
if (files.containsKey(path)) return new FileStat(mode, EntryType.FILE);
|
||||||
else if (folders.contains(path)) return new FileStat(mode, EntryType.FOLDER);
|
else if (folders.contains(path)) return new FileStat(mode, EntryType.FOLDER);
|
||||||
else return new FileStat(Mode.NONE, EntryType.NONE);
|
else return new FileStat(Mode.NONE, EntryType.NONE);
|
||||||
}
|
}
|
||||||
@Override public void close() throws FilesystemException {
|
@Override public synchronized void close() throws FilesystemException {
|
||||||
handles.close();
|
handles.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ public class PhysicalFilesystem implements Filesystem {
|
|||||||
@Override public String normalize(String... paths) {
|
@Override public String normalize(String... paths) {
|
||||||
return Paths.normalize(paths);
|
return Paths.normalize(paths);
|
||||||
}
|
}
|
||||||
@Override public File open(String _path, Mode perms) {
|
@Override public synchronized File open(String _path, Mode perms) {
|
||||||
try {
|
try {
|
||||||
var path = realPath(normalize(_path));
|
var path = realPath(normalize(_path));
|
||||||
checkMode(path, perms);
|
checkMode(path, perms);
|
||||||
@ -39,7 +39,7 @@ public class PhysicalFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); }
|
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 {
|
try {
|
||||||
var path = realPath(_path);
|
var path = realPath(_path);
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ public class PhysicalFilesystem implements Filesystem {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@Override public FileStat stat(String _path) {
|
@Override public synchronized FileStat stat(String _path) {
|
||||||
var path = realPath(_path);
|
var path = realPath(_path);
|
||||||
|
|
||||||
if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE);
|
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
|
Files.isDirectory(path) ? EntryType.FOLDER : EntryType.FILE
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@Override public void close() throws FilesystemException {
|
@Override public synchronized void close() throws FilesystemException {
|
||||||
try {
|
try {
|
||||||
handles.close();
|
handles.close();
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class RootFilesystem implements Filesystem {
|
|||||||
else return filename.protocol + "://" + protocol.normalize(paths);
|
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 {
|
try {
|
||||||
var filename = Filename.parse(path);
|
var filename = Filename.parse(path);
|
||||||
var protocol = getProtocol(filename);
|
var protocol = getProtocol(filename);
|
||||||
@ -63,7 +63,7 @@ public class RootFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.OPEN); }
|
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 {
|
try {
|
||||||
var filename = Filename.parse(path);
|
var filename = Filename.parse(path);
|
||||||
var protocol = getProtocol(filename);
|
var protocol = getProtocol(filename);
|
||||||
@ -73,7 +73,7 @@ public class RootFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.CREATE); }
|
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 {
|
try {
|
||||||
var filename = Filename.parse(path);
|
var filename = Filename.parse(path);
|
||||||
var protocol = getProtocol(filename);
|
var protocol = getProtocol(filename);
|
||||||
@ -82,7 +82,7 @@ public class RootFilesystem implements Filesystem {
|
|||||||
}
|
}
|
||||||
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.STAT); }
|
catch (FilesystemException e) { throw e.setPath(path).setAction(ActionType.STAT); }
|
||||||
}
|
}
|
||||||
@Override public void close() throws FilesystemException {
|
@Override public synchronized void close() throws FilesystemException {
|
||||||
try {
|
try {
|
||||||
for (var protocol : protocols.values()) {
|
for (var protocol : protocols.values()) {
|
||||||
protocol.close();
|
protocol.close();
|
||||||
|
@ -2,41 +2,51 @@ package me.topchetoeu.jscript.utils.filesystem;
|
|||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class STDFilesystem implements Filesystem {
|
public class STDFilesystem implements Filesystem {
|
||||||
private final HashMap<String, File> handles = new HashMap<>();
|
private File in;
|
||||||
|
private File out;
|
||||||
|
private File err;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String normalize(String... path) {
|
public String normalize(String... path) {
|
||||||
var res = Paths.normalize(path);
|
var res = Paths.normalize(path);
|
||||||
while (res.startsWith("/")) res = res.substring(1);
|
while (res.startsWith("/")) res = res.substring(1);
|
||||||
|
while (res.endsWith("/")) res = res.substring(0, res.length() - 1);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public File open(String path, Mode mode) {
|
@Override public synchronized File open(String path, Mode mode) {
|
||||||
path = normalize(path);
|
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);
|
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);
|
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);
|
else return new FileStat(Mode.NONE, EntryType.NONE);
|
||||||
}
|
}
|
||||||
@Override public void close() {
|
@Override public synchronized void close() {
|
||||||
handles.clear();
|
in = out = err = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public STDFilesystem add(String name, File handle) {
|
public STDFilesystem(File in, File out, File err) {
|
||||||
this.handles.put(name, handle);
|
this.in = in;
|
||||||
return this;
|
this.out = out;
|
||||||
|
this.err = err;
|
||||||
}
|
}
|
||||||
|
public STDFilesystem(InputStream in, OutputStream out, OutputStream err) {
|
||||||
public static STDFilesystem ofStd(InputStream in, OutputStream out, OutputStream err) {
|
if (in != null) this.in = File.ofStream(in);
|
||||||
return new STDFilesystem()
|
if (out != null) this.out = File.ofStream(out);
|
||||||
.add("in", File.ofStream(in))
|
if (err != null) this.err = File.ofStream(err);
|
||||||
.add("out", File.ofStream(out))
|
}
|
||||||
.add("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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user