cap_files — filesystem operations
Source: cap_files.c · header: cap_files.h
cap_files is the reference filesystem capability: it exposes full read/write access to a managed FATFS directory to the LLM and automation rules, and shows how to wrap filesystem access as tools safely on embedded targets.
The default managed root is /fatfs; change it at init with cap_files_set_base_dir().
Tools exposed
Section titled “Tools exposed”| Tool ID | Purpose | Input |
|---|---|---|
read_file | Read a text file | path |
write_file | Create or overwrite a file | path, content |
edit_file | Replace the first matching substring | path, old_string, new_string |
delete_file | Delete a file | path |
list_dir | Recursively list files under the tree | optional prefix |
All tools are flagged CLAW_CAP_FLAG_CALLABLE_BY_LLM.
Path safety
Section titled “Path safety”The core guard is strict boundary checks to block path traversal:
cap_files_resolve_path accepts absolute and relative inputs:
This lets the LLM use short relative paths while keeping a hard sandbox.
Tool behavior
Section titled “Tool behavior”read_file: size cap
Section titled “read_file: size cap”Reads are capped at 32 KB (CAP_FILES_MAX_FILE_SIZE); extra bytes are truncated so huge files cannot blow the LLM context:
write_file: parent dirs
Section titled “write_file: parent dirs”write_file recursively creates missing parents—no manual mkdir:
edit_file: first match only
Section titled “edit_file: first match only”edit_file does one substring replace (strstr + memcpy), not global replace:
That gives precise edits and clear errors when old_string is wrong.
list_dir: recursion + prefix
Section titled “list_dir: recursion + prefix”Output is one absolute path per line:
Empty trees return "(no files found)" instead of a blank string so the LLM does not misread silence.
Boundary with cap_lua
Section titled “Boundary with cap_lua”cap_files and cap_lua manage different subtrees:
| Module | Tree | File types |
|---|---|---|
cap_files | /fatfs (configurable) | Any text file |
cap_lua | /fatfs/scripts (default) | .lua only |
The LLM can edit Lua with cap_files and run with cap_lua, or read .md skills managed by cap_skill.