diff --git a/uroot/initramfs/archive.go b/uroot/initramfs/archive.go index 9d8f1a4..893e570 100644 --- a/uroot/initramfs/archive.go +++ b/uroot/initramfs/archive.go @@ -6,11 +6,17 @@ package initramfs import ( + "errors" "io" "github.com/u-root/mkuimage/cpio" ) +// Possible errors. +var ( + ErrNoPath = errors.New("invalid argument: must specify path") +) + // ReadOpener opens a cpio.RecordReader. type ReadOpener interface { OpenReader() (cpio.RecordReader, error) diff --git a/uroot/initramfs/cpio.go b/uroot/initramfs/cpio.go index 791354e..bbe876f 100644 --- a/uroot/initramfs/cpio.go +++ b/uroot/initramfs/cpio.go @@ -22,7 +22,7 @@ var _ WriteOpener = &CPIOFile{} // OpenWriter opens c.Path for writing. func (c *CPIOFile) OpenWriter() (Writer, error) { if len(c.Path) == 0 { - return nil, fmt.Errorf("path is required") + return nil, fmt.Errorf("failed to write to CPIO: %w", ErrNoPath) } f, err := os.OpenFile(c.Path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) if err != nil { @@ -34,7 +34,7 @@ func (c *CPIOFile) OpenWriter() (Writer, error) { // OpenReader opens c.Path for reading. func (c *CPIOFile) OpenReader() (cpio.RecordReader, error) { if len(c.Path) == 0 { - return nil, fmt.Errorf("path is required") + return nil, fmt.Errorf("failed to read from CPIO: %w", ErrNoPath) } f, err := os.Open(c.Path) if err != nil { diff --git a/uroot/initramfs/dir.go b/uroot/initramfs/dir.go index bc847c6..7108803 100644 --- a/uroot/initramfs/dir.go +++ b/uroot/initramfs/dir.go @@ -22,7 +22,7 @@ var _ WriteOpener = &Dir{} // OpenWriter implements Archiver.OpenWriter. func (d *Dir) OpenWriter() (Writer, error) { if len(d.Path) == 0 { - return nil, fmt.Errorf("path is required") + return nil, fmt.Errorf("failed to use directory as output: %w", ErrNoPath) } if err := os.MkdirAll(d.Path, 0o755); err != nil && !errors.Is(err, os.ErrExist) { return nil, err diff --git a/uroot/uroot.go b/uroot/uroot.go index 1121a1d..c85326d 100644 --- a/uroot/uroot.go +++ b/uroot/uroot.go @@ -314,7 +314,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error { // Finally, write the archive. if err := initramfs.Write(archive); err != nil { - return fmt.Errorf("error archiving: %v", err) + return fmt.Errorf("error archiving: %w", err) } return nil } diff --git a/uroot/uroot_test.go b/uroot/uroot_test.go index 38dca11..bd62057 100644 --- a/uroot/uroot_test.go +++ b/uroot/uroot_test.go @@ -405,10 +405,57 @@ func TestCreateInitramfs(t *testing.T) { itest.IsEmpty{}, }, }, + { + name: "cpio no path given", + opts: Opts{ + TempDir: dir, + InitCmd: "/bin/systemd", + OutputFile: &initramfs.CPIOFile{}, + }, + errs: []error{initramfs.ErrNoPath}, + }, + { + name: "dir no path given", + opts: Opts{ + TempDir: dir, + InitCmd: "/bin/systemd", + OutputFile: &initramfs.Dir{}, + }, + errs: []error{initramfs.ErrNoPath}, + }, + { + name: "dir failed to create", + opts: Opts{ + TempDir: dir, + InitCmd: "/bin/systemd", + OutputFile: &initramfs.Dir{Path: filepath.Join(tmp400, "foobar")}, + }, + errs: []error{os.ErrPermission}, + }, + { + name: "cpio failed to create", + opts: Opts{ + TempDir: dir, + InitCmd: "/bin/systemd", + OutputFile: &initramfs.CPIOFile{Path: filepath.Join(tmp400, "foobar")}, + }, + errs: []error{os.ErrPermission}, + }, + { + name: "cpio basefile no path given", + opts: Opts{ + TempDir: dir, + InitCmd: "/bin/systemd", + BaseArchive: &initramfs.CPIOFile{}, + }, + errs: []error{initramfs.ErrNoPath}, + }, } { t.Run(fmt.Sprintf("Test %d [%s]", i, tt.name), func(t *testing.T) { archive := cpio.InMemArchive() - tt.opts.OutputFile = &initramfs.Archive{Archive: archive} + if tt.opts.OutputFile == nil { + tt.opts.OutputFile = &initramfs.Archive{Archive: archive} + } err := CreateInitramfs(l, tt.opts) for _, want := range tt.errs { if !errors.Is(err, want) {