diff --git a/backuper.go b/backuper.go index a83f8e8..47560a3 100644 --- a/backuper.go +++ b/backuper.go @@ -14,14 +14,11 @@ import ( "github.com/nxshock/progressmessage" ) -type Pattern struct { +type Mask struct { Path string // Маски имени файла - FileNamePatternList []string - - // Маски пути - FilePathPatternList []string + MaskList []string // Вкючать файлы в покаталогах Recursive bool @@ -169,9 +166,9 @@ func (b *Config) doBackup(index *Index) error { func (b *Config) fileList(fileNames chan File) { errorCount := 0 - for _, mask := range b.Patterns { - if mask.Recursive { - err := filepath.WalkDir(mask.Path, func(path string, d fs.DirEntry, err error) error { + for _, v := range b.Masks { + if v.Recursive { + err := filepath.WalkDir(v.Path, func(path string, d fs.DirEntry, err error) error { if err != nil { errorCount++ b.logf(LogLevelCritical, "Ошибка при поиске файлов: %v\n", err) @@ -185,14 +182,15 @@ func (b *Config) fileList(fileNames chan File) { return nil } - path = filepath.ToSlash(path) - - if !mask.Recursive && filepath.Dir(path) != mask.Path { + if !v.Recursive && filepath.Dir(path) != v.Path { return nil } - if isFilePathMatchPatterns(mask.FilePathPatternList, path) && isFileNameMatchPatterns(mask.FileNamePatternList, path) { - if !isFilePathMatchPatterns(b.GlobalExcludeFilePathPatterns, path) && !isFileNameMatchPatterns(b.GlobalExcludeFileNamePatterns, path) { + // fileName := filepath.Base(path) + fileName := path // TODO: тестирование - маска действует на весь путь + + if isFileMatchMasks(v.MaskList, fileName) { + if !isFileMatchMasks(b.GlobalExcludeMasks, fileName) { info, err := os.Stat(path) if err != nil { errorCount++ @@ -215,7 +213,7 @@ func (b *Config) fileList(fileNames chan File) { b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err) } } else { - allFilesAndDirs, err := filepath.Glob(filepath.Join(mask.Path, "*")) + allFilesAndDirs, err := filepath.Glob(filepath.Join(v.Path, "*")) if err != nil { errorCount++ b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err) @@ -236,8 +234,8 @@ func (b *Config) fileList(fileNames chan File) { //fileName := filepath.Base(fileOrDirPath) fileName := fileOrDirPath // TODO: тестирование, маска должна накладываться на путь - if isFilePathMatchPatterns(mask.FilePathPatternList, fileName) && isFileNameMatchPatterns(mask.FileNamePatternList, fileName) { - if !isFilePathMatchPatterns(b.GlobalExcludeFilePathPatterns, fileName) && !isFileNameMatchPatterns(b.GlobalExcludeFileNamePatterns, fileName) { + if isFileMatchMasks(v.MaskList, fileName) { + if !isFileMatchMasks(b.GlobalExcludeMasks, fileName) { file := File{ SourcePath: fileOrDirPath, DestinationPath: filepath.ToSlash(fileOrDirPath), @@ -256,6 +254,16 @@ func (b *Config) fileList(fileNames chan File) { close(fileNames) } +func isFileMatchMasks(masks []string, fileName string) bool { + for _, mask := range masks { + if match, _ := filepath.Match(filepath.ToSlash(mask), filepath.ToSlash(fileName)); match { + return true + } + } + + return false +} + func (b *Config) addFileToTarWriter(filePath string, tarWriter *tar.Writer) error { b.logf(LogLevelDebug, "Добавление файла %s...\n", filePath) diff --git a/config.go b/config.go index e14cfef..1669630 100644 --- a/config.go +++ b/config.go @@ -19,13 +19,10 @@ type Config struct { FileName string // Маски файлов для включения в архив - Patterns []*Pattern + Masks []Mask - // Маски файлов для исключения - GlobalExcludeFileNamePatterns []string - - // Маски путей для исключения - GlobalExcludeFilePathPatterns []string + // Маски файлов/путей для исключения из всех масок + GlobalExcludeMasks []string // Логгер Logger LoggerConfig @@ -73,12 +70,6 @@ func LoadConfig(filePath string) (*Config, error) { config.logger = Logger{logger: log.New(os.Stderr, "", 0), MinimalLogLevel: config.Logger.MinimalLogLevel} - for _, mask := range config.Patterns { - if len(mask.FilePathPatternList) == 0 { - mask.FilePathPatternList = []string{"*"} - } - } - configFilePath, err := filepath.Abs(filePath) if err != nil { return nil, err diff --git a/examples/config.toml b/examples/config.toml index c2e158c..bd09ddb 100644 --- a/examples/config.toml +++ b/examples/config.toml @@ -4,7 +4,7 @@ StopOnAnyError = false [Logger] MinimalLogLevel = 1 -[[Patterns]] +[[Masks]] Path = "/home/user/go/src" -FileNamePatternList = ["*.go", "go.mod", "go.sum"] +MaskList = ["*.go", "*/go.mod", "*/go.sum"] Recursive = true diff --git a/go.mod b/go.mod index 9327a7f..a12ec01 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/klauspost/compress v1.16.0 github.com/nxshock/progressmessage v0.0.0-20210730035634-63cec26e1e83 github.com/stretchr/testify v1.8.2 - github.com/tidwall/match v1.1.1 ) require ( diff --git a/go.sum b/go.sum index 9c4658c..1881fba 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= -github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/index.go b/index.go index dc48b6d..54f5d9e 100644 --- a/index.go +++ b/index.go @@ -69,7 +69,7 @@ func (index *Index) GetFilesLocation(mask string, t time.Time) ([]File, error) { var files2 []File for fileName := range index.Files { - if isFilePathMatchPatterns([]string{mask}, fileName) { + if isFileMatchMasks([]string{mask}, fileName) { files := index.Files[fileName] file := files[0] diff --git a/make.bat b/make.bat deleted file mode 100644 index 204b24b..0000000 --- a/make.bat +++ /dev/null @@ -1 +0,0 @@ -go build -ldflags "-s -w" -buildmode=pie -trimpath diff --git a/utils.go b/utils.go index 66b403d..6ccbc67 100644 --- a/utils.go +++ b/utils.go @@ -2,10 +2,7 @@ package main import ( "fmt" - "path/filepath" "strings" - - "github.com/tidwall/match" ) /*func winPathToRelative(s string) string { @@ -56,23 +53,3 @@ func stringIn(s string, ss []string) (bool, int) { return false, -1 } - -func isFileNameMatchPatterns(patterns []string, fileName string) bool { - for _, mask := range patterns { - if match.Match(filepath.Base(fileName), mask) { - return true - } - } - - return false -} - -func isFilePathMatchPatterns(patterns []string, fileName string) bool { - for _, mask := range patterns { - if match.Match(fileName, mask) { - return true - } - } - - return false -}