|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// hostToContainerPath translates a host path (relative or absolute) into the |
| 12 | +// corresponding path inside a sandbox container. The sandbox mounts the working |
| 13 | +// directory at /workspace, so any path under the host working directory becomes |
| 14 | +// /workspace/<rel>. Paths outside the working directory are rejected. |
| 15 | +func hostToContainerPath(hostPath string) (string, error) { |
| 16 | + if hostPath == "" { |
| 17 | + return "", fmt.Errorf("path is empty") |
| 18 | + } |
| 19 | + |
| 20 | + cwd, err := os.Getwd() |
| 21 | + if err != nil { |
| 22 | + return "", fmt.Errorf("cannot determine working directory: %w", err) |
| 23 | + } |
| 24 | + // Resolve symlinks in the working directory so comparisons work on hosts |
| 25 | + // where the cwd is reached through a symlink (e.g. macOS /var -> /private/var). |
| 26 | + evalCwd, err := filepath.EvalSymlinks(cwd) |
| 27 | + if err != nil { |
| 28 | + return "", fmt.Errorf("cannot resolve working directory symlinks: %w", err) |
| 29 | + } |
| 30 | + evalCwd, err = filepath.Abs(evalCwd) |
| 31 | + if err != nil { |
| 32 | + return "", fmt.Errorf("cannot resolve working directory: %w", err) |
| 33 | + } |
| 34 | + evalCwd = filepath.Clean(evalCwd) |
| 35 | + |
| 36 | + absHost := hostPath |
| 37 | + if !filepath.IsAbs(absHost) { |
| 38 | + absHost = filepath.Join(evalCwd, absHost) |
| 39 | + } |
| 40 | + absHost, err = filepath.Abs(absHost) |
| 41 | + if err != nil { |
| 42 | + return "", fmt.Errorf("cannot resolve path %q: %w", hostPath, err) |
| 43 | + } |
| 44 | + absHost = filepath.Clean(absHost) |
| 45 | + |
| 46 | + // Resolve symlinks in the host path. If the path does not exist yet, |
| 47 | + // resolve its directory and keep the final component. |
| 48 | + evalHost := absHost |
| 49 | + if ev, err := filepath.EvalSymlinks(absHost); err == nil { |
| 50 | + evalHost = filepath.Clean(ev) |
| 51 | + } else { |
| 52 | + dir := filepath.Dir(absHost) |
| 53 | + base := filepath.Base(absHost) |
| 54 | + if evDir, err := filepath.EvalSymlinks(dir); err == nil { |
| 55 | + evalHost = filepath.Join(evDir, base) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if evalHost != evalCwd && !strings.HasPrefix(evalHost, evalCwd+string(filepath.Separator)) { |
| 60 | + return "", fmt.Errorf("path %q is outside working directory %q", hostPath, evalCwd) |
| 61 | + } |
| 62 | + |
| 63 | + rel, err := filepath.Rel(evalCwd, evalHost) |
| 64 | + if err != nil { |
| 65 | + return "", fmt.Errorf("cannot relativise %q: %w", hostPath, err) |
| 66 | + } |
| 67 | + if rel == "." { |
| 68 | + return "/workspace", nil |
| 69 | + } |
| 70 | + return "/workspace/" + filepath.ToSlash(rel), nil |
| 71 | +} |
| 72 | + |
| 73 | +// sandboxWriteFile writes data to a path inside a running sandbox container. |
| 74 | +// It is used by the file tools when sandbox mode is active so that writes go |
| 75 | +// through the container's filesystem view and respect its mount options (e.g. |
| 76 | +// a read-only /workspace mount will cause this to fail). |
| 77 | +// |
| 78 | +// The implementation writes the content to a temporary file on the host and |
| 79 | +// then copies it into the container with `docker cp`, creating parent |
| 80 | +// directories with `docker exec mkdir -p` first. |
| 81 | +func sandboxWriteFile(containerName, hostPath string, data []byte, mode os.FileMode) error { |
| 82 | + containerPath, err := hostToContainerPath(hostPath) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + // Write content to a host temp file. docker cp needs a real file path. |
| 88 | + tmpFile, err := os.CreateTemp("", "odek-sandbox-write-*") |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("cannot create temp file: %w", err) |
| 91 | + } |
| 92 | + tmpPath := tmpFile.Name() |
| 93 | + removeTmp := true |
| 94 | + defer func() { |
| 95 | + if removeTmp { |
| 96 | + os.Remove(tmpPath) |
| 97 | + } |
| 98 | + }() |
| 99 | + |
| 100 | + if _, err := tmpFile.Write(data); err != nil { |
| 101 | + tmpFile.Close() |
| 102 | + return fmt.Errorf("cannot write temp file: %w", err) |
| 103 | + } |
| 104 | + if err := tmpFile.Chmod(mode); err != nil { |
| 105 | + tmpFile.Close() |
| 106 | + return fmt.Errorf("cannot chmod temp file: %w", err) |
| 107 | + } |
| 108 | + if err := tmpFile.Close(); err != nil { |
| 109 | + return fmt.Errorf("cannot close temp file: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Ensure parent directories exist inside the container. |
| 113 | + parent := filepath.ToSlash(filepath.Dir(containerPath)) |
| 114 | + if parent != "/workspace" && parent != "/" { |
| 115 | + mkdir := exec.Command("docker", "exec", containerName, "mkdir", "-p", parent) |
| 116 | + if out, err := mkdir.CombinedOutput(); err != nil { |
| 117 | + return fmt.Errorf("cannot create parent dir %s in container: %w\n%s", parent, err, string(out)) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Copy the temp file into the container. |
| 122 | + cp := exec.Command("docker", "cp", tmpPath, containerName+":"+containerPath) |
| 123 | + if out, err := cp.CombinedOutput(); err != nil { |
| 124 | + return fmt.Errorf("cannot copy file to container: %w\n%s", err, string(out)) |
| 125 | + } |
| 126 | + |
| 127 | + removeTmp = false |
| 128 | + os.Remove(tmpPath) |
| 129 | + return nil |
| 130 | +} |
0 commit comments