Skip to content

Commit b348424

Browse files
authored
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
1 parent 64cd6e8 commit b348424

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

modules/repository/create.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +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-
171-
fileName := info.Name()
172-
// Ignore temporary Git files as they will like be missing once info.Info is
173-
// called and cause a disrupt to the whole operation.
174-
if info.IsDir() || strings.HasSuffix(fileName, ".lock") || strings.HasPrefix(filepath.Base(fileName), "tmp_graph") {
169+
if entry.IsDir() {
175170
return nil
176171
}
177-
f, err := info.Info()
178-
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 {
179176
return err
180177
}
181-
if (f.Mode() & notRegularFileMode) == 0 {
182-
size += f.Size()
178+
if (info.Mode() & notRegularFileMode) == 0 {
179+
size += info.Size()
183180
}
184-
return err
181+
return nil
185182
})
186183
return size, err
187184
}

0 commit comments

Comments
 (0)