Skip to content

Commit f13896b

Browse files
committed
Clean up temp file if copy fails
1 parent c63c4a9 commit f13896b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

agent/files.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (a *agent) HandleEditFile(rw http.ResponseWriter, r *http.Request) {
190190
return
191191
}
192192

193-
status, err := a.editFile(path, edits.Edits)
193+
status, err := a.editFile(r.Context(), path, edits.Edits)
194194
if err != nil {
195195
httpapi.Write(ctx, rw, status, codersdk.Response{
196196
Message: err.Error(),
@@ -203,7 +203,7 @@ func (a *agent) HandleEditFile(rw http.ResponseWriter, r *http.Request) {
203203
})
204204
}
205205

206-
func (a *agent) editFile(path string, edits []workspacesdk.FileEdit) (int, error) {
206+
func (a *agent) editFile(ctx context.Context, path string, edits []workspacesdk.FileEdit) (int, error) {
207207
if !filepath.IsAbs(path) {
208208
return http.StatusBadRequest, xerrors.Errorf("file path must be absolute: %q", path)
209209
}
@@ -247,6 +247,9 @@ func (a *agent) editFile(path string, edits []workspacesdk.FileEdit) (int, error
247247

248248
_, err = io.Copy(tmpfile, replace.Chain(f, transforms...))
249249
if err != nil {
250+
if rerr := a.filesystem.Remove(tmpfile.Name()); rerr != nil {
251+
a.logger.Warn(ctx, "unable to clean up temp file", slog.Error(rerr))
252+
}
250253
return http.StatusInternalServerError, xerrors.Errorf("edit %s: %w", path, err)
251254
}
252255

0 commit comments

Comments
 (0)