Skip to content

Commit

Permalink
check whether symlink exists
Browse files Browse the repository at this point in the history
Signed-off-by: Serge Hallyn <[email protected]>
  • Loading branch information
hallyn committed Nov 5, 2023
1 parent 67d1ffb commit 124a642
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions pkg/overlay/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ func ConvertAndOutput(config types.StackerConfig, tag, name string, layerType ty

// slight hack, but this is much faster than a cp, and the
// layers are the same, just in different formats
err = os.Symlink(overlayPath(config.RootFSDir, theLayer.Digest), overlayPath(config.RootFSDir, desc.Digest))
if err != nil {
return errors.Wrapf(err, "failed to create squashfs symlink")
if !PathExists(overlayPath(config.RootFSDir, desc.Digest)) {
err = os.Symlink(overlayPath(config.RootFSDir, theLayer.Digest), overlayPath(config.RootFSDir, desc.Digest))
if err != nil {
return errors.Wrapf(err, "failed to create squashfs symlink")
}

Check warning on line 158 in pkg/overlay/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/overlay/pack.go#L154-L158

Added lines #L154 - L158 were not covered by tests
}
newManifest.Layers = append(newManifest.Layers, desc)
newConfig.RootFS.DiffIDs = append(newConfig.RootFS.DiffIDs, desc.Digest)
Expand All @@ -167,6 +169,29 @@ func ConvertAndOutput(config types.StackerConfig, tag, name string, layerType ty
return nil
}

func IsSymlink(path string) (bool, error) {
statInfo, err := os.Lstat(path)
if err != nil {
return false, err
}
return (statInfo.Mode() & os.ModeSymlink) != 0, nil

Check warning on line 177 in pkg/overlay/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/overlay/pack.go#L172-L177

Added lines #L172 - L177 were not covered by tests
}

func PathExists(path string) bool {
statInfo, err := os.Stat(path)
if statInfo == nil {
isLink, err := IsSymlink(path)
if err != nil {
return false
}
return isLink

Check warning on line 187 in pkg/overlay/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/overlay/pack.go#L180-L187

Added lines #L180 - L187 were not covered by tests
}
if err != nil && os.IsNotExist(err) {
return false
}
return true

Check warning on line 192 in pkg/overlay/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/overlay/pack.go#L189-L192

Added lines #L189 - L192 were not covered by tests
}

func lookupManifestInDir(dir, name string) (ispec.Manifest, error) {
oci, err := umoci.OpenLayout(dir)
if err != nil {
Expand Down

0 comments on commit 124a642

Please sign in to comment.