Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit bb6f211

Browse files
authored
Merge pull request #30 from ipfs/fix/skip-files-size
skip ignored files when calculating size
2 parents 642f445 + cc3f8bd commit bb6f211

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

serialfile.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,18 @@ func (f *serialFile) Size() (int64, error) {
138138

139139
var du int64
140140
err := filepath.Walk(f.path, func(p string, fi os.FileInfo, err error) error {
141-
if err != nil {
141+
if err != nil || fi == nil {
142142
return err
143143
}
144144

145-
if fi != nil && fi.Mode().IsRegular() {
145+
if f.filter.ShouldExclude(fi) {
146+
if fi.Mode().IsDir() {
147+
return filepath.SkipDir
148+
}
149+
} else if fi.Mode().IsRegular() {
146150
du += fi.Size()
147151
}
152+
148153
return nil
149154
})
150155

serialfile_test.go

+51-30
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,32 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
7171
t.Fatal(err)
7272
}
7373
}
74-
expectedHiddenPaths := make([]string, 0, 4)
75-
expectedRegularPaths := make([]string, 0, 6)
74+
expectedPaths := make([]string, 0, 4)
75+
expectedSize := int64(0)
76+
77+
testInputs:
7678
for p := range testInputs {
77-
path := filepath.Join(tmppath, p)
78-
stat, err := os.Stat(path)
79-
if err != nil {
80-
t.Fatal(err)
81-
}
82-
if !fileFilter.ShouldExclude(stat) {
83-
if isFullPathHidden(path) {
84-
expectedHiddenPaths = append(expectedHiddenPaths, p)
85-
} else {
86-
expectedRegularPaths = append(expectedRegularPaths, p)
79+
components := strings.Split(p, "/")
80+
var stat os.FileInfo
81+
for i := range components {
82+
stat, err = os.Stat(filepath.Join(
83+
append([]string{tmppath}, components[:i+1]...)...,
84+
))
85+
if err != nil {
86+
t.Fatal(err)
8787
}
88+
if fileFilter.ShouldExclude(stat) {
89+
continue testInputs
90+
}
91+
}
92+
expectedPaths = append(expectedPaths, p)
93+
if stat.Mode().IsRegular() {
94+
expectedSize += stat.Size()
8895
}
8996
}
9097

98+
sort.Strings(expectedPaths)
99+
91100
stat, err := os.Stat(tmppath)
92101
if err != nil {
93102
t.Fatal(err)
@@ -102,9 +111,14 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
102111
}
103112
defer sf.Close()
104113

114+
if size, err := sf.Size(); err != nil {
115+
t.Fatalf("failed to determine size: %s", err)
116+
} else if size != expectedSize {
117+
t.Fatalf("expected size %d, got size %d", expectedSize, size)
118+
}
119+
105120
rootFound := false
106-
actualRegularPaths := make([]string, 0, len(expectedRegularPaths))
107-
actualHiddenPaths := make([]string, 0, len(expectedHiddenPaths))
121+
actualPaths := make([]string, 0, len(expectedPaths))
108122
err = Walk(sf, func(path string, nd Node) error {
109123
defer nd.Close()
110124

@@ -119,16 +133,15 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
119133
rootFound = true
120134
return nil
121135
}
122-
if isFullPathHidden(path) {
123-
actualHiddenPaths = append(actualHiddenPaths, path)
124-
} else {
125-
actualRegularPaths = append(actualRegularPaths, path)
126-
}
136+
actualPaths = append(actualPaths, path)
127137
if !hidden && isFullPathHidden(path) {
128138
return fmt.Errorf("found a hidden file")
129139
}
130-
if fileFilter.Rules.MatchesPath(path) {
131-
return fmt.Errorf("found a file that should be excluded")
140+
components := filepath.SplitList(path)
141+
for i := range components {
142+
if fileFilter.Rules.MatchesPath(filepath.Join(components[:i+1]...)) {
143+
return fmt.Errorf("found a file that should be excluded")
144+
}
132145
}
133146

134147
data, ok := testInputs[path]
@@ -155,19 +168,27 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
155168
}
156169
return nil
157170
})
171+
if err != nil {
172+
t.Fatal(err)
173+
}
158174
if !rootFound {
159175
t.Fatal("didn't find the root")
160176
}
161-
for _, regular := range expectedRegularPaths {
162-
if idx := sort.SearchStrings(actualRegularPaths, regular); idx < 0 {
163-
t.Errorf("missed regular path %q", regular)
164-
}
177+
178+
if len(expectedPaths) != len(actualPaths) {
179+
t.Fatalf("expected %d paths, found %d",
180+
len(expectedPaths),
181+
len(actualPaths),
182+
)
165183
}
166-
if hidden && len(actualHiddenPaths) != len(expectedHiddenPaths) {
167-
for _, missing := range expectedHiddenPaths {
168-
if idx := sort.SearchStrings(actualHiddenPaths, missing); idx < 0 {
169-
t.Errorf("missed hidden path %q", missing)
170-
}
184+
185+
for i := range expectedPaths {
186+
if expectedPaths[i] != actualPaths[i] {
187+
t.Errorf(
188+
"expected path %q does not match actual %q",
189+
expectedPaths[i],
190+
actualPaths[i],
191+
)
171192
}
172193
}
173194
}

0 commit comments

Comments
 (0)