From 3bc8e48538648ef25c378ab9c37b1d210d87877f Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 8 Apr 2022 22:47:26 -0400 Subject: [PATCH 1/4] *.go: go-staticcheck linting Signed-off-by: Vincent Batts --- keywords.go | 4 +--- keywords_linux_test.go | 3 +-- tar.go | 26 -------------------------- tar_test.go | 6 ------ update_linux_test.go | 3 +-- walk.go | 6 +++--- 6 files changed, 6 insertions(+), 42 deletions(-) diff --git a/keywords.go b/keywords.go index 9adb1fd..e7c6a54 100644 --- a/keywords.go +++ b/keywords.go @@ -174,9 +174,7 @@ func keyValDifference(this, that []KeyVal) []KeyVal { } func keyValCopy(set []KeyVal) []KeyVal { ret := make([]KeyVal, len(set)) - for i := range set { - ret[i] = set[i] - } + copy(ret, set) return ret } diff --git a/keywords_linux_test.go b/keywords_linux_test.go index dbaac0a..6e29012 100644 --- a/keywords_linux_test.go +++ b/keywords_linux_test.go @@ -4,7 +4,6 @@ package mtree import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -42,7 +41,7 @@ func TestXattr(t *testing.T) { } if err := xattr.Set(dir, "user.test", []byte("directory")); err != nil { - t.Skip(fmt.Sprintf("skipping: %q does not support xattrs", dir)) + t.Skipf("skipping: %q does not support xattrs", dir) } if err := xattr.Set(filepath.Join(dir, "file"), "user.test", []byte("regular file")); err != nil { t.Fatal(err) diff --git a/tar.go b/tar.go index 51e251a..ce18344 100644 --- a/tar.go +++ b/tar.go @@ -373,7 +373,6 @@ func flatten(root *Entry, creator *dhCreator, keywords []Keyword) { } creator.DH.Entries = append(creator.DH.Entries, dotEntry) } - return } // resolveHardlinks goes through an Entry tree, and finds the Entry's associated @@ -409,31 +408,6 @@ func resolveHardlinks(root *Entry, hardlinks map[string][]string, countlinks boo } } -// filter takes in a pointer to an Entry, and returns a slice of Entry's that -// satisfy the predicate p -func filter(root *Entry, p func(*Entry) bool) []Entry { - if root != nil { - var validEntrys []Entry - if len(root.Children) > 0 || root.Prev != nil { - for _, c := range root.Children { - // filter the sub-directory - if c.Prev != nil { - validEntrys = append(validEntrys, filter(c, p)...) - } - if p(c) { - if c.Prev == nil { - validEntrys = append([]Entry{*c}, validEntrys...) - } else { - validEntrys = append(validEntrys, *c) - } - } - } - return validEntrys - } - } - return nil -} - func (ts *tarStream) setErr(err error) { ts.err = err } diff --git a/tar_test.go b/tar_test.go index e4eaa56..1f9408c 100644 --- a/tar_test.go +++ b/tar_test.go @@ -336,12 +336,6 @@ type fakeFile struct { Xattrs map[string]string } -// minimal tar archive that mimics what is in ./testdata/test.tar -var minimalFiles = []fakeFile{ - {"x/", "", 0755, '5', 0, 0, nil}, - {"x/files", "howdy\n", 0644, '0', 0, 0, nil}, -} - func makeTarStream(ff []fakeFile) ([]byte, error) { buf := new(bytes.Buffer) diff --git a/update_linux_test.go b/update_linux_test.go index f51026f..2d23160 100644 --- a/update_linux_test.go +++ b/update_linux_test.go @@ -2,7 +2,6 @@ package mtree import ( "encoding/json" - "fmt" "io/ioutil" "os" "path/filepath" @@ -32,7 +31,7 @@ func TestXattrUpdate(t *testing.T) { } if err := xattr.Set(dir, "user.test", []byte("directory")); err != nil { - t.Skip(fmt.Sprintf("skipping: %q does not support xattrs", dir)) + t.Skipf("skipping: %q does not support xattrs", dir) } if err := xattr.Set(tmpfn, "user.test", []byte("regular file")); err != nil { t.Fatal(err) diff --git a/walk.go b/walk.go index 56b93dc..cf792ef 100644 --- a/walk.go +++ b/walk.go @@ -103,7 +103,7 @@ func Walk(root string, excludes []ExcludeFunc, keywords []Keyword, fsEval FsEval } keyFunc, ok := KeywordFuncs[keyword.Prefix()] if !ok { - return fmt.Errorf("Unknown keyword %q for file %q", keyword.Prefix(), path) + return fmt.Errorf("unknown keyword %q for file %q", keyword.Prefix(), path) } kvs, err := creator.fs.KeywordFunc(keyFunc)(path, info, r) if err != nil { @@ -138,7 +138,7 @@ func Walk(root string, excludes []ExcludeFunc, keywords []Keyword, fsEval FsEval } keyFunc, ok := KeywordFuncs[keyword.Prefix()] if !ok { - return fmt.Errorf("Unknown keyword %q for file %q", keyword.Prefix(), path) + return fmt.Errorf("unknown keyword %q for file %q", keyword.Prefix(), path) } kvs, err := creator.fs.KeywordFunc(keyFunc)(path, info, r) if err != nil { @@ -198,7 +198,7 @@ func Walk(root string, excludes []ExcludeFunc, keywords []Keyword, fsEval FsEval } keyFunc, ok := KeywordFuncs[keyword.Prefix()] if !ok { - return fmt.Errorf("Unknown keyword %q for file %q", keyword.Prefix(), path) + return fmt.Errorf("unknown keyword %q for file %q", keyword.Prefix(), path) } kvs, err := creator.fs.KeywordFunc(keyFunc)(path, info, r) if err != nil { From 7b5bbd4eaa42a2fd50f9092fdfd8b2f28c16ff78 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 8 Apr 2022 22:48:20 -0400 Subject: [PATCH 2/4] keywords: tar.Xattrs is deprecated by PAXRecords Signed-off-by: Vincent Batts --- keywordfuncs_linux.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/keywordfuncs_linux.go b/keywordfuncs_linux.go index 2fd82c2..b88f738 100644 --- a/keywordfuncs_linux.go +++ b/keywordfuncs_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package mtree @@ -69,11 +70,11 @@ var ( } xattrKeywordFunc = func(path string, info os.FileInfo, r io.Reader) ([]KeyVal, error) { if hdr, ok := info.Sys().(*tar.Header); ok { - if len(hdr.Xattrs) == 0 { + if len(hdr.PAXRecords) == 0 { return nil, nil } klist := []KeyVal{} - for k, v := range hdr.Xattrs { + for k, v := range hdr.PAXRecords { encKey, err := govis.Vis(k, DefaultVisFlags) if err != nil { return nil, nil From dac969b14ee02a6fb535cb5aee898764b81af15b Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 8 Apr 2022 22:55:48 -0400 Subject: [PATCH 3/4] Makefile: staticcheck validation Signed-off-by: Vincent Batts --- .gitignore | 2 ++ Makefile | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1ddf2a6..1b78688 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ .lint .test .vet +.staticcheck +.gocyclo gomtree diff --git a/Makefile b/Makefile index ec9b37d..0eb08af 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ default: build validation validation: .test .lint .vet .cli.test .PHONY: validation.tags -validation.tags: .test.tags .vet.tags .cli.test +validation.tags: .test.tags .vet.tags .cli.test .staticcheck .PHONY: gocyclo gocyclo: .gocyclo @@ -24,6 +24,14 @@ CLEAN_FILES += .gocyclo .gocyclo: gocyclo -avg -over 15 -ignore 'vendor/*' . && touch $@ +.PHONY: staticcheck +staticcheck: .staticcheck + +CLEAN_FILES += .staticcheck + +.staticcheck: + staticcheck . && touch $@ + .PHONY: test test: .test @@ -79,6 +87,7 @@ $(BUILD): $(SOURCE_FILES) install.tools: @go install -u github.com/fatih/color@latest ; \ go install -u github.com/fzipp/gocyclo/cmd/gocyclo@latest ; \ + go install -u honnef.co/go/tools/cmd/staticcheck@latest ; \ if [ "$(findstring $(GO_VER),$(shell go version))" != "" ] ; then \ go get -u golang.org/x/lint/golint ;\ fi From 0337cb91ea6bf1f542b43ac8c551c34d7b48c2bd Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 8 Apr 2022 22:56:09 -0400 Subject: [PATCH 4/4] keywordfunc: ignore ripemd160 for compat sake Signed-off-by: Vincent Batts --- keywordfunc.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keywordfunc.go b/keywordfunc.go index 7a8a170..8876ca5 100644 --- a/keywordfunc.go +++ b/keywordfunc.go @@ -12,6 +12,8 @@ import ( "os" "github.com/vbatts/go-mtree/pkg/govis" + + //lint:ignore SA1019 yes ripemd160 is deprecated, but this is for mtree compatibility "golang.org/x/crypto/ripemd160" )