file_read: remove max size limit, use islice for streaming reads
The 8MB size check caused models to fall back to cat which reads the entire file without truncation. Now uses itertools.islice to read only the requested segment without loading the whole file into memory. Also: sandbox config additions (pcfs, ~/img).
This commit is contained in:
parent
ba8f7948bb
commit
d7a6d7b8c3
|
|
@ -27,6 +27,7 @@ filesystem:
|
|||
- "{XDG_CONFIG_HOME}/gh"
|
||||
- "{XDG_DATA_HOME}/gh/extensions"
|
||||
- "/dev"
|
||||
- "{HOME}/img"
|
||||
rw:
|
||||
- "{OLLIE_MEMORY_PATH}"
|
||||
- "{OLLIE_PLAN_PATH}"
|
||||
|
|
@ -75,6 +76,7 @@ filesystem:
|
|||
- "/dev/random"
|
||||
- "/dev/null"
|
||||
- "/dev/fuse"
|
||||
- "{PCFS}"
|
||||
rox:
|
||||
- "/usr"
|
||||
- "/lib"
|
||||
|
|
@ -135,5 +137,6 @@ env:
|
|||
- EDITOR
|
||||
- GIT_SEQUENCE_EDITOR
|
||||
- SUPERPOWERD_SESSION_TOKEN
|
||||
- PCFS
|
||||
network:
|
||||
unrestricted: true
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@
|
|||
|
||||
import sys
|
||||
import os
|
||||
from itertools import islice
|
||||
|
||||
MAX_LINES = 2000
|
||||
MAX_LINE_LEN = 2000
|
||||
MAX_SIZE = 8 * 1024 * 1024
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("usage: file_read <file_path> [start] [end]")
|
||||
|
|
@ -30,24 +30,21 @@ if not os.path.isfile(file_path):
|
|||
print(f"error: not a regular file: {file_path}")
|
||||
sys.exit(1)
|
||||
|
||||
size = os.path.getsize(file_path)
|
||||
if size > MAX_SIZE:
|
||||
print(f"error: file too large ({size} bytes, max {MAX_SIZE})")
|
||||
sys.exit(1)
|
||||
|
||||
with open(file_path, 'rb') as f:
|
||||
if b'\x00' in f.read(8192):
|
||||
print("(binary file)")
|
||||
sys.exit(0)
|
||||
|
||||
with open(file_path, 'r', errors='replace') as f:
|
||||
all_lines = f.readlines()
|
||||
|
||||
total = len(all_lines)
|
||||
start = max(1, start)
|
||||
end = min(end, total) if end is not None else min(total, start + MAX_LINES - 1)
|
||||
if end is None:
|
||||
end = start + MAX_LINES - 1
|
||||
|
||||
segment_lines = all_lines[start - 1:end]
|
||||
with open(file_path, 'r', errors='replace') as f:
|
||||
# Skip lines before start
|
||||
for _ in islice(f, start - 1):
|
||||
pass
|
||||
# Read only the lines we need
|
||||
segment_lines = list(islice(f, end - start + 1))
|
||||
|
||||
if not segment_lines:
|
||||
print("(empty file)" if start == 1 else "(no lines in range)")
|
||||
|
|
|
|||
Loading…
Reference in New Issue