loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <i@wolfogre.com>

authored by

Lunny Xiao
Jason Song
and committed by
GitHub
a3ab82e5 4fc1517d

+42 -18
+1 -1
models/fixtures/repository.yml
··· 24 24 fork_id: 0 25 25 is_template: false 26 26 template_id: 0 27 - size: 0 27 + size: 6708 28 28 is_fsck_enabled: true 29 29 close_issues_via_commit_in_any_branch: false 30 30
+1 -1
models/repo/update.go
··· 184 184 return committer.Commit() 185 185 } 186 186 187 - // UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize 187 + // UpdateRepoSize updates the repository size, calculating it using getDirectorySize 188 188 func UpdateRepoSize(ctx context.Context, repoID, size int64) error { 189 189 _, err := db.GetEngine(ctx).ID(repoID).Cols("size").NoAutoTime().Update(&Repository{ 190 190 Size: size,
+30 -2
modules/repository/create.go
··· 8 8 "fmt" 9 9 "os" 10 10 "path" 11 + "path/filepath" 11 12 "strings" 12 13 13 14 "code.gitea.io/gitea/models" ··· 285 286 return repo, nil 286 287 } 287 288 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 289 317 func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error { 290 - size, err := util.GetDirectorySize(repo.RepoPath()) 318 + size, err := getDirectorySize(repo.RepoPath()) 291 319 if err != nil { 292 320 return fmt.Errorf("updateSize: %w", err) 293 321 }
+10
modules/repository/create_test.go
··· 168 168 assert.NoError(t, err) 169 169 assert.True(t, act.IsPrivate) 170 170 } 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 + }
-14
modules/util/path.go
··· 22 22 return filepath.Join(absoluteBase, path) 23 23 } 24 24 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 - 39 25 // IsDir returns true if given path is a directory, 40 26 // or returns false when it's a file or does not exist. 41 27 func IsDir(dir string) (bool, error) {