기능개선
This commit is contained in:
@@ -2,23 +2,36 @@ package path
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lhk.o-r.kr/freerer2/simple_backup/internal/constants"
|
||||
)
|
||||
|
||||
// GetGroupByFolder: group-by 옵션에 따라 백업 경로 생성
|
||||
// GetGroupByFolder generates a backup path based on the groupBy option and time.
|
||||
// If groupBy is invalid or empty, it returns the basePath unchanged.
|
||||
//
|
||||
// Parameters:
|
||||
// - basePath: the base directory path for backup
|
||||
// - groupBy: grouping option (year, mon, day, hour, min, sec)
|
||||
// - t: time to use for path generation
|
||||
//
|
||||
// Returns:
|
||||
// - string: generated path based on groupBy option
|
||||
func GetGroupByFolder(basePath, groupBy string, t time.Time) string {
|
||||
switch groupBy {
|
||||
case "year":
|
||||
case constants.GroupByYear:
|
||||
return fmt.Sprintf("%s/%d", basePath, t.Year())
|
||||
case "mon":
|
||||
case constants.GroupByMonth:
|
||||
return fmt.Sprintf("%s/%d%02d", basePath, t.Year(), t.Month())
|
||||
case "day":
|
||||
case constants.GroupByDay:
|
||||
return fmt.Sprintf("%s/%d%02d%02d", basePath, t.Year(), t.Month(), t.Day())
|
||||
case "hour":
|
||||
case constants.GroupByHour:
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour())
|
||||
case "min":
|
||||
case constants.GroupByMin:
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute())
|
||||
case "sec":
|
||||
case constants.GroupBySec:
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d%02d%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
||||
case "":
|
||||
return basePath
|
||||
@@ -27,3 +40,39 @@ func GetGroupByFolder(basePath, groupBy string, t time.Time) string {
|
||||
return basePath
|
||||
}
|
||||
}
|
||||
|
||||
// 디렉토리 캐시
|
||||
type DirCache struct {
|
||||
sync.RWMutex
|
||||
exists map[string]bool
|
||||
}
|
||||
|
||||
func NewDirCache() *DirCache {
|
||||
return &DirCache{
|
||||
exists: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *DirCache) EnsureDir(path string) error {
|
||||
c.RLock()
|
||||
if c.exists[path] {
|
||||
c.RUnlock()
|
||||
return nil
|
||||
}
|
||||
c.RUnlock()
|
||||
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
// 다시 확인 (다른 고루틴이 생성했을 수 있음)
|
||||
if c.exists[path] {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path, constants.DefaultFileMode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.exists[path] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user