diff options
author | Andrew <write@imaginarycode.com> | 2017-02-10 23:00:46 -0500 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-11 12:00:46 +0800 |
commit | 42835c7f82ac379d5d583fa8406cff529b9d0e9e (patch) | |
tree | 87dc278aa0261a93b65bd06a39db7688f030a288 /models | |
parent | cf0f451c37496c84795e98191a20350e0f5be35c (diff) | |
download | gitea-42835c7f82ac379d5d583fa8406cff529b9d0e9e.tar.gz gitea-42835c7f82ac379d5d583fa8406cff529b9d0e9e.zip |
Implement archive cleanup (#885)
* Implement archive cleanup
Fixes #769
Signed-off-by: Andrew <write@imaginarycode.com>
* Make sure to close the directory file
* Resolve issues noted by @strk
* edit cheatsheet app.ini [ci skip]
* oops [ci skip]
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/models/repo.go b/models/repo.go index 9b1b868778..a7a36cc7d0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1837,6 +1837,60 @@ func DeleteRepositoryArchives() error { }) } +// DeleteOldRepositoryArchives deletes old repository archives. +func DeleteOldRepositoryArchives() { + if taskStatusTable.IsRunning(archiveCleanup) { + return + } + taskStatusTable.Start(archiveCleanup) + defer taskStatusTable.Stop(archiveCleanup) + + log.Trace("Doing: ArchiveCleanup") + + if err := x.Where("id > 0").Iterate(new(Repository), deleteOldRepositoryArchives); err != nil { + log.Error(4, "ArchiveClean: %v", err) + } +} + +func deleteOldRepositoryArchives(idx int, bean interface{}) error { + repo := bean.(*Repository) + basePath := filepath.Join(repo.RepoPath(), "archives") + + for _, ty := range []string{"zip", "targz"} { + path := filepath.Join(basePath, ty) + file, err := os.Open(path) + if err != nil { + if !os.IsNotExist(err) { + log.Warn("Unable to open directory %s: %v", path, err) + return err + } + + // If the directory doesn't exist, that's okay. + continue + } + + files, err := file.Readdir(0) + file.Close() + if err != nil { + log.Warn("Unable to read directory %s: %v", path, err) + return err + } + + minimumOldestTime := time.Now().Add(-setting.Cron.ArchiveCleanup.OlderThan) + for _, info := range files { + if info.ModTime().Before(minimumOldestTime) && !info.IsDir() { + toDelete := filepath.Join(path, info.Name()) + // This is a best-effort purge, so we do not check error codes to confirm removal. + if err = os.Remove(toDelete); err != nil { + log.Trace("Unable to delete %s, but proceeding: %v", toDelete, err) + } + } + } + } + + return nil +} + func gatherMissingRepoRecords() ([]*Repository, error) { repos := make([]*Repository, 0, 10) if err := x. @@ -1915,9 +1969,10 @@ func RewriteRepositoryUpdateHook() error { var taskStatusTable = sync.NewStatusTable() const ( - mirrorUpdate = "mirror_update" - gitFsck = "git_fsck" - checkRepos = "check_repos" + mirrorUpdate = "mirror_update" + gitFsck = "git_fsck" + checkRepos = "check_repos" + archiveCleanup = "archive_cleanup" ) // GitFsck calls 'git fsck' to check repository health. |