Skip to content

Commit 9d5ec02

Browse files
committed
Ensure cache directory exists
1 parent 267b723 commit 9d5ec02

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

internal/docker/imagesgc.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ func DefaultImagesGCConfig() ImagesGCConfig {
4343
}
4444

4545
type ImagesGC struct {
46-
path string
46+
// path contains the path to the GC cache when read from disk.
47+
path string
48+
49+
// images contains the entries of the tracked docker images.
4750
images []gcEntry
48-
clock func() time.Time
51+
52+
// clock returns the current time.
53+
clock func() time.Time
54+
55+
// client implements a docker client.
4956
client imagesGCClient
5057

5158
ImagesGCConfig
@@ -113,11 +120,16 @@ func (gc *ImagesGC) Persist() error {
113120
return nil
114121
}
115122

123+
err := os.MkdirAll(filepath.Dir(gc.path), 0755)
124+
if err != nil && !errors.Is(err, os.ErrExist) {
125+
return err
126+
}
127+
116128
d, err := json.Marshal(gc.images)
117129
if err != nil {
118130
return fmt.Errorf("failed to encode list of images: %w", err)
119131
}
120-
return os.WriteFile(gc.path, d, 0o644)
132+
return os.WriteFile(gc.path, d, 0644)
121133
}
122134

123135
// Track images before they are downloaded. Images already present are ignored if they are not already tracked.

0 commit comments

Comments
 (0)