diff --git a/builder/builder-next/adapters/snapshot/layer.go b/builder/builder-next/adapters/snapshot/layer.go index ed13def16f..e606a8b472 100644 --- a/builder/builder-next/adapters/snapshot/layer.go +++ b/builder/builder-next/adapters/snapshot/layer.go @@ -6,7 +6,7 @@ import ( "path/filepath" "github.com/docker/docker/layer" - "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/longpath" "github.com/pkg/errors" bolt "go.etcd.io/bbolt" "golang.org/x/sync/errgroup" @@ -55,7 +55,7 @@ func (s *snapshotter) EnsureLayer(ctx context.Context, key string) ([]layer.Diff }) } - tmpDir, err := ioutils.TempDir("", "docker-tarsplit") + tmpDir, err := longpath.MkdirTemp("", "docker-tarsplit") if err != nil { return nil, err } diff --git a/builder/dockerfile/copy.go b/builder/dockerfile/copy.go index a69deb0f36..0a994067fc 100644 --- a/builder/dockerfile/copy.go +++ b/builder/dockerfile/copy.go @@ -19,7 +19,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/containerfs" "github.com/docker/docker/pkg/idtools" - "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/longpath" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/system" @@ -390,7 +390,7 @@ func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote b filename := getFilenameForDownload(u.Path, resp) // Prepare file in a tmp dir - tmpDir, err := ioutils.TempDir("", "docker-remote") + tmpDir, err := longpath.MkdirTemp("", "docker-remote") if err != nil { return } diff --git a/builder/remotecontext/archive.go b/builder/remotecontext/archive.go index 9d89329040..bfe937b9aa 100644 --- a/builder/remotecontext/archive.go +++ b/builder/remotecontext/archive.go @@ -9,7 +9,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/chrootarchive" "github.com/docker/docker/pkg/containerfs" - "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/longpath" "github.com/docker/docker/pkg/tarsum" "github.com/pkg/errors" ) @@ -47,7 +47,7 @@ type modifiableContext interface { // // Closing tarStream has to be done by the caller. func FromArchive(tarStream io.Reader) (builder.Source, error) { - root, err := ioutils.TempDir("", "docker-builder") + root, err := longpath.MkdirTemp("", "docker-builder") if err != nil { return nil, err } diff --git a/container/view.go b/container/view.go index 3c48644946..aa32536694 100644 --- a/container/view.go +++ b/container/view.go @@ -138,16 +138,12 @@ func (db *ViewDB) GetByPrefix(s string) (string, error) { if s == "" { return "", ErrEmptyPrefix } - txn := db.store.Txn(false) - iter, err := txn.Get(memdbContainersTable, memdbIDIndexPrefix, s) + iter, err := db.store.Txn(false).Get(memdbContainersTable, memdbIDIndexPrefix, s) if err != nil { return "", err } - var ( - id string - ) - + var id string for { item := iter.Next() if item == nil { diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 0b6ad7f768..672f43c0ca 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -26,7 +26,6 @@ import ( "github.com/docker/docker/errdefs" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/testutil/request" "github.com/docker/docker/volume" @@ -1738,7 +1737,7 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsValidation(c *testing.T) { } if testEnv.IsLocalDaemon() { - tmpDir, err := ioutils.TempDir("", "test-mounts-api") + tmpDir, err := os.MkdirTemp("", "test-mounts-api") assert.NilError(c, err) defer os.RemoveAll(tmpDir) cases = append(cases, []testCase{ @@ -2051,7 +2050,7 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsCreate(c *testing.T) { // for modes only supported on Linux if DaemonIsLinux() { - tmpDir3, err := ioutils.TempDir("", "test-mounts-api-3") + tmpDir3, err := os.MkdirTemp("", "test-mounts-api-3") assert.NilError(c, err) defer os.RemoveAll(tmpDir3) diff --git a/pkg/ioutils/temp_unix.go b/pkg/ioutils/temp_unix.go deleted file mode 100644 index 7489122309..0000000000 --- a/pkg/ioutils/temp_unix.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build !windows -// +build !windows - -package ioutils // import "github.com/docker/docker/pkg/ioutils" - -import "os" - -// TempDir on Unix systems is equivalent to os.MkdirTemp. -func TempDir(dir, prefix string) (string, error) { - return os.MkdirTemp(dir, prefix) -} diff --git a/pkg/ioutils/temp_windows.go b/pkg/ioutils/temp_windows.go deleted file mode 100644 index a57fd9af6a..0000000000 --- a/pkg/ioutils/temp_windows.go +++ /dev/null @@ -1,16 +0,0 @@ -package ioutils // import "github.com/docker/docker/pkg/ioutils" - -import ( - "os" - - "github.com/docker/docker/pkg/longpath" -) - -// TempDir is the equivalent of os.MkdirTemp, except that the result is in Windows longpath format. -func TempDir(dir, prefix string) (string, error) { - tempDir, err := os.MkdirTemp(dir, prefix) - if err != nil { - return "", err - } - return longpath.AddPrefix(tempDir), nil -} diff --git a/pkg/ioutils/tempdir_deprecated.go b/pkg/ioutils/tempdir_deprecated.go new file mode 100644 index 0000000000..b3321602c2 --- /dev/null +++ b/pkg/ioutils/tempdir_deprecated.go @@ -0,0 +1,10 @@ +package ioutils + +import "github.com/docker/docker/pkg/longpath" + +// TempDir is the equivalent of [os.MkdirTemp], except that on Windows +// the result is in Windows longpath format. On Unix systems it is +// equivalent to [os.MkdirTemp]. +// +// Deprecated: use [longpath.MkdirTemp]. +var TempDir = longpath.MkdirTemp diff --git a/pkg/longpath/longpath.go b/pkg/longpath/longpath.go index 4177affba2..1c5dde5218 100644 --- a/pkg/longpath/longpath.go +++ b/pkg/longpath/longpath.go @@ -1,17 +1,20 @@ -// longpath introduces some constants and helper functions for handling long paths -// in Windows, which are expected to be prepended with `\\?\` and followed by either -// a drive letter, a UNC server\share, or a volume identifier. - +// Package longpath introduces some constants and helper functions for handling +// long paths in Windows. +// +// Long paths are expected to be prepended with "\\?\" and followed by either a +// drive letter, a UNC server\share, or a volume identifier. package longpath // import "github.com/docker/docker/pkg/longpath" import ( + "os" + "runtime" "strings" ) // Prefix is the longpath prefix for Windows file paths. const Prefix = `\\?\` -// AddPrefix will add the Windows long path prefix to the path provided if +// AddPrefix adds the Windows long path prefix to the path provided if // it does not already have it. func AddPrefix(path string) string { if !strings.HasPrefix(path, Prefix) { @@ -24,3 +27,17 @@ func AddPrefix(path string) string { } return path } + +// MkdirTemp is the equivalent of [os.MkdirTemp], except that on Windows +// the result is in Windows longpath format. On Unix systems it is +// equivalent to [os.MkdirTemp]. +func MkdirTemp(dir, prefix string) (string, error) { + tempDir, err := os.MkdirTemp(dir, prefix) + if err != nil { + return "", err + } + if runtime.GOOS != "windows" { + return tempDir, nil + } + return AddPrefix(tempDir), nil +}