Skip to content

Commit

Permalink
build: improve errors for --oci-dir cases (#643)
Browse files Browse the repository at this point in the history
If the argument to oci-dir exists but is empty, we currently get a
confusing error about it not being a layout. Other cases could be better
explained while we're at it too.

Signed-off-by: Michael McCracken <[email protected]>
  • Loading branch information
mikemccracken authored Sep 17, 2024
1 parent dbfe650 commit 46064c7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 42 deletions.
10 changes: 0 additions & 10 deletions pkg/lib/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import (
"github.com/pkg/errors"
)

func IsSymlink(p string) bool {
fi, err := os.Lstat(p)
if err != nil {
// Some people can't be helped
return false
}

return fi.Mode()&os.ModeSymlink != 0
}

// DirCopy copies a whole directory recursively
func DirCopy(dest string, source string) error {

Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestDir(t *testing.T) {
_, err = src.Write([]byte("hello world!"))
So(err, ShouldBeNil)

ok := lib.IsSymlink(src.Name())
ok, _ := lib.IsSymlink(src.Name())
So(ok, ShouldBeFalse)
})

Expand Down
23 changes: 23 additions & 0 deletions pkg/lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,26 @@ func FindFiles(base, pattern string) ([]string, error) {

return paths, err
}

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
}
27 changes: 2 additions & 25 deletions pkg/overlay/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ 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
if !PathExists(overlayPath(config.RootFSDir, desc.Digest)) {
if !lib.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")
Expand All @@ -169,29 +169,6 @@ 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 @@ -374,7 +351,7 @@ func stripOverlayAttrsUnder(dirPath string) error {
return err
}
p := filepath.Join(dirPath, path)
if lib.IsSymlink(p) {
if isSymlink, _ := lib.IsSymlink(p); isSymlink {
// user.* xattrs "can not" exist on symlinks
return nil
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/stacker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,30 @@ func (b *Builder) build(s types.Storage, file string) error {
log.Debugf("Dependency Order %v", order)

var oci casext.Engine
if _, statErr := os.Stat(opts.Config.OCIDir); statErr != nil {
oci, err = umoci.CreateLayout(opts.Config.OCIDir)
} else {
ocidirstat, statErr := os.Stat(opts.Config.OCIDir)

// if it exists, it is a directory, and it has an index.json, try openlayout
// otherwise try createlayout

if statErr == nil {
if !ocidirstat.IsDir() {
return errors.Errorf("parameter oci-dir=%q exists but is not a directory.", opts.Config.OCIDir)
}
if !lib.PathExists(filepath.Join(opts.Config.OCIDir, "index.json")) {
return errors.Errorf("parameter oci-dir=%q exists but does not look like an OCI Layout.", opts.Config.OCIDir)
}

oci, err = umoci.OpenLayout(opts.Config.OCIDir)
}
if err != nil {
return err
if err != nil {
return errors.Wrapf(err, "could not open OCI layout at %q", opts.Config.OCIDir)
}
} else {
log.Infof("Creating new OCI Layout at %q", opts.Config.OCIDir)
oci, err = umoci.CreateLayout(opts.Config.OCIDir)
if err != nil {
return errors.Wrapf(err, "could not create layout at %q", opts.Config.OCIDir)
}

}
defer oci.Close()

Expand Down

0 comments on commit 46064c7

Please sign in to comment.