Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: improve errors for --oci-dir cases #643

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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

Check warning on line 130 in pkg/lib/file.go

View check run for this annotation

Codecov / codecov/patch

pkg/lib/file.go#L130

Added line #L130 was not covered by tests
}
if err != nil && os.IsNotExist(err) {
return false

Check warning on line 133 in pkg/lib/file.go

View check run for this annotation

Codecov / codecov/patch

pkg/lib/file.go#L133

Added line #L133 was not covered by tests
}
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 @@
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)

Check warning on line 350 in pkg/stacker/build.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/build.go#L350

Added line #L350 was not covered by tests
}
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)

Check warning on line 353 in pkg/stacker/build.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/build.go#L353

Added line #L353 was not covered by tests
}

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)

Check warning on line 358 in pkg/stacker/build.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/build.go#L358

Added line #L358 was not covered by tests
}
} 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)

Check warning on line 364 in pkg/stacker/build.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/build.go#L364

Added line #L364 was not covered by tests
}

}
defer oci.Close()

Expand Down
Loading