From 61a8c3d69eeced1570af668f2c4ce5c6821e342f Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 27 Mar 2024 14:13:01 +0900 Subject: [PATCH] go 1.21 defense Signed-off-by: Adrian Cole --- docker.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docker.go b/docker.go index 1366b0c480..c946103556 100644 --- a/docker.go +++ b/docker.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io" + "io/fs" "net/url" "os" "path/filepath" @@ -602,20 +603,27 @@ func (c *DockerContainer) CopyFileToContainer(ctx context.Context, hostFilePath return c.CopyDirToContainer(ctx, hostFilePath, containerFilePath, fileMode) } - file, err := os.Open(hostFilePath) + f, err := os.Open(hostFilePath) if err != nil { return err } - defer file.Close() + defer f.Close() - info, err := file.Stat() + info, err := f.Stat() if err != nil { return err } + // In Go 1.22 os.File is always an io.WriterTo. However, testcontainers + // currently allows Go 1.21, so we need to trick the compiler a little. + var file fs.File = f return c.copyToContainer(ctx, func(tw io.Writer) error { // Attempt optimized writeTo, implemented in linux - _, err := file.WriteTo(tw) + if wt, ok := file.(io.WriterTo); ok { + _, err := wt.WriteTo(tw) + return err + } + _, err := io.Copy(tw, f) return err }, info.Size(), containerFilePath, fileMode) }