fix: losts of FS API improvements

This commit is contained in:
TopchetoEU 2023-12-26 14:01:31 +02:00
parent 8e01db637b
commit 4bc363485f
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
10 changed files with 100 additions and 106 deletions

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript; package me.topchetoeu.jscript;
import java.io.File; import java.io.File;
import java.nio.file.Path;
public class Filename { public class Filename {
public final String protocol; public final String protocol;
@ -40,9 +41,7 @@ public class Filename {
return true; return true;
} }
public static Filename fromFile(File file) {
return new Filename("file", file.getAbsolutePath());
}
public Filename(String protocol, String path) { public Filename(String protocol, String path) {
@ -57,4 +56,10 @@ public class Filename {
if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim()); if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim());
else return new Filename("file", val.trim()); else return new Filename("file", val.trim());
} }
public static Path normalize(String path) {
return Path.of(Path.of("/" + path.trim().replace("\\", "/")).normalize().toString().substring(1));
}
public static Filename fromFile(File file) {
return new Filename("file", file.getAbsolutePath());
}
} }

View File

@ -1,8 +1,7 @@
package me.topchetoeu.jscript.filesystem; package me.topchetoeu.jscript.filesystem;
public interface Filesystem { public interface Filesystem {
String cwd(String cwd, String path); String normalize(String... path);
String normalize(String path);
File open(String path, Mode mode) throws FilesystemException; File open(String path, Mode mode) throws FilesystemException;
void create(String path, EntryType type) throws FilesystemException; void create(String path, EntryType type) throws FilesystemException;
FileStat stat(String path) throws FilesystemException; FileStat stat(String path) throws FilesystemException;

View File

@ -1,56 +1,56 @@
package me.topchetoeu.jscript.filesystem; package me.topchetoeu.jscript.filesystem;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.util.Iterator;
import java.nio.file.Path;
import java.util.stream.Stream; import java.util.stream.Stream;
import me.topchetoeu.jscript.filesystem.FilesystemException.FSCode; import me.topchetoeu.jscript.filesystem.FilesystemException.FSCode;
public class ListFile implements File { public class ListFile implements File {
private Stream<Path> stream; private Iterator<String> it;
private String filename; private String filename;
private byte[] currFile; private byte[] currFile;
private long ptr = 0, start = 0, end = 0; private long ptr = 0, start = 0, end = 0;
private void next() { private void next() {
while (stream != null) { if (it != null && it.hasNext()) {
var opt = stream.findFirst(); start = end;
currFile = (it.next() + "\n").getBytes();
if (opt.isPresent()) { end = start + currFile.length;
start = end; }
currFile = (opt.get().toString() + "\n").getBytes(); else {
end = start + currFile.length; it = null;
} currFile = null;
else { end = -1;
stream = null;
currFile = null;
}
} }
} }
@Override @Override
public void close() { public void close() {
stream = null; it = null;
currFile = null; currFile = null;
} }
@Override @Override
public int read(byte[] buff) { public int read(byte[] buff) {
if (stream == null) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
if (ptr < start) return 0; if (ptr < start) return 0;
if (it == null) return 0;
var i = 0; var i = 0;
while (i < buff.length) { while (i < buff.length) {
while (i > end) next(); while (i + ptr >= end) {
next();
if (it == null) return 0;
}
int cpyN = Math.min(currFile.length, buff.length - i); int cpyN = Math.min(currFile.length, buff.length - i);
System.arraycopy(buff, i, currFile, 0, cpyN); System.arraycopy(currFile, (int)(ptr + i - start), buff, i, cpyN);
i += cpyN; i += cpyN;
} }
ptr += i;
return i; return i;
} }
@ -66,8 +66,8 @@ public class ListFile implements File {
throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW); throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW);
} }
public ListFile(Path path) throws IOException { public ListFile(String filename, Stream<String> stream) throws IOException {
stream = Files.list(path); this.it = stream.iterator();
filename = path.toString(); this.filename = filename;
} }
} }

View File

