Skip to content

Commit 72cadd9

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

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

cli/command/utils.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,23 @@ import (
1919
mounttypes "github.com/docker/docker/api/types/mount"
2020
"github.com/docker/docker/api/types/versions"
2121
"github.com/docker/docker/errdefs"
22-
"github.com/moby/sys/sequential"
22+
"github.com/docker/docker/pkg/atomicwriter"
2323
"github.com/moby/term"
2424
"github.com/pkg/errors"
2525
"github.com/spf13/pflag"
2626
)
2727

2828
// CopyToFile writes the content of the reader to the specified file
29+
//
30+
// Deprecated: use [atomicwriter.New].
2931
func CopyToFile(outfile string, r io.Reader) error {
30-
// We use sequential file access here to avoid depleting the standby list
31-
// on Windows. On Linux, this is a call directly to os.CreateTemp
32-
tmpFile, err := sequential.CreateTemp(filepath.Dir(outfile), ".docker_temp_")
33-
if err != nil {
34-
return err
35-
}
36-
37-
tmpPath := tmpFile.Name()
38-
39-
_, err = io.Copy(tmpFile, r)
40-
tmpFile.Close()
41-
32+
writer, err := atomicwriter.New(outfile, 0o600)
4233
if err != nil {
43-
os.Remove(tmpPath)
44-
return err
45-
}
46-
47-
if err = os.Rename(tmpPath, outfile); err != nil {
48-
os.Remove(tmpPath)
4934
return err
5035
}
51-
52-
return nil
36+
defer writer.Close()
37+
_, err = io.Copy(writer, r)
38+
return err
5339
}
5440

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

0 commit comments

Comments
 (0)