purge.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "os"
  17. "path/filepath"
  18. "sort"
  19. "strings"
  20. "time"
  21. "go.uber.org/zap"
  22. )
  23. func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
  24. return purgeFile(lg, dirname, suffix, max, interval, stop, nil)
  25. }
  26. // purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil.
  27. func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error {
  28. errC := make(chan error, 1)
  29. go func() {
  30. for {
  31. fnames, err := ReadDir(dirname)
  32. if err != nil {
  33. errC <- err
  34. return
  35. }
  36. newfnames := make([]string, 0)
  37. for _, fname := range fnames {
  38. if strings.HasSuffix(fname, suffix) {
  39. newfnames = append(newfnames, fname)
  40. }
  41. }
  42. sort.Strings(newfnames)
  43. fnames = newfnames
  44. for len(newfnames) > int(max) {
  45. f := filepath.Join(dirname, newfnames[0])
  46. l, err := TryLockFile(f, os.O_WRONLY, PrivateFileMode)
  47. if err != nil {
  48. break
  49. }
  50. if err = os.Remove(f); err != nil {
  51. errC <- err
  52. return
  53. }
  54. if err = l.Close(); err != nil {
  55. if lg != nil {
  56. lg.Warn("failed to unlock/close", zap.String("path", l.Name()), zap.Error(err))
  57. } else {
  58. plog.Errorf("error unlocking %s when purging file (%v)", l.Name(), err)
  59. }
  60. errC <- err
  61. return
  62. }
  63. if lg != nil {
  64. lg.Info("purged", zap.String("path", f))
  65. } else {
  66. plog.Infof("purged file %s successfully", f)
  67. }
  68. newfnames = newfnames[1:]
  69. }
  70. if purgec != nil {
  71. for i := 0; i < len(fnames)-len(newfnames); i++ {
  72. purgec <- fnames[i]
  73. }
  74. }
  75. select {
  76. case <-time.After(interval):
  77. case <-stop:
  78. return
  79. }
  80. }
  81. }()
  82. return errC
  83. }