Skip to content

Commit 99cb71a

Browse files
committed
Clear cache on startup, use tempDir for unpacking
1 parent 037b9e2 commit 99cb71a

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

catalogd/cmd/catalogd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ func main() {
263263
}
264264

265265
unpackCacheBasePath := filepath.Join(cacheDir, source.UnpackCacheDir)
266+
if err := os.RemoveAll(unpackCacheBasePath); err != nil {
267+
setupLog.Error(err, "unable to clear cache directory for unpacking on startup")
268+
os.Exit(1)
269+
}
266270
if err := os.MkdirAll(unpackCacheBasePath, 0770); err != nil {
267271
setupLog.Error(err, "unable to create cache directory for unpacking")
268272
os.Exit(1)

catalogd/internal/source/containers_image.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,17 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
296296
return wrapTerminal(fmt.Errorf("catalog image is missing the required label %q", ConfigDirLabel), specIsCanonical)
297297
}
298298

299-
if err := os.MkdirAll(unpackPath, 0700); err != nil {
300-
return fmt.Errorf("error creating unpack directory: %w", err)
301-
}
302299
l := log.FromContext(ctx)
303-
l.Info("unpacking image", "path", unpackPath)
300+
l.Info("unpacking image", "path", unpackPath, "temp path", tempUnpackPath)
301+
tempUnpackPath, err := os.MkdirTemp("", fmt.Sprintf("unpack-%s", catalog.Name))
302+
if err != nil {
303+
return fmt.Errorf("error creating temporary unpack directory: %w", err)
304+
}
305+
defer func() {
306+
if err := os.RemoveAll(tempUnpackPath); err != nil {
307+
l.Error(err, "error removing temporary unpack directory")
308+
}
309+
}()
304310
for i, layerInfo := range img.LayerInfos() {
305311
if err := func() error {
306312
layerReader, _, err := layoutSrc.GetBlob(ctx, layerInfo, none.NoCache)
@@ -309,15 +315,21 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
309315
}
310316
defer layerReader.Close()
311317

312-
if err := applyLayer(ctx, unpackPath, dirToUnpack, layerReader); err != nil {
318+
if err := applyLayer(ctx, tempUnpackPath, dirToUnpack, layerReader); err != nil {
313319
return fmt.Errorf("error applying layer[%d]: %w", i, err)
314320
}
315321
l.Info("applied layer", "layer", i)
316322
return nil
317323
}(); err != nil {
318-
return errors.Join(err, deleteRecursive(unpackPath))
324+
return errors.Join(err, deleteRecursive(tempUnpackPath))
319325
}
320326
}
327+
if err := os.MkdirAll(unpackPath, 0700); err != nil {
328+
return fmt.Errorf("error creating unpack directory: %w", err)
329+
}
330+
if err := os.Rename(tempUnpackPath, unpackPath); err != nil {
331+
return fmt.Errorf("error moving temporary unpack directory to final unpack directory: %w", err)
332+
}
321333
if err := setReadOnlyRecursive(unpackPath); err != nil {
322334
return fmt.Errorf("error making unpack directory read-only: %w", err)
323335
}

0 commit comments

Comments
 (0)