withFile calls f on the file at join(dir, rel), protecting against path traversal attacks.
(dir, rel string, f func(*os.File) error)
| 116 | // withFile calls f on the file at join(dir, rel), |
| 117 | // protecting against path traversal attacks. |
| 118 | func withFile(dir, rel string, f func(*os.File) error) (err error) { |
| 119 | r, err := os.OpenRoot(dir) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | defer r.Close() |
| 124 | file, err := r.Open(rel) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | // Record error, in case f writes. |
| 129 | defer func() { err = errors.Join(err, file.Close()) }() |
| 130 | return f(file) |
| 131 | } |
| 132 | |
| 133 | // fileRoots transforms the Roots obtained from the client into absolute paths on |
| 134 | // the local filesystem. |
no test coverage detected
searching dependent graphs…