- Replaced static check for .duplicacy directory with usage of predefined filters.

Do not "misuse" property nobackupFile to trigger this feature.
- Restructured ProcessFilterFile function and splitted it in smaller parts.
- Prepare usage of new filter syntax for arguments of restore command.
This commit is contained in:
a-s-z-home
2018-11-05 00:32:12 +01:00
parent 96dd28995b
commit aaebf4510c
2 changed files with 90 additions and 51 deletions

View File

@@ -481,9 +481,6 @@ func ListEntries(top string, path string, fileList *[]*Entry, patterns []string,
entries := make([]*Entry, 0, 4)
for _, f := range files {
if (f.Name() == DUPLICACY_DIRECTORY) && (nobackupFile == "") {
continue
}
entry := CreateEntryFromFileInfo(f, normalizedPath)
if len(patterns) > 0 && !MatchPath(entry.Path, patterns) {
continue

View File

@@ -12,6 +12,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@@ -69,18 +70,7 @@ func CreateSnapshotFromDirectory(id string, top string, nobackupFile string) (sn
var patterns []string
patterns = ProcessFilterFile(path.Join(GetDuplicacyPreferencePath(), "filters"), make([]string, 0))
LOG_DEBUG("REGEX_DEBUG", "There are %d compiled regular expressions stored", len(RegexMap))
LOG_INFO("SNAPSHOT_FILTER", "Loaded %d include/exclude pattern(s)", len(patterns))
if IsTracing() {
for _, pattern := range patterns {
LOG_TRACE("SNAPSHOT_PATTERN", "Pattern: %s", pattern)
}
}
patterns = ProcessFilters()
directories := make([]*Entry, 0, 256)
directories = append(directories, CreateEntry("", 0, 0, 0))
@@ -122,6 +112,43 @@ func CreateSnapshotFromDirectory(id string, top string, nobackupFile string) (sn
return snapshot, skippedDirectories, skippedFiles, nil
}
func AppendPattern(patterns []string, new_pattern string) (new_patterns []string) {
for _, pattern := range patterns {
if pattern == new_pattern {
LOG_INFO("SNAPSHOT_FILTER", "Ignoring duplicate pattern: %s ...", new_pattern)
return patterns
}
}
new_patterns = append(patterns, new_pattern)
return new_patterns
}
func ProcessFilters() (patterns []string) {
patternFileLines := []string{
`# ============================ exclude the internal files and directories in all ".duplicacy" subfolders`,
`e:(?i)(^|/)` + regexp.QuoteMeta(DUPLICACY_DIRECTORY) + `/cache/`,
`e:(?i)(^|/)` + regexp.QuoteMeta(DUPLICACY_DIRECTORY) + `/temporary$`,
`# ============================ exclude the internal files and directories in toplevel ".duplicacy" subfolder`,
`e:(?i)^` + regexp.QuoteMeta(DUPLICACY_DIRECTORY) + `/incomplete$`,
`e:(?i)^` + regexp.QuoteMeta(DUPLICACY_DIRECTORY) + `/logs/`,
"@" + joinPath(GetDuplicacyPreferencePath(), "filters"),
}
LOG_DEBUG("SNAPSHOT_FILTER", "Adding standard filters ...")
patterns = ProcessFilterLines(patternFileLines, make([]string, 0))
LOG_DEBUG("REGEX_DEBUG", "There are %d compiled regular expressions stored", len(RegexMap))
LOG_INFO("SNAPSHOT_FILTER", "Loaded %d include/exclude pattern(s)", len(patterns))
if IsTracing() {
for _, pattern := range patterns {
LOG_TRACE("SNAPSHOT_PATTERN", "Pattern: %s", pattern)
}
}
return patterns
}
func ProcessFilterFile(patternFile string, includedFiles []string) (patterns []string) {
if Contains(includedFiles, patternFile) {
// cycle in include mechanism discovered.
@@ -132,7 +159,14 @@ func ProcessFilterFile(patternFile string, includedFiles []string) (patterns []s
LOG_INFO("SNAPSHOT_FILTER", "Parsing filter file %s ...", patternFile)
patternFileContent, err := ioutil.ReadFile(patternFile)
if err == nil {
for _, pattern := range strings.Split(string(patternFileContent), "\n") {
patternFileLines := strings.Split(string(patternFileContent), "\n")
patterns = ProcessFilterLines(patternFileLines, includedFiles)
}
return patterns
}
func ProcessFilterLines(patternFileLines []string, includedFiles []string) (patterns []string) {
for _, pattern := range patternFileLines {
pattern = strings.TrimSpace(pattern)
if len(pattern) == 0 {
continue
@@ -140,11 +174,20 @@ func ProcessFilterFile(patternFile string, includedFiles []string) (patterns []s
if strings.HasPrefix(pattern, "@") {
patternIncludeFile := strings.TrimSpace(pattern[1:])
if patternIncludeFile == "" {
continue
}
if ! filepath.IsAbs(patternIncludeFile) {
patternIncludeFile = joinPath(filepath.Dir(patternFile), patternIncludeFile)
basePath := ""
if len(includedFiles) == 0 {
basePath, _ = os.Getwd()
} else {
basePath = filepath.Dir(includedFiles[len(includedFiles)-1])
}
patternIncludeFile = joinPath(basePath, patternIncludeFile)
}
for _, pattern := range ProcessFilterFile(patternIncludeFile, includedFiles) {
patterns = append(patterns, pattern)
patterns = AppendPattern(patterns, pattern)
}
continue
}
@@ -168,8 +211,7 @@ func ProcessFilterFile(patternFile string, includedFiles []string) (patterns []s
}
}
patterns = append(patterns, pattern)
}
patterns = AppendPattern(patterns, pattern)
}
return patterns