Skip to content

Commit 898f63b

Browse files
committed
Detect failure to access os.TempDir
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent ce463d5 commit 898f63b

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/fsnotify/fsevents v0.2.0
2525
github.com/go-viper/mapstructure/v2 v2.4.0
2626
github.com/google/go-cmp v0.7.0
27+
github.com/google/uuid v1.6.0
2728
github.com/hashicorp/go-version v1.7.0
2829
github.com/jonboulle/clockwork v0.5.0
2930
github.com/mattn/go-shellwords v1.0.12
@@ -108,7 +109,6 @@ require (
108109
github.com/google/gnostic-models v0.6.8 // indirect
109110
github.com/google/gofuzz v1.2.0 // indirect
110111
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
111-
github.com/google/uuid v1.6.0 // indirect
112112
github.com/gorilla/mux v1.8.1 // indirect
113113
github.com/gorilla/websocket v1.5.0 // indirect
114114
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect

pkg/compose/build_bake.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"errors"
2626
"fmt"
2727
"io"
28-
"math/rand"
28+
"io/fs"
2929
"os"
3030
"os/exec"
3131
"path/filepath"
@@ -41,6 +41,7 @@ import (
4141
"github.com/docker/compose/v2/pkg/progress"
4242
"github.com/docker/docker/api/types/versions"
4343
"github.com/docker/docker/builder/remotecontext/urlutil"
44+
"github.com/google/uuid"
4445
"github.com/moby/buildkit/client"
4546
gitutil "github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
4647
"github.com/moby/buildkit/util/progress/progressui"
@@ -289,14 +290,26 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
289290
}
290291
logrus.Debugf("bake build config:\n%s", string(b))
291292

293+
tmpdir := os.TempDir()
294+
/*if !fs.ValidPath(tmpdir) {
295+
// see https://github.com/docker/compose/issues/12998
296+
return nil, fmt.Errorf("can't acces os.tempDir %s. Please check permissions.", tmpdir)
297+
}*/
292298
var metadataFile string
293299
for {
294300
// we don't use os.CreateTemp here as we need a temporary file name, but don't want it actually created
295301
// as bake relies on atomicwriter and this creates conflict during rename
296-
metadataFile = filepath.Join(os.TempDir(), fmt.Sprintf("compose-build-metadataFile-%d.json", rand.Int31()))
297-
if _, err = os.Stat(metadataFile); os.IsNotExist(err) {
298-
break
302+
metadataFile = filepath.Join(tmpdir, fmt.Sprintf("compose-build-metadataFile-%s.json", uuid.New().String()))
303+
if _, err = os.Stat(metadataFile); err != nil {
304+
if os.IsNotExist(err) {
305+
break
306+
}
307+
var pathError *fs.PathError
308+
if errors.As(err, &pathError) {
309+
return nil, fmt.Errorf("can't acces os.tempDir %s: %s", tmpdir, pathError.Err)
310+
}
299311
}
312+
logrus.Debugf("metadata file already exists: %s", metadataFile)
300313
}
301314
defer func() {
302315
_ = os.Remove(metadataFile)
@@ -369,9 +382,13 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
369382
for {
370383
line, readErr := reader.ReadString('\n')
371384
if readErr != nil {
372-
if readErr == io.EOF {
385+
switch {
386+
case errors.Is(err, io.EOF):
373387
break
374-
} else {
388+
case errors.Is(err, os.ErrClosed):
389+
logrus.Debugf("bake stopped")
390+
break
391+
default:
375392
return nil, fmt.Errorf("failed to execute bake: %w", readErr)
376393
}
377394
}

0 commit comments

Comments
 (0)