Skip to content

Commit

Permalink
go 1.21 defense
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Mar 27, 2024
1 parent 4649a1e commit 61a8c3d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 61a8c3d

Please sign in to comment.