Bladeren bron

pkg/ioutils: TempDir: move to pkg/longpath

This utility wasn't very related to all other utilities in pkg/ioutils.
Moving it to longpath to also make it more clear what it does.

It looks like there's only a single (public) external consumer of this
utility, and only used in a test, and it's not 100% clear if it was
intentional to use our package, of if it was a case of "I actually meant
`io/ioutil.MkdirTemp`" so we could consider skipping the alias.

While moving the package, I also renamed `TempDir` to `MkdirTemp`, which
is the signature it matches in "os" from stdlib.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 jaren geleden
bovenliggende
commit
c63ea32a17

+ 2 - 2
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
 	}

+ 2 - 2
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
 	}

+ 2 - 2
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
 	}

+ 2 - 6
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 {

+ 0 - 22
pkg/ioutils/tempdir.go

@@ -1,22 +0,0 @@
-package ioutils // import "github.com/docker/docker/pkg/ioutils"
-
-import (
-	"os"
-	"runtime"
-
-	"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].
-func TempDir(dir, prefix string) (string, error) {
-	tempDir, err := os.MkdirTemp(dir, prefix)
-	if err != nil {
-		return "", err
-	}
-	if runtime.GOOS != "windows" {
-		return tempDir, nil
-	}
-	return longpath.AddPrefix(tempDir), nil
-}

+ 10 - 0
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

+ 16 - 0
pkg/longpath/longpath.go

@@ -6,6 +6,8 @@
 package longpath // import "github.com/docker/docker/pkg/longpath"
 
 import (
+	"os"
+	"runtime"
 	"strings"
 )
 
@@ -25,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
+}