Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Jan 19, 2024
1 parent d41baa3 commit adeb4a8
Show file tree
Hide file tree
Showing 31 changed files with 142 additions and 180 deletions.
44 changes: 16 additions & 28 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Command mkuimage builds CPIO archives with the given files and Go commands.
package main

import (
Expand All @@ -27,21 +28,21 @@ import (
"github.com/u-root/uio/ulog"
)

// multiFlag is used for flags that support multiple invocations, e.g. -files
// multiFlag is used for flags that support multiple invocations, e.g. -files.
type multiFlag []string

func (m *multiFlag) String() string {
return fmt.Sprint(*m)
}

// Set implements flag.Value.Set.
func (m *multiFlag) Set(value string) error {
*m = append(*m, value)
return nil
}

// errors from the u-root command
var (
ErrEmptyFilesArg = errors.New("empty argument to -files")
errEmptyFilesArg = errors.New("empty argument to -files")
)

// Flags for u-root builder.
Expand All @@ -55,10 +56,7 @@ var (
statsOutputPath *string
statsLabel *string
shellbang *bool
// For the new gobusybox support
usegobusybox *bool
genDir *string
// For the new "filepath only" logic
// For the new "filepath only" logic.
urootSourceDir *string
)

Expand Down Expand Up @@ -93,9 +91,6 @@ func init() {
statsOutputPath = flag.String("stats-output-path", "", "Write build stats to this file (JSON)")
statsLabel = flag.String("stats-label", "", "Use this statsLabel when writing stats")

// Flags for the gobusybox, which we hope to move to, since it works with modules.
genDir = flag.String("gen-dir", "", "Directory to generate source in")

// Flag for the new filepath only mode. This will be required to find the u-root commands and make templates work
// In almost every case, "." is fine.
urootSourceDir = flag.String("uroot-source", ".", "Path to the locally checked out u-root source tree in case commands from there are desired.")
Expand All @@ -110,8 +105,12 @@ type buildStats struct {

func writeBuildStats(stats buildStats, path string) error {
var allStats []buildStats
if data, err := os.ReadFile(*statsOutputPath); err == nil {
json.Unmarshal(data, &allStats)
data, err := os.ReadFile(*statsOutputPath)
if err != nil {
return err
}
if err := json.Unmarshal(data, &allStats); err != nil {
return err
}
found := false
for i, s := range allStats {
Expand All @@ -127,14 +126,11 @@ func writeBuildStats(stats buildStats, path string) error {
return strings.Compare(allStats[i].Label, allStats[j].Label) == -1
})
}
data, err := json.MarshalIndent(allStats, "", " ")
data, err = json.MarshalIndent(allStats, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(*statsOutputPath, data, 0o644); err != nil {
return err
}
return nil
return os.WriteFile(*statsOutputPath, data, 0o644)
}

func generateLabel(env *golang.Environ) string {
Expand Down Expand Up @@ -166,14 +162,14 @@ func checkArgs(args ...string) error {
}

if args[len(args)-1] == "-files" {
return fmt.Errorf("last argument is -files:%w", ErrEmptyFilesArg)
return fmt.Errorf("last argument is -files:%w", errEmptyFilesArg)
}

// We know the last arg is not -files; scan the arguments for -files
// followed by a switch.
for i := 0; i < len(args)-1; i++ {
if args[i] == "-files" && args[i+1][0] == '-' {
return fmt.Errorf("-files argument %d is followed by a switch: %w", i, ErrEmptyFilesArg)
return fmt.Errorf("-files argument %d is followed by a switch: %w", i, errEmptyFilesArg)
}
}

Expand Down Expand Up @@ -219,7 +215,7 @@ func main() {
l.Fatalf("Build error: %v", err)
}

elapsed := time.Now().Sub(start)
elapsed := time.Since(start)

stats := buildStats{
Label: *statsLabel,
Expand Down Expand Up @@ -256,14 +252,6 @@ func isRecommendedVersion(v string) bool {
return false
}

func canFindSource(dir string) error {
d := filepath.Join(dir, "cmds", "core")
if _, err := os.Stat(d); err != nil {
return fmt.Errorf("can not build u-root in %q:%w (-uroot-source may be incorrect or not set)", *urootSourceDir, os.ErrNotExist)
}
return nil
}

// Main is a separate function so defers are run on return, which they wouldn't
// on exit.
func Main(l ulog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error {
Expand Down
29 changes: 15 additions & 14 deletions cmd/mkuimage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestUrootCmdline(t *testing.T) {
env: []string{"GO111MODULE=off"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bash"},
itest.HasFile{Path: "bin/bash"},
},
},
{
Expand All @@ -65,35 +65,35 @@ func TestUrootCmdline(t *testing.T) {
env: []string{"GO111MODULE=off"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasFile{"/bin/foo"},
itest.HasFile{"/bin/bar"},
itest.HasFile{Path: "/bin/foo"},
itest.HasFile{Path: "/bin/bar"},
},
},
{
name: "include multiple extra files",
args: []string{"-nocmd", "-files=/bin/bash", "-files=/bin/ls", fmt.Sprintf("-files=%s", samplef.Name())},
env: []string{"GO111MODULE=off"},
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bash"},
itest.HasFile{"bin/ls"},
itest.HasFile{samplef.Name()},
itest.HasFile{Path: "bin/bash"},
itest.HasFile{Path: "bin/ls"},
itest.HasFile{Path: samplef.Name()},
},
},
{
name: "include one extra file with rename",
args: []string{"-nocmd", "-files=/bin/bash:bin/bush"},
env: []string{"GO111MODULE=off"},
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bush"},
itest.HasFile{Path: "bin/bush"},
},
},
{
name: "supplied file can be uinit",
args: []string{"-nocmd", "-files=/bin/bash:bin/bash", "-uinitcmd=/bin/bash"},
env: []string{"GO111MODULE=off"},
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bash"},
itest.HasRecord{cpio.Symlink("bin/uinit", "bash")},
itest.HasFile{Path: "bin/bash"},
itest.HasRecord{R: cpio.Symlink("bin/uinit", "bash")},
},
},
}
Expand All @@ -104,7 +104,7 @@ func TestUrootCmdline(t *testing.T) {
args: []string{"-uinitcmd=echo foobar fuzz", "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", "github.com/u-root/u-root/cmds/core/echo"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasRecord{cpio.Symlink("bin/uinit", "../bbin/echo")},
itest.HasRecord{R: cpio.Symlink("bin/uinit", "../bbin/echo")},
itest.HasContent{
Path: "etc/uinit.flags",
Content: "\"foobar\"\n\"fuzz\"",
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestUrootCmdline(t *testing.T) {
var bbTests []testCase
for _, test := range bareTests {
gbbTest := test
gbbTest.name = gbbTest.name + " gbb-gomodule"
gbbTest.name += " gbb-gomodule"
gbbTest.args = append([]string{"-build=gbb"}, gbbTest.args...)
gbbTest.env = append(gbbTest.env, "GO111MODULE=on")

Expand Down Expand Up @@ -289,6 +289,7 @@ func TestUrootCmdline(t *testing.T) {
}

func buildIt(t *testing.T, args, env []string, want error) (*os.File, []byte, error) {
t.Helper()
f, err := os.CreateTemp("", "u-root-")
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -321,9 +322,9 @@ func TestCheckArgs(t *testing.T) {
args []string
err error
}{
{"-files is only arg", []string{"-files"}, ErrEmptyFilesArg},
{"-files followed by -files", []string{"-files", "-files"}, ErrEmptyFilesArg},
{"-files followed by any other switch", []string{"-files", "-abc"}, ErrEmptyFilesArg},
{"-files is only arg", []string{"-files"}, errEmptyFilesArg},
{"-files followed by -files", []string{"-files", "-files"}, errEmptyFilesArg},
{"-files followed by any other switch", []string{"-files", "-abc"}, errEmptyFilesArg},
{"no args", []string{}, nil},
{"u-root alone", []string{"u-root"}, nil},
{"u-root with -files and other args", []string{"u-root", "-files", "/bin/bash", "core"}, nil},
Expand Down
8 changes: 5 additions & 3 deletions cpio/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ func InMemArchive() *Archive {
}

// ArchiveFromRecords creates a new Archive from the records.
func ArchiveFromRecords(rs []Record) *Archive {
func ArchiveFromRecords(rs []Record) (*Archive, error) {
a := InMemArchive()
for _, r := range rs {
a.WriteRecord(r)
if err := a.WriteRecord(r); err != nil {
return nil, err
}
}
return a
return a, nil
}

// ArchiveFromReader reads records from r into a new Archive in memory.
Expand Down
11 changes: 5 additions & 6 deletions cpio/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func FuzzWriteReadInMemArchive(f *testing.F) {
recs := []Record{}
var i uint64
for i = 0; i < fileCount; i++ {

recs = append(recs, StaticRecord(content, Info{
Ino: ino | i,
Mode: syscall.S_IFREG | mode | i,
Expand All @@ -40,24 +39,24 @@ func FuzzWriteReadInMemArchive(f *testing.F) {
}))
}

arch := ArchiveFromRecords(recs)
archReader := arch.Reader()
arch, err := ArchiveFromRecords(recs)
if err != nil {
t.Fatal(err)
}

archReader := arch.Reader()
for _, rec := range recs {
readRec, err := archReader.ReadRecord()

if err != nil {
t.Fatalf("failed to read record from archive")
}

if !Equal(rec, readRec) {
t.Fatalf("records not equal: %v %v", rec, readRec)
}

if !arch.Contains(rec) {
t.Fatalf("record not in archive %v %#v", rec, arch)
}
}

})
}
1 change: 1 addition & 0 deletions cpio/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cpio
// But we are unable to import the unix package when plan 9 is enabled,
// so lucky us, the numbers have been the same for half a century.
// It is ok to just define them.
// nolint
const (
S_IEXEC = 0x40
S_IFBLK = 0x6000
Expand Down
30 changes: 0 additions & 30 deletions cpio/cpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,3 @@ func Format(name string) (RecordFormat, error) {
}
return op, nil
}

func modeFromLinux(mode uint64) os.FileMode {
m := os.FileMode(mode & 0o777)
switch mode & S_IFMT {
case S_IFBLK:
m |= os.ModeDevice
case S_IFCHR:
m |= os.ModeDevice | os.ModeCharDevice
case S_IFDIR:
m |= os.ModeDir
case S_IFIFO:
m |= os.ModeNamedPipe
case S_IFLNK:
m |= os.ModeSymlink
case S_IFREG:
// nothing to do
case S_IFSOCK:
m |= os.ModeSocket
}
if mode&S_ISGID != 0 {
m |= os.ModeSetgid
}
if mode&S_ISUID != 0 {
m |= os.ModeSetuid
}
if mode&S_ISVTX != 0 {
m |= os.ModeSticky
}
return m
}
7 changes: 2 additions & 5 deletions cpio/fs_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"path/filepath"
"syscall"

"github.com/u-root/uio/uio"
"github.com/u-root/mkuimage/upath"
"github.com/u-root/uio/uio"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -51,10 +51,7 @@ func setModes(r Record) error {
if err := os.Chown(r.Name, int(r.UID), int(r.GID)); err != nil {
return err
}
if err := os.Chmod(r.Name, toFileMode(r)); err != nil {
return err
}
return nil
return os.Chmod(r.Name, toFileMode(r))
}

func toFileMode(r Record) os.FileMode {
Expand Down
4 changes: 2 additions & 2 deletions cpio/newc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func headerFromInfo(i Info) header {
return h
}

func (h header) Info() Info {
func (h header) info() Info {
var i Info
i.Ino = uint64(h.Ino)
i.Mode = uint64(h.Mode)
Expand Down Expand Up @@ -307,7 +307,7 @@ func (r *reader) ReadRecord() (Record, error) {
return Record{}, err
}

info := hdr.Info()
info := hdr.info()
info.Name = Normalize(string(nameBuf[:hdr.NameLength-1]))

recLen := uint64(r.pos - recPos)
Expand Down
Loading

0 comments on commit adeb4a8

Please sign in to comment.