@@ -26,9 +26,10 @@ import (
2626 v1 "github.com/opencontainers/image-spec/specs-go/v1"
2727 "github.com/opencontainers/runtime-spec/specs-go"
2828 "github.com/sirupsen/logrus"
29- "github.com/tonistiigi/dchapes-mode"
29+ mode "github.com/tonistiigi/dchapes-mode"
3030 "go.podman.io/buildah/copier"
3131 "go.podman.io/buildah/define"
32+ "go.podman.io/buildah/internal/ctxreader"
3233 "go.podman.io/buildah/internal/tmpdir"
3334 "go.podman.io/buildah/internal/urlsource"
3435 "go.podman.io/buildah/pkg/chrootuser"
@@ -129,7 +130,7 @@ type AddAndCopyOptions struct {
129130}
130131
131132// getURL writes a tar archive containing the named content
132- func getURL (src string , chown * idtools.IDPair , mountpoint , renameTarget string , writer io.Writer , chmod string , srcDigest digest.Digest , certPath string , insecureSkipTLSVerify types.OptionalBool , timestamp * time.Time ) error {
133+ func getURL (ctx context. Context , src string , chown * idtools.IDPair , mountpoint , renameTarget string , writer io.Writer , chmod string , srcDigest digest.Digest , certPath string , insecureSkipTLSVerify types.OptionalBool , timestamp * time.Time ) error {
133134 url , err := url .Parse (src )
134135 if err != nil {
135136 return err
@@ -152,7 +153,11 @@ func getURL(src string, chown *idtools.IDPair, mountpoint, renameTarget string,
152153 Proxy : http .ProxyFromEnvironment ,
153154 }
154155 httpClient := & http.Client {Transport : tr }
155- response , err := httpClient .Get (src )
156+ req , err := http .NewRequestWithContext (ctx , "GET" , src , nil )
157+ if err != nil {
158+ return err
159+ }
160+ response , err := httpClient .Do (req )
156161 if err != nil {
157162 return err
158163 }
@@ -184,7 +189,7 @@ func getURL(src string, chown *idtools.IDPair, mountpoint, renameTarget string,
184189 }
185190 // Figure out the size of the content.
186191 size := response .ContentLength
187- var responseBody io. Reader = response .Body
192+ responseBody := ctxreader . NewCancelableReader ( ctx , response .Body )
188193 if size < 0 {
189194 // Create a temporary file and copy the content to it, so that
190195 // we can figure out how much content there is.
@@ -307,10 +312,15 @@ func getParentsPrefixToRemoveAndParentsToSkip(pattern string, contextDir string)
307312 return prefix , out
308313}
309314
310- // Add copies the contents of the specified sources into the container's root
315+ // Add() calls AddContext() with context.Background().
316+ func (b * Builder ) Add (destination string , extract bool , options AddAndCopyOptions , sources ... string ) error {
317+ return b .AddContext (context .Background (), destination , extract , options , sources ... )
318+ }
319+
320+ // AddContext copies the contents of the specified sources into the container's root
311321// filesystem, optionally extracting contents of local files that look like
312322// non-empty archives.
313- func (b * Builder ) Add ( destination string , extract bool , options AddAndCopyOptions , sources ... string ) error {
323+ func (b * Builder ) AddContext ( ctx context. Context , destination string , extract bool , options AddAndCopyOptions , sources ... string ) error {
314324 mountPoint , err := b .Mount (b .MountLabel )
315325 if err != nil {
316326 return err
@@ -374,7 +384,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
374384 DisallowWildcard : options .AllowWildcard == types .OptionalBoolFalse ,
375385 AllowEmptyWildcard : options .AllowEmptyWildcard == types .OptionalBoolTrue ,
376386 }
377- localSourceStats , err = copier .Stat ( contextDir , contextDir , statOptions , localSources )
387+ localSourceStats , err = copier .StatContext ( ctx , contextDir , contextDir , statOptions , localSources )
378388 if err != nil {
379389 return fmt .Errorf ("checking on sources under %q: %w" , contextDir , err )
380390 }
@@ -466,7 +476,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
466476 statOptions := copier.StatOptions {
467477 CheckForArchives : extract ,
468478 }
469- destStats , err := copier .Stat ( mountPoint , filepath .Join (mountPoint , b .WorkDir ()), statOptions , []string {extractDirectory })
479+ destStats , err := copier .StatContext ( ctx , mountPoint , filepath .Join (mountPoint , b .WorkDir ()), statOptions , []string {extractDirectory })
470480 if err != nil {
471481 return fmt .Errorf ("checking on destination %v: %w" , extractDirectory , err )
472482 }
@@ -559,7 +569,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
559569 if ! strings .HasPrefix (putDirAbs , stagingDirAbs + string (os .PathSeparator )) && putDirAbs != stagingDirAbs {
560570 return fmt .Errorf ("destination path %q escapes staging directory" , destination )
561571 }
562- if err := copier .Mkdir ( putRoot , putDirAbs , mkdirOptions ); err != nil {
572+ if err := copier .MkdirContext ( ctx , putRoot , putDirAbs , mkdirOptions ); err != nil {
563573 return fmt .Errorf ("ensuring target directory exists: %w" , err )
564574 }
565575 tempPath := putDir
@@ -570,7 +580,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
570580 tempPath = filepath .Dir (tempPath )
571581 }
572582 } else {
573- if err := copier .Mkdir ( mountPoint , extractDirectory , mkdirOptions ); err != nil {
583+ if err := copier .MkdirContext ( ctx , mountPoint , extractDirectory , mkdirOptions ); err != nil {
574584 return fmt .Errorf ("ensuring target directory exists: %w" , err )
575585 }
576586
@@ -599,7 +609,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
599609 defer wg .Done ()
600610 defer pipeWriter .Close ()
601611 var cloneDir , subdir string
602- cloneDir , subdir , getErr = define .TempDirForURL ( tmpdir .GetTempDir (), "" , src )
612+ cloneDir , subdir , getErr = define .TempDirForURLContext ( ctx , tmpdir .GetTempDir (), "" , src )
603613 if getErr != nil {
604614 return
605615 }
@@ -621,12 +631,12 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
621631 }
622632 writer := io .WriteCloser (pipeWriter )
623633 repositoryDir := filepath .Join (cloneDir , subdir )
624- getErr = copier .Get ( repositoryDir , repositoryDir , getOptions , []string {"." }, writer )
634+ getErr = copier .GetContext ( ctx , repositoryDir , repositoryDir , getOptions , []string {"." }, writer )
625635 }()
626636 } else {
627637 go func () {
628- getErr = retry .IfNecessary (context . TODO () , func () error {
629- return getURL (src , chownFiles , mountPoint , renameTarget , pipeWriter , options .Chmod , srcDigest , options .CertPath , options .InsecureSkipTLSVerify , options .Timestamp )
638+ getErr = retry .IfNecessary (ctx , func () error {
639+ return getURL (ctx , src , chownFiles , mountPoint , renameTarget , pipeWriter , options .Chmod , srcDigest , options .CertPath , options .InsecureSkipTLSVerify , options .Timestamp )
630640 }, & retry.Options {
631641 MaxRetry : options .MaxRetries ,
632642 Delay : options .RetryDelay ,
@@ -656,7 +666,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
656666 IgnoreDevices : userns .RunningInUserNS (),
657667 Timestamp : options .Timestamp ,
658668 }
659- putErr = copier .Put ( putRoot , putDir , putOptions , io .TeeReader (pipeReader , hasher ))
669+ putErr = copier .PutContext ( ctx , putRoot , putDir , putOptions , io .TeeReader (pipeReader , hasher ))
660670 }
661671 hashCloser .Close ()
662672 pipeReader .Close ()
@@ -788,7 +798,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
788798 AllowEmptyWildcard : options .AllowEmptyWildcard == types .OptionalBoolTrue ,
789799 NoDerefSymlinks : options .FollowSymlink == types .OptionalBoolFalse ,
790800 }
791- getErr = copier .Get ( contextDir , contextDir , getOptions , []string {globbedToGlobbable (globbed )}, writer )
801+ getErr = copier .GetContext ( ctx , contextDir , contextDir , getOptions , []string {globbedToGlobbable (globbed )}, writer )
792802 closeErr = writer .Close ()
793803 if renameTarget != "" && renamedItems > 1 {
794804 renameErr = fmt .Errorf ("internal error: renamed %d items when we expected to only rename 1" , renamedItems )
@@ -820,7 +830,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
820830 IgnoreDevices : userns .RunningInUserNS (),
821831 Timestamp : options .Timestamp ,
822832 }
823- putErr = copier .Put ( putRoot , putDir , putOptions , io .TeeReader (pipeReader , hasher ))
833+ putErr = copier .PutContext ( ctx , putRoot , putDir , putOptions , io .TeeReader (pipeReader , hasher ))
824834 }
825835 hashCloser .Close ()
826836 pipeReader .Close ()
0 commit comments