@ -18,15 +18,10 @@ public class MemoryFilesystem implements Filesystem {
} }
@Override @Override
public String normalize(String path) { public String normalize(String... path) {
return Paths.normalize(path); return Paths.normalize(path);
} }
@Override
public String cwd(String cwd, String path) {
return Paths.cwd(cwd, path);
}
@Override @Override
public void create(String _path, EntryType type) { public void create(String _path, EntryType type) {
var path = realPath(_path); var path = realPath(_path);

View File

@ -3,8 +3,8 @@ package me.topchetoeu.jscript.filesystem;
import java.util.ArrayList; import java.util.ArrayList;
public class Paths { public class Paths {
public static String normalize(String path) { public static String normalize(String... path) {
var parts = path.split("[\\\\/]"); var parts = String.join("/", path).split("[\\\\/]");
var res = new ArrayList<String>(); var res = new ArrayList<String>();
for (var part : parts) { for (var part : parts) {
@ -12,14 +12,15 @@ public class Paths {
else if (part.equals("..")) { else if (part.equals("..")) {
if (res.size() > 0) res.remove(res.size() - 1); if (res.size() > 0) res.remove(res.size() - 1);
} }
else if (!part.equals(".")) res.add(part); else if (!part.equals(".") && !part.isEmpty()) res.add(part);
} }
var sb = new StringBuilder(); var sb = new StringBuilder();
for (var el : res) sb.append("/").append(el); for (var el : res) sb.append("/").append(el);
return sb.toString(); if (sb.isEmpty()) return "/";
else return sb.toString();
} }
public static String chroot(String root, String path) { public static String chroot(String root, String path) {

View File

@ -9,24 +9,24 @@ import me.topchetoeu.jscript.filesystem.FilesystemException.FSCode;
public class PhysicalFile implements File { public class PhysicalFile implements File {
private String filename; private String filename;
private RandomAccessFile file; private RandomAccessFile file;
private Mode perms; private Mode mode;
@Override @Override
public int read(byte[] buff) { public int read(byte[] buff) {
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); if (file == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
else try { return file.read(buff); } else try { return file.read(buff); }
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); } catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); }
} }
@Override @Override
public void write(byte[] buff) { public void write(byte[] buff) {
if (file == null || !perms.writable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW); if (file == null || !mode.writable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW);
else try { file.write(buff); } else try { file.write(buff); }
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW); } catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW); }
} }
@Override @Override
public long seek(long offset, int pos) { public long seek(long offset, int pos) {
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); if (file == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
try { try {
if (pos == 1) offset += file.getFilePointer(); if (pos == 1) offset += file.getFilePointer();
@ -43,14 +43,15 @@ public class PhysicalFile implements File {
try { file.close(); } try { file.close(); }
catch (IOException e) {} // SHUT catch (IOException e) {} // SHUT
file = null; file = null;
perms = Mode.NONE; mode = Mode.NONE;
} }
public PhysicalFile(String path, Mode mode) throws FileNotFoundException { public PhysicalFile(String name, String path, Mode mode) throws FileNotFoundException {
this.filename = name;
this.mode = mode;
if (mode == Mode.NONE) file = null; if (mode == Mode.NONE) file = null;
else try { file = new RandomAccessFile(path, mode.name); } else try { file = new RandomAccessFile(path, mode.name); }
catch (FileNotFoundException e) { throw new FilesystemException(filename, FSCode.DOESNT_EXIST); } catch (FileNotFoundException e) { throw new FilesystemException(filename, FSCode.DOESNT_EXIST); }
perms = mode;
} }
} }

View File

@ -11,9 +11,9 @@ public class PhysicalFilesystem implements Filesystem {
private void checkMode(Path path, Mode mode) { private void checkMode(Path path, Mode mode) {
if (!path.startsWith(root)) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_R); if (!path.startsWith(root)) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_R);
var stat = stat(path.toString());
if (mode.readable && !stat.mode.readable) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_R); if (mode.readable && !Files.isReadable(path)) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_R);
if (mode.writable && !stat.mode.writable) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_RW); if (mode.writable && !Files.isWritable(path)) throw new FilesystemException(path.toString(), FSCode.NO_PERMISSIONS_RW);
} }
private Path realPath(String path) { private Path realPath(String path) {
@ -21,24 +21,20 @@ public class PhysicalFilesystem implements Filesystem {
} }
@Override @Override
public String normalize(String path) { public String normalize(String... paths) {
return Paths.normalize(path); return Paths.normalize(paths);
}
@Override
public String cwd(String cwd, String path) {
return Paths.cwd(cwd, path);
} }
@Override @Override
public File open(String _path, Mode perms) { public File open(String _path, Mode perms) {
_path = normalize(_path);
var path = realPath(_path); var path = realPath(_path);
checkMode(path, perms); checkMode(path, perms);
try { try {
if (Files.isDirectory(path)) return new ListFile(path); if (Files.isDirectory(path)) return new ListFile(_path, Files.list(path).map((v -> v.getFileName().toString())));
else return new PhysicalFile(path.toString(), perms); else return new PhysicalFile(_path, path.toString(), perms);
} }
catch (IOException e) { throw new FilesystemException(path.toString(), FSCode.DOESNT_EXIST); } catch (IOException e) { throw new FilesystemException(path.toString(), FSCode.DOESNT_EXIST); }
} }

View File

@ -23,17 +23,17 @@ public class RootFilesystem implements Filesystem {
if (mode.writable && perms != null && !canWrite(_path)) throw new FilesystemException(_path, FSCode.NO_PERMISSIONS_RW); if (mode.writable && perms != null && !canWrite(_path)) throw new FilesystemException(_path, FSCode.NO_PERMISSIONS_RW);
} }
@Override public String normalize(String path) { @Override public String normalize(String... paths) {
var filename = Filename.parse(path); if (paths.length == 0) return "file://";
var protocol = protocols.get(filename.protocol); else {
if (protocol == null) return filename.toString(); var filename = Filename.parse(paths[0]);
else return new Filename(filename.protocol, protocol.normalize(filename.path)).toString(); var protocol = protocols.get(filename.protocol);
} paths[0] = filename.path;
@Override public String cwd(String cwd, String path) {
var filename = Filename.parse(cwd);
var protocol = protocols.get(filename.protocol); if (protocol == null) return Paths.normalize(paths);
if (protocol == null) return filename.toString(); else return filename.protocol + "://" + protocol.normalize(paths);
else return new Filename(filename.protocol, protocol.cwd(filename.path, path)).toString(); }
} }
@Override public File open(String path, Mode perms) throws FilesystemException { @Override public File open(String path, Mode perms) throws FilesystemException {
var filename = Filename.parse(path); var filename = Filename.parse(path);
@ -57,9 +57,8 @@ public class RootFilesystem implements Filesystem {
var filename = Filename.parse(path); var filename = Filename.parse(path);
var protocol = protocols.get(filename.protocol); var protocol = protocols.get(filename.protocol);
if (protocol == null) throw new FilesystemException(filename.toString(), FSCode.DOESNT_EXIST); if (protocol == null) throw new FilesystemException(filename.toString(), FSCode.DOESNT_EXIST);
modeAllowed(filename.toString(), Mode.READ);
try { return protocol.stat(path); } try { return protocol.stat(filename.path); }
catch (FilesystemException e) { throw new FilesystemException(filename.toString(), e.code); } catch (FilesystemException e) { throw new FilesystemException(filename.toString(), e.code); }
} }

View File

@ -15,7 +15,7 @@ public class FileLib {
@NativeGetter public PromiseLib pointer(Context ctx) { @NativeGetter public PromiseLib pointer(Context ctx) {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
return file.getPtr(); return file.seek(0, 1);
} }
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });
@ -23,23 +23,14 @@ public class FileLib {
@NativeGetter public PromiseLib length(Context ctx) { @NativeGetter public PromiseLib length(Context ctx) {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
long curr = file.getPtr(); long curr = file.seek(0, 1);
file.setPtr(0, 2); long res = file.seek(0, 2);
long res = file.getPtr(); file.seek(curr, 0);
file.setPtr(curr, 0);
return res; return res;
} }
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });
} }
@NativeGetter public PromiseLib getMode(Context ctx) {
return PromiseLib.await(ctx, () -> {
try {
return file.mode().name;
}
catch (FilesystemException e) { throw e.toEngineException(); }
});
}
@Native public PromiseLib read(Context ctx, int n) { @Native public PromiseLib read(Context ctx, int n) {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
@ -73,11 +64,10 @@ public class FileLib {
return null; return null;
}); });
} }
@Native public PromiseLib setPointer(Context ctx, long ptr) { @Native public PromiseLib seek(Context ctx, long ptr, int whence) {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
file.setPtr(ptr, 0); return file.seek(ptr, whence);
return null;
} }
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });

