fix: some fixes in the filesystem

This commit is contained in:
TopchetoEU 2024-04-20 22:18:47 +03:00
parent e33cdbb172
commit ba6462458c
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 51 additions and 14 deletions

View File

@ -51,7 +51,7 @@ public class FilesystemLib {
try { try {
if (fs.stat(path).type != EntryType.FILE) { if (fs.stat(path).type != EntryType.FILE) {
throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN); throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN).setPath(path);
} }
return new FileLib(fs.open(path, _mode)); return new FileLib(fs.open(path, _mode));

View File

@ -29,7 +29,11 @@ import me.topchetoeu.jscript.utils.filesystem.Mode;
import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem; import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem;
import me.topchetoeu.jscript.utils.filesystem.RootFilesystem; import me.topchetoeu.jscript.utils.filesystem.RootFilesystem;
import me.topchetoeu.jscript.utils.filesystem.STDFilesystem; import me.topchetoeu.jscript.utils.filesystem.STDFilesystem;
import me.topchetoeu.jscript.utils.interop.Arguments;
import me.topchetoeu.jscript.utils.interop.Expose;
import me.topchetoeu.jscript.utils.interop.ExposeType;
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider; import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
import me.topchetoeu.jscript.utils.interop.WrapperName;
import me.topchetoeu.jscript.utils.modules.ModuleRepo; import me.topchetoeu.jscript.utils.modules.ModuleRepo;
import me.topchetoeu.jscript.utils.permissions.PermissionsManager; import me.topchetoeu.jscript.utils.permissions.PermissionsManager;
import me.topchetoeu.jscript.utils.permissions.PermissionsProvider; import me.topchetoeu.jscript.utils.permissions.PermissionsProvider;
@ -43,6 +47,37 @@ public class JScriptRepl {
static int j = 0; static int j = 0;
static String[] args; static String[] args;
public static interface Interface {
void print();
}
public static class Test implements Interface {
public final int a = 10;
public final int b = 5;
@Override
public void print() {
System.out.println("test!");
}
}
@WrapperName("Interface")
public static class InterfaceLib {
@Expose
public static void __print(Arguments args) { args.self(Interface.class).print(); }
}
@WrapperName("Testificate")
public static class TestLib {
@Expose(type = ExposeType.GETTER)
public static int __a(Arguments args) { return args.self(Test.class).a; }
@Expose(type = ExposeType.GETTER)
public static int __b(Arguments args) { return args.self(Test.class).b; }
@Expose
public static void __print(Arguments args) { System.out.println("NO!"); }
}
private static void reader() { private static void reader() {
try { try {
for (var arg : args) { for (var arg : args) {
@ -92,6 +127,9 @@ public class JScriptRepl {
var wp = NativeWrapperProvider.get(environment); var wp = NativeWrapperProvider.get(environment);
var glob = GlobalScope.get(environment); var glob = GlobalScope.get(environment);
wp.set(Interface.class, InterfaceLib.class);
wp.set(Test.class, TestLib.class);
glob.define(null, false, new NativeFunction("exit", args -> { glob.define(null, false, new NativeFunction("exit", args -> {
throw new InterruptException(); throw new InterruptException();
})); }));
@ -113,6 +151,12 @@ public class JScriptRepl {
return null; return null;
})); }));
var test = new Test();
glob.define(environment, "test1", false, test);
glob.define(environment, "test2", false, test);
glob.define(environment, false, wp.getConstr(TestLib.class));
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("."));

View File

@ -32,7 +32,7 @@ public abstract class BaseFile<T> implements File {
@Override public synchronized 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 == Mode.NONE) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking.");
return onSeek(offset, pos); return onSeek(offset, pos);
} }
catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); } catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); }

View File

@ -54,16 +54,12 @@ public class FilesystemException extends RuntimeException {
@Override public String getMessage() { @Override public String getMessage() {
var parts = new ArrayList<String>(10); var parts = new ArrayList<String>(10);
path = String.join(" ", parts).trim();
if (path.isEmpty()) path = null;
parts.clear();
parts.add(action == null ? "An action performed upon " : action.readable(reason.usePast)); parts.add(action == null ? "An action performed upon " : action.readable(reason.usePast));
if (entry == EntryType.FILE) parts.add("file"); if (entry == EntryType.FILE) parts.add("file");
if (entry == EntryType.FOLDER) parts.add("folder"); if (entry == EntryType.FOLDER) parts.add("folder");
if (path != null) parts.add(path); if (path != null && !path.isBlank()) parts.add(path.trim());
parts.add(reason.readable); parts.add(reason.readable);

View File

@ -1,5 +1,6 @@
package me.topchetoeu.jscript.utils.filesystem; package me.topchetoeu.jscript.utils.filesystem;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
@ -35,7 +36,8 @@ public class PhysicalFilesystem implements Filesystem {
)); ));
else return handles.put(new PhysicalFile(path, perms)); else return handles.put(new PhysicalFile(path, perms));
} }
catch (IOException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); } catch (FileNotFoundException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); }
catch (IOException e) { throw new FilesystemException(ErrorReason.NO_PERMISSION); }
} }
catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); } catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); }
} }
@ -68,12 +70,7 @@ public class PhysicalFilesystem implements Filesystem {
if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE); if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE);
var perms = Mode.NONE; var perms = Mode.of(Files.isReadable(path), Files.isWritable(path));
if (Files.isReadable(path)) {
if (Files.isWritable(path)) perms = Mode.READ_WRITE;
else perms = Mode.READ;
}
if (perms == Mode.NONE) return new FileStat(Mode.NONE, EntryType.NONE); if (perms == Mode.NONE) return new FileStat(Mode.NONE, EntryType.NONE);