11package build
22
33import (
4+ "cmp"
45 "context"
56 _ "crypto/sha256" // ensure digests can be computed
67 "encoding/json"
78 "io"
9+ iofs "io/fs"
10+ "path/filepath"
11+ "slices"
12+ "strings"
813 "sync"
914
1015 "github.com/moby/buildkit/exporter/containerimage/exptypes"
@@ -14,6 +19,7 @@ import (
1419 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1520 "github.com/pkg/errors"
1621 "github.com/sirupsen/logrus"
22+ "github.com/tonistiigi/fsutil/types"
1723)
1824
1925// NewResultHandle stores a gateway client, gateway reference, and the error from
@@ -75,6 +81,40 @@ func (r *ResultHandle) NewContainer(ctx context.Context, cfg *InvokeConfig) (gat
7581 return r .gwClient .NewContainer (ctx , req )
7682}
7783
84+ func (r * ResultHandle ) StatFile (ctx context.Context , fpath string , cfg * InvokeConfig ) (* types.Stat , error ) {
85+ containerCfg , err := r .getContainerConfig (cfg )
86+ if err != nil {
87+ return nil , err
88+ }
89+
90+ candidateMounts := make ([]gateway.Mount , 0 , len (containerCfg .Mounts ))
91+ for _ , m := range containerCfg .Mounts {
92+ if strings .HasPrefix (fpath , m .Dest ) {
93+ candidateMounts = append (candidateMounts , m )
94+ }
95+ }
96+ if len (candidateMounts ) == 0 {
97+ return nil , iofs .ErrNotExist
98+ }
99+
100+ slices .SortFunc (candidateMounts , func (a , b gateway.Mount ) int {
101+ return cmp .Compare (len (a .Dest ), len (b .Dest ))
102+ })
103+
104+ m := candidateMounts [len (candidateMounts )- 1 ]
105+ relpath , err := filepath .Rel (m .Dest , fpath )
106+ if err != nil {
107+ return nil , err
108+ }
109+
110+ if m .Ref == nil {
111+ return nil , iofs .ErrNotExist
112+ }
113+
114+ req := gateway.StatRequest {Path : filepath .ToSlash (relpath )}
115+ return m .Ref .StatFile (ctx , req )
116+ }
117+
78118func (r * ResultHandle ) getContainerConfig (cfg * InvokeConfig ) (containerCfg gateway.NewContainerRequest , _ error ) {
79119 if r .ref != nil && r .solveErr == nil {
80120 logrus .Debugf ("creating container from successful build" )
0 commit comments