@@ -5,10 +5,6 @@ import (
5
5
"context"
6
6
"errors"
7
7
"fmt"
8
- "io"
9
- "os"
10
- "path/filepath"
11
-
12
8
"github.com/containerd/containerd/archive"
13
9
"github.com/containers/image/v5/copy"
14
10
"github.com/containers/image/v5/docker"
@@ -22,6 +18,10 @@ import (
22
18
"github.com/containers/image/v5/types"
23
19
"github.com/go-logr/logr"
24
20
"github.com/opencontainers/go-digest"
21
+ "github.com/operator-framework/operator-controller/internal/util"
22
+ "io"
23
+ "os"
24
+ "path/filepath"
25
25
"sigs.k8s.io/controller-runtime/pkg/log"
26
26
"sigs.k8s.io/controller-runtime/pkg/reconcile"
27
27
)
@@ -69,12 +69,11 @@ func (i *ContainersImageRegistry) Unpack(ctx context.Context, bundle *BundleSour
69
69
//
70
70
//////////////////////////////////////////////////////
71
71
unpackPath := i .unpackPath (bundle .Name , canonicalRef .Digest ())
72
- if unpackStat , err := os .Stat (unpackPath ); err == nil {
73
- if ! unpackStat .IsDir () {
74
- panic (fmt .Sprintf ("unexpected file at unpack path %q: expected a directory" , unpackPath ))
75
- }
72
+ if isUnpacked , _ , err := IsImageUnpacked (unpackPath ); isUnpacked && err == nil {
76
73
l .Info ("image already unpacked" , "ref" , imgRef .String (), "digest" , canonicalRef .Digest ().String ())
77
74
return successResult (bundle .Name , unpackPath , canonicalRef ), nil
75
+ } else if err != nil {
76
+ return nil , fmt .Errorf ("error checking bundle already unpacked: %w" , err )
78
77
}
79
78
80
79
//////////////////////////////////////////////////////
@@ -147,7 +146,7 @@ func (i *ContainersImageRegistry) Unpack(ctx context.Context, bundle *BundleSour
147
146
//
148
147
//////////////////////////////////////////////////////
149
148
if err := i .unpackImage (ctx , unpackPath , layoutRef , srcCtx ); err != nil {
150
- if cleanupErr := deleteRecursive (unpackPath ); cleanupErr != nil {
149
+ if cleanupErr := DeleteReadOnlyRecursive (unpackPath ); cleanupErr != nil {
151
150
err = errors .Join (err , cleanupErr )
152
151
}
153
152
return nil , fmt .Errorf ("error unpacking image: %w" , err )
@@ -175,7 +174,7 @@ func successResult(bundleName, unpackPath string, canonicalRef reference.Canonic
175
174
}
176
175
177
176
func (i * ContainersImageRegistry ) Cleanup (_ context.Context , bundle * BundleSource ) error {
178
- return deleteRecursive (i .bundlePath (bundle .Name ))
177
+ return DeleteReadOnlyRecursive (i .bundlePath (bundle .Name ))
179
178
}
180
179
181
180
func (i * ContainersImageRegistry ) bundlePath (bundleName string ) string {
@@ -265,8 +264,8 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
265
264
}
266
265
}()
267
266
268
- if err := os . MkdirAll (unpackPath , 0700 ); err != nil {
269
- return fmt .Errorf ("error creating unpack directory: %w" , err )
267
+ if err := util . EnsureEmptyDirectory (unpackPath , 0700 ); err != nil {
268
+ return fmt .Errorf ("error ensuring empty unpack directory: %w" , err )
270
269
}
271
270
l := log .FromContext (ctx )
272
271
l .Info ("unpacking image" , "path" , unpackPath )
@@ -284,10 +283,10 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
284
283
l .Info ("applied layer" , "layer" , i )
285
284
return nil
286
285
}(); err != nil {
287
- return errors .Join (err , deleteRecursive (unpackPath ))
286
+ return errors .Join (err , DeleteReadOnlyRecursive (unpackPath ))
288
287
}
289
288
}
290
- if err := setReadOnlyRecursive (unpackPath ); err != nil {
289
+ if err := SetReadOnlyRecursive (unpackPath ); err != nil {
291
290
return fmt .Errorf ("error making unpack directory read-only: %w" , err )
292
291
}
293
292
return nil
@@ -324,65 +323,9 @@ func (i *ContainersImageRegistry) deleteOtherImages(bundleName string, digestToK
324
323
continue
325
324
}
326
325
imgDirPath := filepath .Join (bundlePath , imgDir .Name ())
327
- if err := deleteRecursive (imgDirPath ); err != nil {
326
+ if err := DeleteReadOnlyRecursive (imgDirPath ); err != nil {
328
327
return fmt .Errorf ("error removing image directory: %w" , err )
329
328
}
330
329
}
331
330
return nil
332
331
}
333
-
334
- func setReadOnlyRecursive (root string ) error {
335
- if err := filepath .WalkDir (root , func (path string , d os.DirEntry , err error ) error {
336
- if err != nil {
337
- return err
338
- }
339
-
340
- fi , err := d .Info ()
341
- if err != nil {
342
- return err
343
- }
344
-
345
- if err := func () error {
346
- switch typ := fi .Mode ().Type (); typ {
347
- case os .ModeSymlink :
348
- // do not follow symlinks
349
- // 1. if they resolve to other locations in the root, we'll find them anyway
350
- // 2. if they resolve to other locations outside the root, we don't want to change their permissions
351
- return nil
352
- case os .ModeDir :
353
- return os .Chmod (path , 0500 )
354
- case 0 : // regular file
355
- return os .Chmod (path , 0400 )
356
- default :
357
- return fmt .Errorf ("refusing to change ownership of file %q with type %v" , path , typ .String ())
358
- }
359
- }(); err != nil {
360
- return err
361
- }
362
- return nil
363
- }); err != nil {
364
- return fmt .Errorf ("error making bundle cache read-only: %w" , err )
365
- }
366
- return nil
367
- }
368
-
369
- func deleteRecursive (root string ) error {
370
- if err := filepath .WalkDir (root , func (path string , d os.DirEntry , err error ) error {
371
- if os .IsNotExist (err ) {
372
- return nil
373
- }
374
- if err != nil {
375
- return err
376
- }
377
- if ! d .IsDir () {
378
- return nil
379
- }
380
- if err := os .Chmod (path , 0700 ); err != nil {
381
- return err
382
- }
383
- return nil
384
- }); err != nil {
385
- return fmt .Errorf ("error making bundle cache writable for deletion: %w" , err )
386
- }
387
- return os .RemoveAll (root )
388
- }
0 commit comments