aboutsummaryrefslogtreecommitdiffstats
path: root/modules/repository
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-01-14 02:54:02 +0800
committerGitHub <noreply@github.com>2023-01-13 18:54:02 +0000
commita3ab82e5924196632f1731ae3ef2099bf9d39501 (patch)
tree7b4f840dae95f4f5b2a3ff11d822e3fdf45016e6 /modules/repository
parent4fc1517da0014ea4414ac51d67b26553c30139a1 (diff)
downloadgitea-a3ab82e5924196632f1731ae3ef2099bf9d39501.tar.gz
gitea-a3ab82e5924196632f1731ae3ef2099bf9d39501.zip
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>
Diffstat (limited to 'modules/repository')
-rw-r--r--modules/repository/create.go32
-rw-r--r--modules/repository/create_test.go10
2 files changed, 40 insertions, 2 deletions
diff --git a/modules/repository/create.go b/modules/repository/create.go
index 1e157ec85e..454a192ddb 100644
--- a/modules/repository/create.go
+++ b/modules/repository/create.go
@@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path"
+ "path/filepath"
"strings"
"code.gitea.io/gitea/models"
@@ -285,9 +286,36 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m
return repo, nil
}
-// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize
+const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
+
+// getDirectorySize returns the disk consumption for a given path
+func getDirectorySize(path string) (int64, error) {
+ var size int64
+ err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error {
+ if err != nil {
+ if os.IsNotExist(err) { // ignore the error because the file maybe deleted during traversing.
+ return nil
+ }
+ return err
+ }
+ if info.IsDir() {
+ return nil
+ }
+ f, err := info.Info()
+ if err != nil {
+ return err
+ }
+ if (f.Mode() & notRegularFileMode) == 0 {
+ size += f.Size()
+ }
+ return err
+ })
+ return size, err
+}
+
+// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
- size, err := util.GetDirectorySize(repo.RepoPath())
+ size, err := getDirectorySize(repo.RepoPath())
if err != nil {
return fmt.Errorf("updateSize: %w", err)
}
diff --git a/modules/repository/create_test.go b/modules/repository/create_test.go
index da4a738b64..293071bdce 100644
--- a/modules/repository/create_test.go
+++ b/modules/repository/create_test.go
@@ -168,3 +168,13 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
assert.NoError(t, err)
assert.True(t, act.IsPrivate)
}
+
+func TestGetDirectorySize(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 1)
+ assert.NoError(t, err)
+
+ size, err := getDirectorySize(repo.RepoPath())
+ assert.NoError(t, err)
+ assert.EqualValues(t, size, repo.Size)
+}