@@ -76,12 +76,9 @@ func (i *ContainersImageRegistry) Unpack(ctx context.Context, catalog *catalogdv
7676 //
7777 //////////////////////////////////////////////////////
7878 unpackPath := i .unpackPath (catalog .Name , canonicalRef .Digest ())
79- if unpackStat , err := os .Stat (unpackPath ); err == nil {
80- if ! unpackStat .IsDir () {
81- panic (fmt .Sprintf ("unexpected file at unpack path %q: expected a directory" , unpackPath ))
82- }
79+ if isUnpacked , unpackTime := isImageUnpacked (unpackPath ); isUnpacked {
8380 l .Info ("image already unpacked" , "ref" , imgRef .String (), "digest" , canonicalRef .Digest ().String ())
84- return successResult (unpackPath , canonicalRef , unpackStat . ModTime () ), nil
81+ return successResult (unpackPath , canonicalRef , unpackTime ), nil
8582 }
8683
8784 //////////////////////////////////////////////////////
@@ -296,6 +293,10 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
296293 return wrapTerminal (fmt .Errorf ("catalog image is missing the required label %q" , ConfigDirLabel ), specIsCanonical )
297294 }
298295
296+ // ensure unpack directory is empty
297+ if err := os .RemoveAll (unpackPath ); err != nil {
298+ return fmt .Errorf ("error removing unpacked path: %w" , err )
299+ }
299300 if err := os .MkdirAll (unpackPath , 0700 ); err != nil {
300301 return fmt .Errorf ("error creating unpack directory: %w" , err )
301302 }
@@ -431,3 +432,13 @@ func wrapTerminal(err error, isTerminal bool) error {
431432 }
432433 return reconcile .TerminalError (err )
433434}
435+
436+ func isImageUnpacked (unpackPath string ) (bool , time.Time ) {
437+ if unpackStat , err := os .Stat (unpackPath ); err == nil {
438+ if ! unpackStat .IsDir () {
439+ panic (fmt .Sprintf ("unexpected file at unpack path %q: expected a directory" , unpackPath ))
440+ }
441+ return true , unpackStat .ModTime ()
442+ }
443+ return false , time.Time {}
444+ }
0 commit comments