Skip to content

Commit a3ab82e

Browse files
lunnywolfogre
andauthored
Fix error when calculate the repository size (#22392)
Fix #22386 `GetDirectorySize` moved as `getDirectorySize` because it becomes a special function which should not be put in `util`. Co-authored-by: Jason Song <[email protected]>
1 parent 4fc1517 commit a3ab82e

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

models/fixtures/repository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
fork_id: 0
2525
is_template: false
2626
template_id: 0
27-
size: 0
27+
size: 6708
2828
is_fsck_enabled: true
2929
close_issues_via_commit_in_any_branch: false
3030

models/repo/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func ChangeRepositoryName(doer *user_model.User, repo *Repository, newRepoName s
184184
return committer.Commit()
185185
}
186186

187-
// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize
187+
// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
188188
func UpdateRepoSize(ctx context.Context, repoID, size int64) error {
189189
_, err := db.GetEngine(ctx).ID(repoID).Cols("size").NoAutoTime().Update(&Repository{
190190
Size: size,

modules/repository/create.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"path"
11+
"path/filepath"
1112
"strings"
1213

1314
"code.gitea.io/gitea/models"
@@ -285,9 +286,36 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m
285286
return repo, nil
286287
}
287288

288-
// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize
289+
const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
290+
291+
// getDirectorySize returns the disk consumption for a given path
292+
func getDirectorySize(path string) (int64, error) {
293+
var size int64
294+
err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error {
295+
if err != nil {
296+
if os.IsNotExist(err) { // ignore the error because the file maybe deleted during traversing.
297+
return nil
298+
}
299+
return err
300+
}
301+
if info.IsDir() {
302+
return nil
303+
}
304+
f, err := info.Info()
305+
if err != nil {
306+
return err
307+
}
308+
if (f.Mode() & notRegularFileMode) == 0 {
309+
size += f.Size()
310+
}
311+
return err
312+
})
313+
return size, err
314+
}
315+
316+
// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
289317
func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
290-
size, err := util.GetDirectorySize(repo.RepoPath())
318+
size, err := getDirectorySize(repo.RepoPath())
291319
if err != nil {
292320
return fmt.Errorf("updateSize: %w", err)
293321
}

modules/repository/create_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,13 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
168168
assert.NoError(t, err)
169169
assert.True(t, act.IsPrivate)
170170
}
171+
172+
func TestGetDirectorySize(t *testing.T) {
173+
assert.NoError(t, unittest.PrepareTestDatabase())
174+
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 1)
175+
assert.NoError(t, err)
176+
177+
size, err := getDirectorySize(repo.RepoPath())
178+
assert.NoError(t, err)
179+
assert.EqualValues(t, size, repo.Size)
180+
}

modules/util/path.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ func EnsureAbsolutePath(path, absoluteBase string) string {
2222
return filepath.Join(absoluteBase, path)
2323
}
2424

25-
const notRegularFileMode os.FileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
26-
27-
// GetDirectorySize returns the disk consumption for a given path
28-
func GetDirectorySize(path string) (int64, error) {
29-
var size int64
30-
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
31-
if info != nil && (info.Mode()&notRegularFileMode) == 0 {
32-
size += info.Size()
33-
}
34-
return err
35-
})
36-
return size, err
37-
}
38-
3925
// IsDir returns true if given path is a directory,
4026
// or returns false when it's a file or does not exist.
4127
func IsDir(dir string) (bool, error) {

0 commit comments

Comments
 (0)