Skip to content

Commit 64a2e41

Browse files
committed
cli/command: deprecate CopyToFile and reimplement with atomicwriter
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent a5ab4dc commit 64a2e41

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

cli/command/utils.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,23 @@ import (
1717
"github.com/docker/cli/cli/streams"
1818
"github.com/docker/docker/api/types/filters"
1919
"github.com/docker/docker/errdefs"
20-
"github.com/moby/sys/sequential"
20+
"github.com/docker/docker/pkg/atomicwriter"
2121
"github.com/moby/term"
2222
"github.com/pkg/errors"
2323
"github.com/spf13/pflag"
2424
)
2525

2626
// CopyToFile writes the content of the reader to the specified file
27+
//
28+
// Deprecated: use [atomicwriter.New].
2729
func CopyToFile(outfile string, r io.Reader) error {
28-
// We use sequential file access here to avoid depleting the standby list
29-
// on Windows. On Linux, this is a call directly to os.CreateTemp
30-
tmpFile, err := sequential.CreateTemp(filepath.Dir(outfile), ".docker_temp_")
31-
if err != nil {
32-
return err
33-
}
34-
35-
tmpPath := tmpFile.Name()
36-
37-
_, err = io.Copy(tmpFile, r)
38-
tmpFile.Close()
39-
30+
writer, err := atomicwriter.New(outfile, 0o600)
4031
if err != nil {
41-
os.Remove(tmpPath)
42-
return err
43-
}
44-
45-
if err = os.Rename(tmpPath, outfile); err != nil {
46-
os.Remove(tmpPath)
4732
return err
4833
}
49-
50-
return nil
34+
defer writer.Close()
35+
_, err = io.Copy(writer, r)
36+
return err
5137
}
5238

5339
var ErrPromptTerminated = errdefs.Cancelled(errors.New("prompt terminated"))

0 commit comments

Comments
 (0)