diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-07 16:24:02 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-09 12:50:01 +0300 |
commit | ce10e8f6e75b039430fc68e407bf21d6ac3c4f37 (patch) | |
tree | 42cca6bf4467b752c52ed1d68d83ec084f9da9fa | |
parent | 6faed425b2109f86dc15b249294dd74b7da67cf7 (diff) |
housekeeping: Rearrange code to improve readability
Rearrange the housekeeping code to improve visibility and locate
functions next to each other which require each other. This is in
preparation of some code changes.
-rw-r--r-- | internal/git/housekeeping/housekeeping.go | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/internal/git/housekeeping/housekeeping.go b/internal/git/housekeeping/housekeeping.go index 7dfc57c60..6d2f4a510 100644 --- a/internal/git/housekeeping/housekeeping.go +++ b/internal/git/housekeeping/housekeeping.go @@ -11,7 +11,10 @@ import ( log "github.com/sirupsen/logrus" ) -const deleteTempFilesOlderThanDuration = 7 * 24 * time.Hour +const ( + deleteTempFilesOlderThanDuration = 7 * 24 * time.Hour + minimumDirPerm = 0700 +) // Perform will perform housekeeping duties on a repository func Perform(ctx context.Context, repoPath string) error { @@ -63,8 +66,26 @@ func Perform(ctx context.Context, repoPath string) error { return err } -func myLogger(ctx context.Context) *log.Entry { - return ctxlogrus.Extract(ctx).WithField("system", "housekeeping") +// Delete a directory structure while ensuring the current user has permission to delete the directory structure +func forceRemove(ctx context.Context, path string) error { + err := os.RemoveAll(path) + if err == nil { + return nil + } + + // Delete failed. Try again after chmod'ing directories recursively + if err := FixDirectoryPermissions(ctx, path); err != nil { + return err + } + + return os.RemoveAll(path) +} + +func shouldRemove(path string, modTime time.Time, mode os.FileMode) bool { + base := filepath.Base(path) + + // Only delete entries starting with `tmp_` and older than a week + return strings.HasPrefix(base, "tmp_") && time.Since(modTime) >= deleteTempFilesOlderThanDuration } // FixDirectoryPermissions does a recursive directory walk to look for @@ -75,8 +96,6 @@ func FixDirectoryPermissions(ctx context.Context, path string) error { return fixDirectoryPermissions(ctx, path, make(map[string]struct{})) } -const minimumDirPerm = 0700 - func fixDirectoryPermissions(ctx context.Context, path string, retriedPaths map[string]struct{}) error { logger := myLogger(ctx) return filepath.Walk(path, func(path string, info os.FileInfo, errIncoming error) error { @@ -105,24 +124,6 @@ func fixDirectoryPermissions(ctx context.Context, path string, retriedPaths map[ }) } -// Delete a directory structure while ensuring the current user has permission to delete the directory structure -func forceRemove(ctx context.Context, path string) error { - err := os.RemoveAll(path) - if err == nil { - return nil - } - - // Delete failed. Try again after chmod'ing directories recursively - if err := FixDirectoryPermissions(ctx, path); err != nil { - return err - } - - return os.RemoveAll(path) -} - -func shouldRemove(path string, modTime time.Time, mode os.FileMode) bool { - base := filepath.Base(path) - - // Only delete entries starting with `tmp_` and older than a week - return strings.HasPrefix(base, "tmp_") && time.Since(modTime) >= deleteTempFilesOlderThanDuration +func myLogger(ctx context.Context) *log.Entry { + return ctxlogrus.Extract(ctx).WithField("system", "housekeeping") } |