Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 96d5245

Browse files
committed
Fix path issues on win32
Signed-off-by: Christian Dupuis <[email protected]>
1 parent 6e1ed91 commit 96d5245

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

registry/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func ReadImage(name string, path string) (*ImageCache, error) {
3333
img, _ := index.Image(hash)
3434
skill.Log.Infof("Loaded image")
3535
return &ImageCache{
36+
Digest: hash.String(),
3637
Name: name,
37-
Path: path,
3838
Image: &img,
3939
ImagePath: path,
4040
Ref: nil,

registry/save.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (i ImageId) String() string {
6262
}
6363

6464
type ImageCache struct {
65+
Digest string
6566
Name string
66-
Path string
6767
Image *v1.Image
6868
ImagePath string
6969
Ref *name.Reference
@@ -134,25 +134,24 @@ func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
134134
return nil, errors.Wrapf(err, "failed to parse reference: %s", image)
135135
}
136136

137-
var path string
138-
if v, ok := os.LookupEnv("ATOMIST_CACHE_DIR"); ok {
139-
path = filepath.Join(v, "docker-index")
140-
} else {
141-
path = filepath.Join(os.TempDir(), "docker-index")
142-
}
143-
144-
createPaths := func(digest string) (string, string, error) {
145-
finalPath := strings.Replace(filepath.Join(path, digest), ":", string(os.PathSeparator), 1)
146-
tarPath := filepath.Join(finalPath, uuid.NewString()+".tar")
137+
createPaths := func(digest string) (string, error) {
138+
var path string
139+
if v, ok := os.LookupEnv("ATOMIST_CACHE_DIR"); ok {
140+
path = filepath.Join(v, "docker-index")
141+
} else {
142+
path = filepath.Join(os.TempDir(), "docker-index")
143+
}
144+
tarPath := filepath.Join(path, "sha256", digest[7:])
145+
tarFileName := filepath.Join(tarPath, uuid.NewString()+".tar")
147146

148147
if _, err := os.Stat(tarPath); !os.IsNotExist(err) {
149-
return finalPath, tarPath, nil
148+
return tarFileName, nil
150149
}
151-
err := os.MkdirAll(finalPath, os.ModePerm)
150+
err := os.MkdirAll(tarPath, os.ModePerm)
152151
if err != nil {
153-
return "", "", err
152+
return "", err
154153
}
155-
return finalPath, tarPath, nil
154+
return tarFileName, nil
156155
}
157156

158157
desc, err := remote.Get(ref, withAuth())
@@ -165,13 +164,13 @@ func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
165164
if err != nil {
166165
return nil, errors.Wrapf(err, "failed to get local image: %s", image)
167166
}
168-
path, imagePath, err := createPaths(im.ID)
167+
imagePath, err := createPaths(im.ID)
169168
if err != nil {
170169
return nil, errors.Wrapf(err, "failed to create cache paths")
171170
}
172171
return &ImageCache{
172+
Digest: im.ID,
173173
Name: image,
174-
Path: path,
175174
Image: &img,
176175
Ref: &ref,
177176
ImagePath: imagePath,
@@ -192,13 +191,13 @@ func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
192191
digestHash, _ := img.Digest()
193192
digest = digestHash.String()
194193
}
195-
path, imagePath, err := createPaths(digest)
194+
imagePath, err := createPaths(digest)
196195
if err != nil {
197196
return nil, errors.Wrapf(err, "failed to create cache paths")
198197
}
199198
return &ImageCache{
199+
Digest: digest,
200200
Name: image,
201-
Path: path,
202201
Image: &img,
203202
Ref: &ref,
204203
ImagePath: imagePath,

sbom/index.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,10 @@ func IndexImage(image string, cli command.Cli) (*types.Sbom, *v1.Image, error) {
7474
}
7575

7676
func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.Image, error) {
77-
// see if we can re-use an existing sbom
78-
sbomPath := filepath.Join(cache.Path, "sbom.json")
79-
if _, ok := os.LookupEnv("ATOMIST_NO_CACHE"); !ok {
80-
if _, err := os.Stat(sbomPath); !os.IsNotExist(err) {
81-
var sbom types.Sbom
82-
b, err := os.ReadFile(sbomPath)
83-
if err == nil {
84-
err := json.Unmarshal(b, &sbom)
85-
if err == nil {
86-
if sbom.Descriptor.SbomVersion == internal.FromBuild().SbomVersion && sbom.Descriptor.Version == internal.FromBuild().Version {
87-
skill.Log.Infof(`Indexed %d packages`, len(sbom.Artifacts))
88-
return &sbom, cache.Image, nil
89-
}
90-
}
91-
}
92-
}
77+
configFilePath := cli.ConfigFile().Filename
78+
sbomFilePath := filepath.Join(filepath.Dir(configFilePath), "sbom", "sha256", cache.Digest[7:], "sbom.json")
79+
if sbom := cachedSbom(cache, sbomFilePath); sbom != nil {
80+
return sbom, cache.Image, nil
9381
}
9482

9583
err := cache.StoreImage()
@@ -99,7 +87,6 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
9987
}
10088

10189
lm := createLayerMapping(*cache.Image)
102-
skill.Log.Debugf("Created layer mapping")
10390

10491
s := internal.StartSpinner("info", "Indexing", cli.Out().IsTerminal())
10592
defer s.Stop()
@@ -173,12 +160,39 @@ func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, *v1.I
173160

174161
js, err := json.MarshalIndent(sbom, "", " ")
175162
if err == nil {
176-
_ = os.WriteFile(sbomPath, js, 0644)
163+
err = os.MkdirAll(filepath.Dir(sbomFilePath), os.ModePerm)
164+
if err != nil {
165+
return nil, nil, errors.Wrapf(err, "failed create to sbom folder")
166+
}
167+
err = os.WriteFile(sbomFilePath, js, 0644)
168+
if err != nil {
169+
return nil, nil, errors.Wrapf(err, "failed to write sbom")
170+
}
177171
}
178172

179173
return &sbom, cache.Image, nil
180174
}
181175

176+
func cachedSbom(cache *registry.ImageCache, sbomFilePath string) *types.Sbom {
177+
// see if we can re-use an existing sbom
178+
if _, ok := os.LookupEnv("ATOMIST_NO_CACHE"); !ok {
179+
if _, err := os.Stat(sbomFilePath); !os.IsNotExist(err) {
180+
var sbom types.Sbom
181+
b, err := os.ReadFile(sbomFilePath)
182+
if err == nil {
183+
err := json.Unmarshal(b, &sbom)
184+
if err == nil {
185+
if sbom.Descriptor.SbomVersion == internal.FromBuild().SbomVersion && sbom.Descriptor.Version == internal.FromBuild().Version {
186+
skill.Log.Infof(`Indexed %d packages`, len(sbom.Artifacts))
187+
return &sbom
188+
}
189+
}
190+
}
191+
}
192+
}
193+
return nil
194+
}
195+
182196
func createLayerMapping(img v1.Image) types.LayerMapping {
183197
lm := types.LayerMapping{
184198
ByDiffId: make(map[string]string, 0),
@@ -203,5 +217,6 @@ func createLayerMapping(img v1.Image) types.LayerMapping {
203217
lm.DigestByOrdinal[i] = layer.Digest.String()
204218
}
205219

220+
skill.Log.Debugf("Created layer mapping")
206221
return lm
207222
}

0 commit comments

Comments
 (0)