static int fileOpBot_maxReadSize = 500000; static int fileOpBot_maxWriteSize = 500000; static S fileOpBot(S s) ctex { new Matches m; if "get user home" ret ok(userHome()); if "list directory *" ret structure(listDirAwarenessStyle(new File(assertAbsolutePath(m.unq(0))))); if "mkdir *" { S dir = assertAbsolutePath(m.unq(0)); mkdir(new File(dir)); ret new File(dir).exists() ? "ok" : "problem"; } if (matchOneOf(s, m, "rmdir *", "delete file *")) { S dir = assertAbsolutePath(m.unq(0)); new File(dir).delete(); ret !new File(dir).exists() ? "ok" : "problem"; } if "load file * bin from * l *" { File f = new File(assertAbsolutePath(m.unq(0))); long from = parseLong(m.unq(1)); int l = parseInt(m.unq(2)); if (l > fileOpBot_maxReadSize) fail(format("Can't read more than * bytes per call", fileOpBot_maxReadSize)); byte[] buf = new byte[l]; RandomAccessFile raf = new RandomAccessFile(f, "r"); try { raf.seek(from); raf.readFully(buf); ret "OK " + bytesToHex(buf); } finally { raf.close(); } } if "get file * length" { File f = new File(assertAbsolutePath(m.unq(0))); ret str(f.length()); } if "set file * length to *" { File f = new File(assertAbsolutePath(m.unq(0))); RandomAccessFile raf = new RandomAccessFile(f, "rw"); try { raf.setLength(parseLong(m.unq(1))); ret "OK"; } finally { raf.close(); } } if "write file * bin from * data *" { print(shorten(s, 200)); File f = new File(assertAbsolutePath(m.unq(0))); long from = parseLong(m.unq(1)); byte[] data = hexToBytes(m.unq(2)); if (l(data) > fileOpBot_maxWriteSize) fail(format("Can't write more than * bytes per call", fileOpBot_maxWriteSize)); RandomAccessFile raf = new RandomAccessFile(f, "rw"); try { raf.seek(from); raf.write(data); ret "OK"; } finally { raf.close(); } } null; }