From e634e8c9d54d84ef9d76720823352bfa8dc9367a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 11 Jan 2023 00:23:18 +0800 Subject: [PATCH 1/4] Fix error when calculate the repository size --- models/fixtures/repository.yml | 2 +- models/repo/update.go | 2 +- modules/repository/create.go | 26 ++++++++++++++++++++++++-- modules/repository/create_test.go | 10 ++++++++++ modules/util/path.go | 14 +------------- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index 19b243fb4e4e5..5d4781ad44f7c 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -24,7 +24,7 @@ fork_id: 0 is_template: false template_id: 0 - size: 0 + size: 6708 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false diff --git a/models/repo/update.go b/models/repo/update.go index 3aef280ff80bc..8dd1097450e4a 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -184,7 +184,7 @@ func ChangeRepositoryName(doer *user_model.User, repo *Repository, newRepoName s return committer.Commit() } -// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize +// UpdateRepoSize updates the repository size, calculating it using getDirectorySize func UpdateRepoSize(ctx context.Context, repoID, size int64) error { _, err := db.GetEngine(ctx).ID(repoID).Cols("size").NoAutoTime().Update(&Repository{ Size: size, diff --git a/modules/repository/create.go b/modules/repository/create.go index 1e157ec85eff8..c2963d978de8a 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,30 @@ 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.FileMode = 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 os.IsNotExist(err) || info.IsDir() { + return nil + } + f, err := info.Info() + if err != nil { + return err + } + if info != nil && (f.Mode()¬RegularFileMode) == 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 da4a738b64976..293071bdce8e0 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) +} diff --git a/modules/util/path.go b/modules/util/path.go index 03013f713395e..ef5ae661a6eed 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -22,19 +22,7 @@ func EnsureAbsolutePath(path, absoluteBase string) string { return filepath.Join(absoluteBase, path) } -const notRegularFileMode os.FileMode = 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.Walk(path, func(_ string, info os.FileInfo, err error) error { - if info != nil && (info.Mode()¬RegularFileMode) == 0 { - size += info.Size() - } - return err - }) - return size, err -} + // IsDir returns true if given path is a directory, // or returns false when it's a file or does not exist. From 2696577f7850e04c597ac4d552c8d33b0a747544 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 11 Jan 2023 09:52:58 +0800 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Jason Song --- modules/repository/create.go | 2 +- modules/util/path.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/repository/create.go b/modules/repository/create.go index c2963d978de8a..e723a7db4f0a5 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -286,7 +286,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m return repo, nil } -const notRegularFileMode os.FileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular +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) { diff --git a/modules/util/path.go b/modules/util/path.go index ef5ae661a6eed..134ce2bae08ec 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -23,7 +23,6 @@ func EnsureAbsolutePath(path, absoluteBase string) string { } - // IsDir returns true if given path is a directory, // or returns false when it's a file or does not exist. func IsDir(dir string) (bool, error) { From a86112884fa835335a6d761fdd7a0bc6248acba9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 11 Jan 2023 09:53:26 +0800 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Jason Song --- modules/util/path.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/util/path.go b/modules/util/path.go index 134ce2bae08ec..e060b527f3f1e 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -22,7 +22,6 @@ func EnsureAbsolutePath(path, absoluteBase string) string { return filepath.Join(absoluteBase, path) } - // IsDir returns true if given path is a directory, // or returns false when it's a file or does not exist. func IsDir(dir string) (bool, error) { From 438fc931920f638ffc4259d99cbaeaa0de12e362 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 13 Jan 2023 15:58:36 +0800 Subject: [PATCH 4/4] Fix bug --- modules/repository/create.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/repository/create.go b/modules/repository/create.go index c2963d978de8a..4fa00f3e16ea2 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -292,14 +292,20 @@ const notRegularFileMode os.FileMode = os.ModeSymlink | os.ModeNamedPipe | os.Mo func getDirectorySize(path string) (int64, error) { var size int64 err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error { - if os.IsNotExist(err) || info.IsDir() { + 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 info != nil && (f.Mode()¬RegularFileMode) == 0 { + if (f.Mode() & notRegularFileMode) == 0 { size += f.Size() } return err