From 241cb6114844012a15042999b1cf8439bf7ccdb0 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 29 Mar 2020 15:47:38 -0700 Subject: [PATCH 1/2] fix: skip ignored files when calculating size fixes https://github.com/ipfs/go-ipfs/issues/7052 --- serialfile.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/serialfile.go b/serialfile.go index f56fcc6..86af306 100644 --- a/serialfile.go +++ b/serialfile.go @@ -138,13 +138,18 @@ func (f *serialFile) Size() (int64, error) { var du int64 err := filepath.Walk(f.path, func(p string, fi os.FileInfo, err error) error { - if err != nil { + if err != nil || fi == nil { return err } - if fi != nil && fi.Mode().IsRegular() { + if f.filter.ShouldExclude(fi) { + if fi.Mode().IsDir() { + return filepath.SkipDir + } + } else if fi.Mode().IsRegular() { du += fi.Size() } + return nil }) From cc3f8bdd392fa5ebaa5e2b77bffea67336bedf62 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 29 Mar 2020 17:21:06 -0700 Subject: [PATCH 2/2] test: test file size when ignoring Also, fix the file ignore tests. --- serialfile_test.go | 81 +++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/serialfile_test.go b/serialfile_test.go index edd5bb9..ee8da3a 100644 --- a/serialfile_test.go +++ b/serialfile_test.go @@ -71,23 +71,32 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { t.Fatal(err) } } - expectedHiddenPaths := make([]string, 0, 4) - expectedRegularPaths := make([]string, 0, 6) + expectedPaths := make([]string, 0, 4) + expectedSize := int64(0) + +testInputs: for p := range testInputs { - path := filepath.Join(tmppath, p) - stat, err := os.Stat(path) - if err != nil { - t.Fatal(err) - } - if !fileFilter.ShouldExclude(stat) { - if isFullPathHidden(path) { - expectedHiddenPaths = append(expectedHiddenPaths, p) - } else { - expectedRegularPaths = append(expectedRegularPaths, p) + components := strings.Split(p, "/") + var stat os.FileInfo + for i := range components { + stat, err = os.Stat(filepath.Join( + append([]string{tmppath}, components[:i+1]...)..., + )) + if err != nil { + t.Fatal(err) } + if fileFilter.ShouldExclude(stat) { + continue testInputs + } + } + expectedPaths = append(expectedPaths, p) + if stat.Mode().IsRegular() { + expectedSize += stat.Size() } } + sort.Strings(expectedPaths) + stat, err := os.Stat(tmppath) if err != nil { t.Fatal(err) @@ -102,9 +111,14 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { } defer sf.Close() + if size, err := sf.Size(); err != nil { + t.Fatalf("failed to determine size: %s", err) + } else if size != expectedSize { + t.Fatalf("expected size %d, got size %d", expectedSize, size) + } + rootFound := false - actualRegularPaths := make([]string, 0, len(expectedRegularPaths)) - actualHiddenPaths := make([]string, 0, len(expectedHiddenPaths)) + actualPaths := make([]string, 0, len(expectedPaths)) err = Walk(sf, func(path string, nd Node) error { defer nd.Close() @@ -119,16 +133,15 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { rootFound = true return nil } - if isFullPathHidden(path) { - actualHiddenPaths = append(actualHiddenPaths, path) - } else { - actualRegularPaths = append(actualRegularPaths, path) - } + actualPaths = append(actualPaths, path) if !hidden && isFullPathHidden(path) { return fmt.Errorf("found a hidden file") } - if fileFilter.Rules.MatchesPath(path) { - return fmt.Errorf("found a file that should be excluded") + components := filepath.SplitList(path) + for i := range components { + if fileFilter.Rules.MatchesPath(filepath.Join(components[:i+1]...)) { + return fmt.Errorf("found a file that should be excluded") + } } data, ok := testInputs[path] @@ -155,19 +168,27 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) { } return nil }) + if err != nil { + t.Fatal(err) + } if !rootFound { t.Fatal("didn't find the root") } - for _, regular := range expectedRegularPaths { - if idx := sort.SearchStrings(actualRegularPaths, regular); idx < 0 { - t.Errorf("missed regular path %q", regular) - } + + if len(expectedPaths) != len(actualPaths) { + t.Fatalf("expected %d paths, found %d", + len(expectedPaths), + len(actualPaths), + ) } - if hidden && len(actualHiddenPaths) != len(expectedHiddenPaths) { - for _, missing := range expectedHiddenPaths { - if idx := sort.SearchStrings(actualHiddenPaths, missing); idx < 0 { - t.Errorf("missed hidden path %q", missing) - } + + for i := range expectedPaths { + if expectedPaths[i] != actualPaths[i] { + t.Errorf( + "expected path %q does not match actual %q", + expectedPaths[i], + actualPaths[i], + ) } } }