configdir.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 Yunion
  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 utils
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "regexp"
  21. "strings"
  22. "time"
  23. aggrerrors "yunion.io/x/pkg/util/errors"
  24. )
  25. const (
  26. ConfigDirMode = os.FileMode(0755)
  27. configDirFmt = "20060102.150405.000"
  28. configDirFmtStaging = configDirFmt + ".staging"
  29. )
  30. var (
  31. configDirPat = regexp.MustCompile(`^\d{8}\.\d{6}\.\d{3}$`)
  32. )
  33. type ConfigDirManager struct {
  34. baseDir string
  35. }
  36. // TODO
  37. //
  38. // - set active
  39. func NewConfigDirManager(baseDir string) *ConfigDirManager {
  40. return &ConfigDirManager{
  41. baseDir: baseDir,
  42. }
  43. }
  44. type DirFunc func(string) error
  45. // call f() inside 20180830.104050.333.lbcorpus.json.staging
  46. // move to final name 20180830.104050.333.lbcorpus.json
  47. func (m *ConfigDirManager) NewDir(f DirFunc) (finalDir string, err error) {
  48. stagingDir := m.stagingDir()
  49. if stagingDir == "" {
  50. err = fmt.Errorf("failed creating staging dir")
  51. return
  52. }
  53. defer func() {
  54. if err != nil {
  55. os.RemoveAll(stagingDir)
  56. }
  57. }()
  58. err = f(stagingDir)
  59. if err != nil {
  60. return
  61. }
  62. finalDir = DirStagingToFinal(stagingDir)
  63. err = os.Rename(stagingDir, finalDir)
  64. if err != nil {
  65. return
  66. }
  67. return
  68. }
  69. func (m *ConfigDirManager) subdirs() []string {
  70. dirs := []string{}
  71. fis, err := ioutil.ReadDir(m.baseDir)
  72. if err != nil {
  73. return dirs
  74. }
  75. for _, fi := range fis {
  76. if !fi.IsDir() {
  77. continue
  78. }
  79. dirName := fi.Name()
  80. if !configDirPat.Match([]byte(dirName)) {
  81. continue
  82. }
  83. dir := filepath.Join(m.baseDir, dirName)
  84. dirs = append(dirs, dir)
  85. }
  86. return dirs
  87. }
  88. func (m *ConfigDirManager) MostRecentSubdir() string {
  89. dirs := m.subdirs()
  90. nDirs := len(dirs)
  91. if nDirs == 0 {
  92. return ""
  93. }
  94. return dirs[nDirs-1]
  95. }
  96. func (m *ConfigDirManager) Prune(nRetain int) error {
  97. if nRetain <= 0 {
  98. // zero means prune nothing
  99. return nil
  100. }
  101. dirs := m.subdirs()
  102. if len(dirs) > nRetain {
  103. // retain the most recent n
  104. dirs = dirs[:len(dirs)-nRetain]
  105. errs := []error{}
  106. for _, dir := range dirs {
  107. err := os.RemoveAll(dir)
  108. if os.IsNotExist(err) {
  109. continue
  110. }
  111. errs = append(errs, err)
  112. }
  113. if len(errs) > 0 {
  114. return aggrerrors.NewAggregate(errs)
  115. }
  116. }
  117. return nil
  118. }
  119. // stagingDir creates a new staging dir and returns the full path
  120. func (m *ConfigDirManager) stagingDir() string {
  121. for {
  122. now := time.Now()
  123. dn := now.Format(configDirFmtStaging)
  124. path := filepath.Join(m.baseDir, dn)
  125. _, err := os.Stat(path)
  126. if err != nil && os.IsNotExist(err) {
  127. os.MkdirAll(path, ConfigDirMode)
  128. return path
  129. }
  130. time.Sleep(time.Millisecond)
  131. }
  132. }
  133. func DirStagingToFinal(s string) string {
  134. if strings.HasSuffix(s, ".staging") {
  135. return s[:len(s)-8]
  136. }
  137. return s
  138. }