Skip to content

Commit b59fee1

Browse files
author
lololol
committed
fix: let file_read return how much is left of a file
1 parent 7e523af commit b59fee1

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

tools/filetool.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def file_read(filename: Annotated[str, "Name of the file to read"], start: Annot
4646
print("warn: os.path.isfile('%s') is False" % realpath)
4747
return "cannot open file (this is not a file, maybe a directory?)"
4848
with open(realpath,"r",encoding="utf-8",errors="replace") as f:
49+
f.seek(0,2)
50+
total_bytes = f.tell()
4951
f.seek(start)
5052
if bytes == -1:
5153
print("info: file_read asked to get -1 bytes, reading all")
@@ -55,12 +57,15 @@ def file_read(filename: Annotated[str, "Name of the file to read"], start: Annot
5557
if len(data) >= MAX_DATA:
5658
return "error: trying to return %d bytes, can only read %d at once" % (len(data),MAX_DATA)
5759
else:
58-
return data
60+
status_str = "ok, read %d out of %d bytes, %d remaining\n" % (len(data),total_bytes - start,total_bytes - start - len(data))
61+
return status_str + data
5962
else:
6063
new_realpath = expanduser(normpath(FN_PREFIX + "/" + realpath))
6164
if os.path.isfile(new_realpath):
6265
print("info: file_read converted relative to absolute path")
6366
with open(realpath) as f:
67+
f.seek(0,2)
68+
total_bytes = f.tell()
6469
f.seek(start)
6570
if bytes == -1:
6671
print("info: file_read asked to get -1 bytes, reading all")
@@ -70,7 +75,8 @@ def file_read(filename: Annotated[str, "Name of the file to read"], start: Annot
7075
if len(data) >= MAX_DATA:
7176
return "error: trying to return %d bytes, can only read %d at once" % (len(data),MAX_DATA)
7277
else:
73-
return data
78+
status_str = "ok, read %d out of %d bytes, %d remaining\n" % (len(data),total_bytes - start,total_bytes - start - len(data))
79+
return status_str + data
7480
else:
7581
print("warn: read from '%s' blocked by sandbox" % filename)
7682
return "error: cannot read file, no permission"

0 commit comments

Comments
 (0)