View File

@ -20,6 +20,10 @@ import me.topchetoeu.jscript.interop.Native;
@Native("Filesystem") @Native("Filesystem")
public class FilesystemLib { public class FilesystemLib {
@Native public static final int SEEK_SET = 0;
@Native public static final int SEEK_CUR = 1;
@Native public static final int SEEK_END = 2;
private static Filesystem fs(Context ctx) { private static Filesystem fs(Context ctx) {
var env = ctx.environment(); var env = ctx.environment();
if (env != null) { if (env != null) {
@ -29,24 +33,28 @@ public class FilesystemLib {
throw EngineException.ofError("Current environment doesn't have a file system."); throw EngineException.ofError("Current environment doesn't have a file system.");
} }
@Native public static String normalize(Context ctx, String... paths) {
return fs(ctx).normalize(paths);
}
@Native public static PromiseLib open(Context ctx, String _path, String mode) { @Native public static PromiseLib open(Context ctx, String _path, String mode) {
var filename = Filename.parse(_path); var path = fs(ctx).normalize(_path);
var _mode = Mode.parse(mode); var _mode = Mode.parse(mode);
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
if (fs(ctx).stat(filename.path).type != EntryType.FILE) { if (fs(ctx).stat(path).type != EntryType.FILE) {
throw new FilesystemException(filename.toString(), FSCode.NOT_FILE); throw new FilesystemException(path, FSCode.NOT_FILE);
} }
var file = fs(ctx).open(filename.path, _mode); var file = fs(ctx).open(path, _mode);
return new FileLib(file); return new FileLib(file);
} }
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });
} }
@Native public static ObjectValue ls(Context ctx, String _path) throws IOException { @Native public static ObjectValue ls(Context ctx, String _path) throws IOException {
var filename = Filename.parse(_path); var path = fs(ctx).normalize(_path);
return Values.toJSAsyncIterator(ctx, new Iterator<>() { return Values.toJSAsyncIterator(ctx, new Iterator<>() {
private boolean failed, done; private boolean failed, done;
@ -57,11 +65,11 @@ public class FilesystemLib {
if (done) return; if (done) return;
if (!failed) { if (!failed) {
if (file == null) { if (file == null) {
if (fs(ctx).stat(filename.path).type != EntryType.FOLDER) { if (fs(ctx).stat(path).type != EntryType.FOLDER) {
throw new FilesystemException(filename.toString(), FSCode.NOT_FOLDER); throw new FilesystemException(path, FSCode.NOT_FOLDER);
} }
file = fs(ctx).open(filename.path, Mode.READ); file = fs(ctx).open(path, Mode.READ);
} }
if (nextLine == null) { if (nextLine == null) {
@ -108,34 +116,34 @@ public class FilesystemLib {
}); });
} }
@Native public static PromiseLib mkfile(Context ctx, String _path) throws IOException { @Native public static PromiseLib mkfile(Context ctx, String path) throws IOException {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
fs(ctx).create(Filename.parse(_path).toString(), EntryType.FILE); fs(ctx).create(path, EntryType.FILE);
return null; return null;
} }
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });
} }
@Native public static PromiseLib rm(Context ctx, String _path, boolean recursive) throws IOException { @Native public static PromiseLib rm(Context ctx, String path, boolean recursive) throws IOException {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
if (!recursive) fs(ctx).create(Filename.parse(_path).toString(), EntryType.NONE); if (!recursive) fs(ctx).create(path, EntryType.NONE);
else { else {
var stack = new Stack<String>(); var stack = new Stack<String>();
stack.push(_path); stack.push(path);
while (!stack.empty()) { while (!stack.empty()) {
var path = Filename.parse(stack.pop()).toString(); var currPath = stack.pop();
FileStat stat; FileStat stat;
try { stat = fs(ctx).stat(path); } try { stat = fs(ctx).stat(currPath); }
catch (FilesystemException e) { continue; } catch (FilesystemException e) { continue; }
if (stat.type == EntryType.FOLDER) { if (stat.type == EntryType.FOLDER) {
for (var el : fs(ctx).open(path, Mode.READ).readToString().split("\n")) stack.push(el); for (var el : fs(ctx).open(currPath, Mode.READ).readToString().split("\n")) stack.push(el);
} }
else fs(ctx).create(path, EntryType.NONE); else fs(ctx).create(currPath, EntryType.NONE);
} }
} }
return null; return null;
@ -143,10 +151,10 @@ public class FilesystemLib {
catch (FilesystemException e) { throw e.toEngineException(); } catch (FilesystemException e) { throw e.toEngineException(); }
}); });
} }
@Native public static PromiseLib stat(Context ctx, String _path) throws IOException { @Native public static PromiseLib stat(Context ctx, String path) throws IOException {
return PromiseLib.await(ctx, () -> { return PromiseLib.await(ctx, () -> {
try { try {
var stat = fs(ctx).stat(_path); var stat = fs(ctx).stat(path);
var res = new ObjectValue(); var res = new ObjectValue();
res.defineProperty(ctx, "type", stat.type.name); res.defineProperty(ctx, "type", stat.type.name);