Skip to content

Commit

Permalink
check whether symlink exists (#534)
Browse files Browse the repository at this point in the history
* fix: check whether symlink exists

Signed-off-by: Serge Hallyn <[email protected]>

* fix: generateLayer: catch case where manifest has no layers

Signed-off-by: Serge Hallyn <[email protected]>

---------

Signed-off-by: Serge Hallyn <[email protected]>
  • Loading branch information
hallyn authored Nov 10, 2023
1 parent 589a648 commit dcc1eca
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 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")
}
}
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
}

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

func lookupManifestInDir(dir, name string) (ispec.Manifest, error) {
oci, err := umoci.OpenLayout(dir)
if err != nil {
Expand Down Expand Up @@ -372,6 +397,9 @@ func generateLayer(config types.StackerConfig, oci casext.Engine, mutators []*mu
if ovl.HasBuiltOCIOutput {
for i, layerType := range layerTypes {
manifest := ovl.Manifests[layerType]
if len(manifest.Layers) < 1 {
continue
}
layer := manifest.Layers[len(manifest.Layers)-1]

config := ovl.Configs[layerType]
Expand Down

0 comments on commit dcc1eca

Please sign in to comment.