| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | |
| 10 | fileenc "reasonix/internal/fileutil/encoding" |
| 11 | "reasonix/internal/tool" |
| 12 | ) |
| 13 | |
| 14 | func init() { tool.RegisterBuiltin(writeFile{}) } |
| 15 | |
| 16 | // writeFile writes a file. roots, when non-empty, confines the target to the |
| 17 | // workspace (see confine); guard rejects Reasonix session-data targets even |
| 18 | // inside the roots (see SessionDataGuard); the zero value registered at init is |
| 19 | // unconfined and is overridden per run by ConfineWriters. workDir, when |
| 20 | // non-empty, is the directory a relative path resolves against (see resolveIn). |
| 21 | type writeFile struct { |
| 22 | roots []string |
| 23 | guard SessionDataGuard |
| 24 | managed ManagedConfigPaths |
| 25 | workDir string |
| 26 | // overlay, when non-nil, routes the write through the host transport so an |
| 27 | // open editor buffer updates too. Consulted only after write confinement, |
| 28 | // and only for plain-UTF-8 targets (the overlay is text-only, so non-UTF-8 |
| 29 | // files keep the local encoding-preserving path). |
| 30 | overlay FileOverlay |
| 31 | } |
| 32 | |
| 33 | func (writeFile) Name() string { return "write_file" } |
| 34 | |
| 35 | func (writeFile) Description() string { |
| 36 | return "Write content to a file at the given path (overwriting existing content). Creates parent directories as needed." |
| 37 | } |
| 38 | |
| 39 | func (writeFile) Schema() json.RawMessage { |
| 40 | return json.RawMessage(`{"type":"object","properties":{"path":{"type":"string","description":"File path"},"content":{"type":"string","description":"Full content to write"}},"required":["path","content"]}`) |
| 41 | } |
| 42 | |
| 43 | func (writeFile) ReadOnly() bool { return false } |
| 44 | |
| 45 | func (w writeFile) Execute(ctx context.Context, args json.RawMessage) (string, error) { |
| 46 | var p struct { |
| 47 | Path string `json:"path"` |
| 48 | Content string `json:"content"` |
| 49 | } |
| 50 | if err := json.Unmarshal(args, &p); err != nil { |
| 51 | return "", fmt.Errorf("invalid args: %w", err) |
| 52 | } |
| 53 | if p.Path == "" { |
| 54 | return "", fmt.Errorf("path is required") |
| 55 | } |
| 56 | p.Path = resolveIn(w.workDir, p.Path) |
| 57 | if err := confineWrite(ctx, w.roots, w.guard, w.managed, p.Path); err != nil { |
| 58 | return "", err |
| 59 | } |
| 60 | // Preserve the existing file's encoding (GBK/UTF-16/BOM) on overwrite instead |
| 61 | // of always writing UTF-8, which would silently corrupt a non-UTF-8 file. |
| 62 | // readFileEncoded returns enc=UTF8 for a missing file — the right default for |
| 63 | // a newly created one. |
| 64 | existing, enc, rerr := readFileEncoded(p.Path) |
| 65 | if rerr == nil && existing == p.Content { |
| 66 | return fmt.Sprintf("%s already contains the exact content; no changes made", p.Path), nil |
| 67 | } |
| 68 | // The host overlay applies the write to the editor buffer and the file in |
| 69 | // one step. Text-only, so it handles plain UTF-8 targets (and new files); |
| 70 | // non-UTF-8 files stay on the local encoding-preserving path below. |
| 71 | if w.overlay != nil && filepath.IsAbs(p.Path) && (rerr != nil || enc == fileenc.UTF8) { |
| 72 | if ok, werr := w.overlay.WriteTextFile(ctx, p.Path, p.Content); ok { |
| 73 | if werr != nil { |
| 74 | return "", fmt.Errorf("write %s: %w", p.Path, werr) |
| 75 | } |
| 76 | return fmt.Sprintf("wrote %d bytes to %s", len(p.Content), p.Path), nil |
| 77 | } |
| 78 | } |
| 79 | if dir := filepath.Dir(p.Path); dir != "" && dir != "." { |
| 80 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 81 | return "", fmt.Errorf("mkdir %s: %w", dir, err) |
| 82 | } |
| 83 | } |
| 84 | if err := writeFileEncoded(p.Path, p.Content, enc); err != nil { |
| 85 | return "", fmt.Errorf("write %s: %w", p.Path, err) |
| 86 | } |
| 87 | return fmt.Sprintf("wrote %d bytes to %s", len(p.Content), p.Path), nil |
| 88 | } |
| 89 |