Skip to content

Commit 7d1f7bf

Browse files
committed
cli/command: deprecate CopyToFile and reimplement with atomicwriter
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 9457ab2 commit 7d1f7bf

1 file changed

Lines changed: 10 additions & 24 deletions

File tree

cli/command/utils.go

Lines changed: 10 additions & 24 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/moby/sys/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"))
@@ -187,7 +173,7 @@ func AddPlatformFlag(flags *pflag.FlagSet, target *string) {
187173
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
188174
}
189175

190-
// ValidateOutputPath validates the output paths of the `export` and `save` commands.
176+
// ValidateOutputPath validates the output paths of the "docker cp" command.
191177
func ValidateOutputPath(path string) error {
192178
dir := filepath.Dir(filepath.Clean(path))
193179
if dir != "" && dir != "." {
@@ -213,8 +199,8 @@ func ValidateOutputPath(path string) error {
213199
return nil
214200
}
215201

216-
// ValidateOutputPathFileMode validates the output paths of the `cp` command and serves as a
217-
// helper to `ValidateOutputPath`
202+
// ValidateOutputPathFileMode validates the output paths of the "docker cp" command
203+
// and serves as a helper to [ValidateOutputPath]
218204
func ValidateOutputPathFileMode(fileMode os.FileMode) error {
219205
switch {
220206
case fileMode&os.ModeDevice != 0:

0 commit comments

Comments
 (0)