Skip to content

Commit 4f5122a

Browse files
authored
Ignore "non-existing" errors when getDirectorySize calculates the size (#28276) (#28285)
Backport #28276 The git command may operate the git directory (add/remove) files in any time. So when the code iterates the directory, some files may disappear during the "walk". All "IsNotExist" errors should be ignored.
1 parent 84e65af commit 4f5122a

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

modules/repository/create.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,25 @@ const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | o
160160
// getDirectorySize returns the disk consumption for a given path
161161
func getDirectorySize(path string) (int64, error) {
162162
var size int64
163-
err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error {
164-
if err != nil {
165-
if os.IsNotExist(err) { // ignore the error because the file maybe deleted during traversing.
166-
return nil
167-
}
163+
err := filepath.WalkDir(path, func(_ string, entry os.DirEntry, err error) error {
164+
if os.IsNotExist(err) { // ignore the error because some files (like temp/lock file) may be deleted during traversing.
165+
return nil
166+
} else if err != nil {
168167
return err
169168
}
170-
if info.IsDir() {
169+
if entry.IsDir() {
171170
return nil
172171
}
173-
f, err := info.Info()
174-
if err != nil {
172+
info, err := entry.Info()
173+
if os.IsNotExist(err) { // ignore the error as above
174+
return nil
175+
} else if err != nil {
175176
return err
176177
}
177-
if (f.Mode() & notRegularFileMode) == 0 {
178-
size += f.Size()
178+
if (info.Mode() & notRegularFileMode) == 0 {
179+
size += info.Size()
179180
}
180-
return err
181+
return nil
181182
})
182183
return size, err
183184
}

0 commit comments

Comments
 (0)