Skip to content

Commit 46064c7

Browse files
build: improve errors for --oci-dir cases (#643)
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]>
1 parent dbfe650 commit 46064c7

File tree

5 files changed

+49
-42
lines changed

5 files changed

+49
-42
lines changed

pkg/lib/dir.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ import (
1010
"github.com/pkg/errors"
1111
)
1212

13-
func IsSymlink(p string) bool {
14-
fi, err := os.Lstat(p)
15-
if err != nil {
16-
// Some people can't be helped
17-
return false
18-
}
19-
20-
return fi.Mode()&os.ModeSymlink != 0
21-
}
22-
2313
// DirCopy copies a whole directory recursively
2414
func DirCopy(dest string, source string) error {
2515

pkg/lib/dir_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestDir(t *testing.T) {
1919
_, err = src.Write([]byte("hello world!"))
2020
So(err, ShouldBeNil)
2121

22-
ok := lib.IsSymlink(src.Name())
22+
ok, _ := lib.IsSymlink(src.Name())
2323
So(ok, ShouldBeFalse)
2424
})
2525

pkg/lib/file.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,26 @@ func FindFiles(base, pattern string) ([]string, error) {
111111

112112
return paths, err
113113
}
114+
115+
func IsSymlink(path string) (bool, error) {
116+
statInfo, err := os.Lstat(path)
117+
if err != nil {
118+
return false, err
119+
}
120+
return (statInfo.Mode() & os.ModeSymlink) != 0, nil
121+
}
122+
123+
func PathExists(path string) bool {
124+
statInfo, err := os.Stat(path)
125+
if statInfo == nil {
126+
isLink, err := IsSymlink(path)
127+
if err != nil {
128+
return false
129+
}
130+
return isLink
131+
}
132+
if err != nil && os.IsNotExist(err) {
133+
return false
134+
}
135+
return true
136+
}

pkg/overlay/pack.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func ConvertAndOutput(config types.StackerConfig, tag, name string, layerType ty
151151

152152
// slight hack, but this is much faster than a cp, and the
153153
// layers are the same, just in different formats
154-
if !PathExists(overlayPath(config.RootFSDir, desc.Digest)) {
154+
if !lib.PathExists(overlayPath(config.RootFSDir, desc.Digest)) {
155155
err = os.Symlink(overlayPath(config.RootFSDir, theLayer.Digest), overlayPath(config.RootFSDir, desc.Digest))
156156
if err != nil {
157157
return errors.Wrapf(err, "failed to create squashfs symlink")
@@ -169,29 +169,6 @@ func ConvertAndOutput(config types.StackerConfig, tag, name string, layerType ty
169169
return nil
170170
}
171171

172-
func IsSymlink(path string) (bool, error) {
173-
statInfo, err := os.Lstat(path)
174-
if err != nil {
175-
return false, err
176-
}
177-
return (statInfo.Mode() & os.ModeSymlink) != 0, nil
178-
}
179-
180-
func PathExists(path string) bool {
181-
statInfo, err := os.Stat(path)
182-
if statInfo == nil {
183-
isLink, err := IsSymlink(path)
184-
if err != nil {
185-
return false
186-
}
187-
return isLink
188-
}
189-
if err != nil && os.IsNotExist(err) {
190-
return false
191-
}
192-
return true
193-
}
194-
195172
func lookupManifestInDir(dir, name string) (ispec.Manifest, error) {
196173
oci, err := umoci.OpenLayout(dir)
197174
if err != nil {
@@ -374,7 +351,7 @@ func stripOverlayAttrsUnder(dirPath string) error {
374351
return err
375352
}
376353
p := filepath.Join(dirPath, path)
377-
if lib.IsSymlink(p) {
354+
if isSymlink, _ := lib.IsSymlink(p); isSymlink {
378355
// user.* xattrs "can not" exist on symlinks
379356
return nil
380357
}

pkg/stacker/build.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,30 @@ func (b *Builder) build(s types.Storage, file string) error {
340340
log.Debugf("Dependency Order %v", order)
341341

342342
var oci casext.Engine
343-
if _, statErr := os.Stat(opts.Config.OCIDir); statErr != nil {
344-
oci, err = umoci.CreateLayout(opts.Config.OCIDir)
345-
} else {
343+
ocidirstat, statErr := os.Stat(opts.Config.OCIDir)
344+
345+
// if it exists, it is a directory, and it has an index.json, try openlayout
346+
// otherwise try createlayout
347+
348+
if statErr == nil {
349+
if !ocidirstat.IsDir() {
350+
return errors.Errorf("parameter oci-dir=%q exists but is not a directory.", opts.Config.OCIDir)
351+
}
352+
if !lib.PathExists(filepath.Join(opts.Config.OCIDir, "index.json")) {
353+
return errors.Errorf("parameter oci-dir=%q exists but does not look like an OCI Layout.", opts.Config.OCIDir)
354+
}
355+
346356
oci, err = umoci.OpenLayout(opts.Config.OCIDir)
347-
}
348-
if err != nil {
349-
return err
357+
if err != nil {
358+
return errors.Wrapf(err, "could not open OCI layout at %q", opts.Config.OCIDir)
359+
}
360+
} else {
361+
log.Infof("Creating new OCI Layout at %q", opts.Config.OCIDir)
362+
oci, err = umoci.CreateLayout(opts.Config.OCIDir)
363+
if err != nil {
364+
return errors.Wrapf(err, "could not create layout at %q", opts.Config.OCIDir)
365+
}
366+
350367
}
351368
defer oci.Close()
352369

0 commit comments

Comments
 (0)