Compare commits

..

No commits in common. "89c481f95dda8109da2ef806c5ae76bbd292fde0" and "1a97d439fc8d0cdd3fb802fe02d35a8343d28851" have entirely different histories.

8 changed files with 30 additions and 58 deletions

View File

@ -14,14 +14,11 @@ import (
"github.com/nxshock/progressmessage" "github.com/nxshock/progressmessage"
) )
type Pattern struct { type Mask struct {
Path string Path string
// Маски имени файла // Маски имени файла
FileNamePatternList []string MaskList []string
// Маски пути
FilePathPatternList []string
// Вкючать файлы в покаталогах // Вкючать файлы в покаталогах
Recursive bool Recursive bool
@ -169,9 +166,9 @@ func (b *Config) doBackup(index *Index) error {
func (b *Config) fileList(fileNames chan File) { func (b *Config) fileList(fileNames chan File) {
errorCount := 0 errorCount := 0
for _, mask := range b.Patterns { for _, v := range b.Masks {
if mask.Recursive { if v.Recursive {
err := filepath.WalkDir(mask.Path, func(path string, d fs.DirEntry, err error) error { err := filepath.WalkDir(v.Path, func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
errorCount++ errorCount++
b.logf(LogLevelCritical, "Ошибка при поиске файлов: %v\n", err) b.logf(LogLevelCritical, "Ошибка при поиске файлов: %v\n", err)
@ -185,14 +182,15 @@ func (b *Config) fileList(fileNames chan File) {
return nil return nil
} }
path = filepath.ToSlash(path) if !v.Recursive && filepath.Dir(path) != v.Path {
if !mask.Recursive && filepath.Dir(path) != mask.Path {
return nil return nil
} }
if isFilePathMatchPatterns(mask.FilePathPatternList, path) && isFileNameMatchPatterns(mask.FileNamePatternList, path) { // fileName := filepath.Base(path)
if !isFilePathMatchPatterns(b.GlobalExcludeFilePathPatterns, path) && !isFileNameMatchPatterns(b.GlobalExcludeFileNamePatterns, path) { fileName := path // TODO: тестирование - маска действует на весь путь
if isFileMatchMasks(v.MaskList, fileName) {
if !isFileMatchMasks(b.GlobalExcludeMasks, fileName) {
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil { if err != nil {
errorCount++ errorCount++
@ -215,7 +213,7 @@ func (b *Config) fileList(fileNames chan File) {
b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err) b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err)
} }
} else { } else {
allFilesAndDirs, err := filepath.Glob(filepath.Join(mask.Path, "*")) allFilesAndDirs, err := filepath.Glob(filepath.Join(v.Path, "*"))
if err != nil { if err != nil {
errorCount++ errorCount++
b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err) b.logf(LogLevelCritical, "Ошибка при получении списка файлов: %v\n", err)
@ -236,8 +234,8 @@ func (b *Config) fileList(fileNames chan File) {
//fileName := filepath.Base(fileOrDirPath) //fileName := filepath.Base(fileOrDirPath)
fileName := fileOrDirPath // TODO: тестирование, маска должна накладываться на путь fileName := fileOrDirPath // TODO: тестирование, маска должна накладываться на путь
if isFilePathMatchPatterns(mask.FilePathPatternList, fileName) && isFileNameMatchPatterns(mask.FileNamePatternList, fileName) { if isFileMatchMasks(v.MaskList, fileName) {
if !isFilePathMatchPatterns(b.GlobalExcludeFilePathPatterns, fileName) && !isFileNameMatchPatterns(b.GlobalExcludeFileNamePatterns, fileName) { if !isFileMatchMasks(b.GlobalExcludeMasks, fileName) {
file := File{ file := File{
SourcePath: fileOrDirPath, SourcePath: fileOrDirPath,
DestinationPath: filepath.ToSlash(fileOrDirPath), DestinationPath: filepath.ToSlash(fileOrDirPath),
@ -256,6 +254,16 @@ func (b *Config) fileList(fileNames chan File) {
close(fileNames) 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 { func (b *Config) addFileToTarWriter(filePath string, tarWriter *tar.Writer) error {
b.logf(LogLevelDebug, "Добавление файла %s...\n", filePath) b.logf(LogLevelDebug, "Добавление файла %s...\n", filePath)

View File

@ -19,13 +19,10 @@ type Config struct {
FileName string FileName string
// Маски файлов для включения в архив // Маски файлов для включения в архив
Patterns []*Pattern Masks []Mask
// Маски файлов для исключения // Маски файлов/путей для исключения из всех масок
GlobalExcludeFileNamePatterns []string GlobalExcludeMasks []string
// Маски путей для исключения
GlobalExcludeFilePathPatterns []string
// Логгер // Логгер
Logger LoggerConfig 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} 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) configFilePath, err := filepath.Abs(filePath)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -4,7 +4,7 @@ StopOnAnyError = false
[Logger] [Logger]
MinimalLogLevel = 1 MinimalLogLevel = 1
[[Patterns]] [[Masks]]
Path = "/home/user/go/src" Path = "/home/user/go/src"
FileNamePatternList = ["*.go", "go.mod", "go.sum"] MaskList = ["*.go", "*/go.mod", "*/go.sum"]
Recursive = true Recursive = true

1
go.mod
View File

@ -7,7 +7,6 @@ require (
github.com/klauspost/compress v1.16.0 github.com/klauspost/compress v1.16.0
github.com/nxshock/progressmessage v0.0.0-20210730035634-63cec26e1e83 github.com/nxshock/progressmessage v0.0.0-20210730035634-63cec26e1e83
github.com/stretchr/testify v1.8.2 github.com/stretchr/testify v1.8.2
github.com/tidwall/match v1.1.1
) )
require ( require (

2
go.sum
View File

@ -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.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 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -69,7 +69,7 @@ func (index *Index) GetFilesLocation(mask string, t time.Time) ([]File, error) {
var files2 []File var files2 []File
for fileName := range index.Files { for fileName := range index.Files {
if isFilePathMatchPatterns([]string{mask}, fileName) { if isFileMatchMasks([]string{mask}, fileName) {
files := index.Files[fileName] files := index.Files[fileName]
file := files[0] file := files[0]

View File

@ -1 +0,0 @@
go build -ldflags "-s -w" -buildmode=pie -trimpath

View File

@ -2,10 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"github.com/tidwall/match"
) )
/*func winPathToRelative(s string) string { /*func winPathToRelative(s string) string {
@ -56,23 +53,3 @@ func stringIn(s string, ss []string) (bool, int) {
return false, -1 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
}