Skip to content

Commit 4e4fd08

Browse files
committed
config: fix file/folder ownership
Signed-off-by: CrazyMax <[email protected]>
1 parent 1de3325 commit 4e4fd08

File tree

20 files changed

+317
-103
lines changed

20 files changed

+317
-103
lines changed

build/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ func toRepoOnly(in string) (string, error) {
151151
return strings.Join(out, ","), nil
152152
}
153153

154-
func Build(ctx context.Context, nodes []builder.Node, opts map[string]Options, docker *dockerutil.Client, configDir string, w progress.Writer) (resp map[string]*client.SolveResponse, err error) {
155-
return BuildWithResultHandler(ctx, nodes, opts, docker, configDir, w, nil)
154+
func Build(ctx context.Context, nodes []builder.Node, opts map[string]Options, docker *dockerutil.Client, cfg *confutil.Config, w progress.Writer) (resp map[string]*client.SolveResponse, err error) {
155+
return BuildWithResultHandler(ctx, nodes, opts, docker, cfg, w, nil)
156156
}
157157

158-
func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opts map[string]Options, docker *dockerutil.Client, configDir string, w progress.Writer, resultHandleFunc func(driverIndex int, rCtx *ResultHandle)) (resp map[string]*client.SolveResponse, err error) {
158+
func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opts map[string]Options, docker *dockerutil.Client, cfg *confutil.Config, w progress.Writer, resultHandleFunc func(driverIndex int, rCtx *ResultHandle)) (resp map[string]*client.SolveResponse, err error) {
159159
if len(nodes) == 0 {
160160
return nil, errors.Errorf("driver required for build")
161161
}
@@ -234,12 +234,12 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opts map[
234234
return nil, err
235235
}
236236
localOpt := opt
237-
so, release, err := toSolveOpt(ctx, np.Node(), multiDriver, &localOpt, gatewayOpts, configDir, w, docker)
237+
so, release, err := toSolveOpt(ctx, np.Node(), multiDriver, &localOpt, gatewayOpts, cfg, w, docker)
238238
opts[k] = localOpt
239239
if err != nil {
240240
return nil, err
241241
}
242-
if err := saveLocalState(so, k, opt, np.Node(), configDir); err != nil {
242+
if err := saveLocalState(so, k, opt, np.Node(), cfg); err != nil {
243243
return nil, err
244244
}
245245
addGitAttrs(so)

build/localstate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55

66
"github.com/docker/buildx/builder"
77
"github.com/docker/buildx/localstate"
8+
"github.com/docker/buildx/util/confutil"
89
"github.com/moby/buildkit/client"
910
)
1011

11-
func saveLocalState(so *client.SolveOpt, target string, opts Options, node builder.Node, configDir string) error {
12+
func saveLocalState(so *client.SolveOpt, target string, opts Options, node builder.Node, cfg *confutil.Config) error {
1213
var err error
1314
if so.Ref == "" {
1415
return nil
@@ -30,7 +31,7 @@ func saveLocalState(so *client.SolveOpt, target string, opts Options, node build
3031
if lp == "" && dp == "" {
3132
return nil
3233
}
33-
l, err := localstate.New(configDir)
34+
l, err := localstate.New(cfg)
3435
if err != nil {
3536
return err
3637
}

build/opt.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"github.com/tonistiigi/fsutil"
3636
)
3737

38-
func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *Options, bopts gateway.BuildOpts, configDir string, pw progress.Writer, docker *dockerutil.Client) (_ *client.SolveOpt, release func(), err error) {
38+
func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *Options, bopts gateway.BuildOpts, cfg *confutil.Config, pw progress.Writer, docker *dockerutil.Client) (_ *client.SolveOpt, release func(), err error) {
3939
nodeDriver := node.Driver
4040
defers := make([]func(), 0, 2)
4141
releaseF := func() {
@@ -271,7 +271,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *O
271271

272272
// add node identifier to shared key if one was specified
273273
if so.SharedKey != "" {
274-
so.SharedKey += ":" + confutil.TryNodeIdentifier(configDir)
274+
so.SharedKey += ":" + confutil.TryNodeIdentifier(cfg)
275275
}
276276

277277
if opt.Pull {
@@ -401,7 +401,7 @@ func loadInputs(ctx context.Context, d *driver.DriverHandle, inp *Inputs, pw pro
401401
}
402402
// stdin is dockerfile
403403
dockerfileReader = rc
404-
inp.ContextPath, _ = os.MkdirTemp("", "empty-dir")
404+
inp.ContextPath, _ = osutil.MkdirTemp("", "empty-dir")
405405
toRemove = append(toRemove, inp.ContextPath)
406406
if err := setLocalMount("context", inp.ContextPath, target); err != nil {
407407
return nil, err
@@ -593,11 +593,11 @@ func setLocalMount(name, dir string, so *client.SolveOpt) error {
593593
}
594594

595595
func createTempDockerfile(r io.Reader, multiReader *SyncMultiReader) (string, error) {
596-
dir, err := os.MkdirTemp("", "dockerfile")
596+
dir, err := osutil.MkdirTemp("", "dockerfile")
597597
if err != nil {
598598
return "", err
599599
}
600-
f, err := os.Create(filepath.Join(dir, "Dockerfile"))
600+
f, err := osutil.Create(filepath.Join(dir, "Dockerfile"))
601601
if err != nil {
602602
return "", err
603603
}

build/url.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package build
22

33
import (
44
"context"
5-
"os"
65
"path/filepath"
76

87
"github.com/docker/buildx/driver"
8+
"github.com/docker/buildx/util/osutil"
99
"github.com/docker/buildx/util/progress"
1010
"github.com/docker/go-units"
1111
"github.com/moby/buildkit/client"
@@ -56,11 +56,11 @@ func createTempDockerfileFromURL(ctx context.Context, d *driver.DriverHandle, ur
5656
if err != nil {
5757
return nil, err
5858
}
59-
dir, err := os.MkdirTemp("", "buildx")
59+
dir, err := osutil.MkdirTemp("", "buildx")
6060
if err != nil {
6161
return nil, err
6262
}
63-
if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), dt, 0600); err != nil {
63+
if err := osutil.WriteFile(filepath.Join(dir, "Dockerfile"), dt, 0600); err != nil {
6464
return nil, err
6565
}
6666
out = dir

builder/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
439439
if buildkitdConfigFile == "" {
440440
// if buildkit daemon config is not provided, check if the default one
441441
// is available and use it
442-
if f, ok := confutil.DefaultConfigFile(dockerCli); ok {
442+
if f, ok := confutil.NewConfig(dockerCli).BuildKitConfigFile(); ok {
443443
buildkitdConfigFile = f
444444
}
445445
}
@@ -584,7 +584,7 @@ func Leave(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Leav
584584
return err
585585
}
586586

587-
ls, err := localstate.New(confutil.ConfigDir(dockerCli))
587+
ls, err := localstate.New(confutil.NewConfig(dockerCli))
588588
if err != nil {
589589
return err
590590
}

commands/bake.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
265265
}
266266

267267
done := timeBuildCommand(mp, attributes)
268-
resp, retErr := build.Build(ctx, nodes, bo, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), printer)
268+
resp, retErr := build.Build(ctx, nodes, bo, dockerutil.NewClient(dockerCli), confutil.NewConfig(dockerCli), printer)
269269
if err := printer.Wait(); retErr == nil {
270270
retErr = err
271271
}
@@ -470,7 +470,7 @@ func saveLocalStateGroup(dockerCli command.Cli, in bakeOptions, targets []string
470470
refs = append(refs, b.Ref)
471471
bo[k] = b
472472
}
473-
l, err := localstate.New(confutil.ConfigDir(dockerCli))
473+
l, err := localstate.New(confutil.NewConfig(dockerCli))
474474
if err != nil {
475475
return err
476476
}
@@ -621,7 +621,7 @@ func bakeMetricAttributes(dockerCli command.Cli, driverType, url, cmdContext str
621621
commandNameAttribute.String("bake"),
622622
attribute.Stringer(string(commandOptionsHash), &bakeOptionsHash{
623623
bakeOptions: options,
624-
configDir: confutil.ConfigDir(dockerCli),
624+
cfg: confutil.NewConfig(dockerCli),
625625
url: url,
626626
cmdContext: cmdContext,
627627
targets: targets,
@@ -633,7 +633,7 @@ func bakeMetricAttributes(dockerCli command.Cli, driverType, url, cmdContext str
633633

634634
type bakeOptionsHash struct {
635635
*bakeOptions
636-
configDir string
636+
cfg *confutil.Config
637637
url string
638638
cmdContext string
639639
targets []string
@@ -657,7 +657,7 @@ func (o *bakeOptionsHash) String() string {
657657

658658
joinedFiles := strings.Join(files, ",")
659659
joinedTargets := strings.Join(targets, ",")
660-
salt := confutil.TryNodeIdentifier(o.configDir)
660+
salt := confutil.TryNodeIdentifier(o.cfg)
661661

662662
h := sha256.New()
663663
for _, s := range []string{url, cmdContext, joinedFiles, joinedTargets, salt} {

commands/build.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141
"github.com/docker/cli/cli/command"
4242
dockeropts "github.com/docker/cli/opts"
4343
"github.com/docker/docker/api/types/versions"
44-
"github.com/docker/docker/pkg/ioutils"
4544
"github.com/moby/buildkit/client"
4645
"github.com/moby/buildkit/exporter/containerimage/exptypes"
4746
"github.com/moby/buildkit/frontend/subrequests"
@@ -238,7 +237,7 @@ func buildMetricAttributes(dockerCli command.Cli, driverType string, options *bu
238237
commandNameAttribute.String("build"),
239238
attribute.Stringer(string(commandOptionsHash), &buildOptionsHash{
240239
buildOptions: options,
241-
configDir: confutil.ConfigDir(dockerCli),
240+
cfg: confutil.NewConfig(dockerCli),
242241
}),
243242
driverNameAttribute.String(options.builder),
244243
driverTypeAttribute.String(driverType),
@@ -250,7 +249,7 @@ func buildMetricAttributes(dockerCli command.Cli, driverType string, options *bu
250249
// the fmt.Stringer interface.
251250
type buildOptionsHash struct {
252251
*buildOptions
253-
configDir string
252+
cfg *confutil.Config
254253
result string
255254
resultOnce sync.Once
256255
}
@@ -267,7 +266,7 @@ func (o *buildOptionsHash) String() string {
267266
if contextPath != "-" && osutil.IsLocalDir(contextPath) {
268267
contextPath = osutil.ToAbs(contextPath)
269268
}
270-
salt := confutil.TryNodeIdentifier(o.configDir)
269+
salt := confutil.TryNodeIdentifier(o.cfg)
271270

272271
h := sha256.New()
273272
for _, s := range []string{target, contextPath, dockerfile, salt} {
@@ -374,7 +373,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
374373
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
375374
}
376375
if options.imageIDFile != "" {
377-
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0644); err != nil {
376+
if err := osutil.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0644); err != nil {
378377
return errors.Wrap(err, "writing image ID file")
379378
}
380379
}
@@ -742,7 +741,7 @@ func writeMetadataFile(filename string, dt interface{}) error {
742741
if err != nil {
743742
return err
744743
}
745-
return ioutils.AtomicWriteFile(filename, b, 0644)
744+
return osutil.AtomicWriteFile(filename, b, 0644)
746745
}
747746

748747
func decodeExporterResponse(exporterResponse map[string]string) map[string]interface{} {

commands/install.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package commands
22

33
import (
4-
"os"
5-
64
"github.com/docker/buildx/util/cobrautil"
75
"github.com/docker/buildx/util/cobrautil/completion"
6+
"github.com/docker/buildx/util/osutil"
87
"github.com/docker/cli/cli"
98
"github.com/docker/cli/cli/command"
109
"github.com/docker/cli/cli/config"
@@ -17,7 +16,7 @@ type installOptions struct {
1716

1817
func runInstall(_ command.Cli, _ installOptions) error {
1918
dir := config.Dir()
20-
if err := os.MkdirAll(dir, 0755); err != nil {
19+
if err := osutil.MkdirAll(dir, 0755); err != nil {
2120
return errors.Wrap(err, "could not create docker config")
2221
}
2322

controller/build/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, nodes []builder.No
214214
if generateResult {
215215
var mu sync.Mutex
216216
var idx int
217-
resp, err = build.BuildWithResultHandler(ctx, nodes, opts, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), progress, func(driverIndex int, gotRes *build.ResultHandle) {
217+
resp, err = build.BuildWithResultHandler(ctx, nodes, opts, dockerutil.NewClient(dockerCli), confutil.NewConfig(dockerCli), progress, func(driverIndex int, gotRes *build.ResultHandle) {
218218
mu.Lock()
219219
defer mu.Unlock()
220220
if res == nil || driverIndex < idx {
221221
idx, res = driverIndex, gotRes
222222
}
223223
})
224224
} else {
225-
resp, err = build.Build(ctx, nodes, opts, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), progress)
225+
resp, err = build.Build(ctx, nodes, opts, dockerutil.NewClient(dockerCli), confutil.NewConfig(dockerCli), progress)
226226
}
227227
if err != nil {
228228
return nil, res, err

controller/pb/export.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77

88
"github.com/containerd/console"
9+
"github.com/docker/buildx/util/osutil"
910
"github.com/moby/buildkit/client"
1011
"github.com/pkg/errors"
1112
)
@@ -85,7 +86,7 @@ func CreateExports(entries []*ExportEntry) ([]client.ExportEntry, error) {
8586
if err == nil && fi.IsDir() {
8687
return nil, errors.Errorf("destination file %s is a directory", entry.Destination)
8788
}
88-
f, err := os.Create(entry.Destination)
89+
f, err := osutil.Create(entry.Destination)
8990
if err != nil {
9091
return nil, errors.Errorf("failed to open %s", err)
9192
}

0 commit comments

Comments
 (0)