Ignore "non-existing" errors when getDirectorySize calculates the size (#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.

Fix #26765
This commit is contained in:
wxiaoguang 2023-11-29 13:08:58 +08:00 committed by GitHub
parent 64cd6e8df5
commit b348424c64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -160,28 +160,25 @@ const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | o
// getDirectorySize returns the disk consumption for a given path // getDirectorySize returns the disk consumption for a given path
func getDirectorySize(path string) (int64, error) { func getDirectorySize(path string) (int64, error) {
var size int64 var size int64
err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error { err := filepath.WalkDir(path, func(_ string, entry os.DirEntry, err error) error {
if err != nil { if os.IsNotExist(err) { // ignore the error because some files (like temp/lock file) may be deleted during traversing.
if os.IsNotExist(err) { // ignore the error because the file maybe deleted during traversing. return nil
} else if err != nil {
return err
}
if entry.IsDir() {
return nil return nil
} }
return err info, err := entry.Info()
} if os.IsNotExist(err) { // ignore the error as above
fileName := info.Name()
// Ignore temporary Git files as they will like be missing once info.Info is
// called and cause a disrupt to the whole operation.
if info.IsDir() || strings.HasSuffix(fileName, ".lock") || strings.HasPrefix(filepath.Base(fileName), "tmp_graph") {
return nil return nil
} } else if err != nil {
f, err := info.Info()
if err != nil {
return err return err
} }
if (f.Mode() & notRegularFileMode) == 0 { if (info.Mode() & notRegularFileMode) == 0 {
size += f.Size() size += info.Size()
} }
return err return nil
}) })
return size, err return size, err
} }