Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Commit

Permalink
Fix issue with trailing (or possibly multiple) slashes for excludes
Browse files Browse the repository at this point in the history
* Also check if a folder can be excluded completely instead of excluding
each file inside it.
* Remove directories to be removed recursively
  • Loading branch information
wilriker committed Jun 5, 2019
1 parent f1a80d8 commit d30e2cc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions duetbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -21,6 +22,7 @@ const (
File = "f"
)

var multiSlashRegex = regexp.MustCompile(`/{2,}`)
var httpClient *http.Client

type localTime struct {
Expand Down Expand Up @@ -55,7 +57,7 @@ func (e *excludes) String() string {
}

func (e *excludes) Set(value string) error {
e.excls = append(e.excls, value)
e.excls = append(e.excls, cleanPath(value))
return nil
}

Expand All @@ -68,6 +70,12 @@ func (e *excludes) Contains(path string) bool {
return false
}

func cleanPath(path string) string {
cleanedPath := multiSlashRegex.ReplaceAllString(path, "/")
cleanedPath = strings.TrimSuffix(cleanedPath, "/")
return cleanedPath
}

func getFileList(baseURL string, dir string, first uint64) (*filelist, error) {

fileListURL := "rr_filelist?dir="
Expand Down Expand Up @@ -217,7 +225,7 @@ func removeDeletedFiles(fl *filelist, outDir string, verbose bool) error {

for _, f := range files {
if _, exists := existingFiles[f.Name()]; !exists {
if err := os.Remove(filepath.Join(outDir, f.Name())); err != nil {
if err := os.RemoveAll(filepath.Join(outDir, f.Name())); err != nil {
return err
}
if verbose {
Expand All @@ -230,6 +238,11 @@ func removeDeletedFiles(fl *filelist, outDir string, verbose bool) error {
}

func syncFolder(address, folder, outDir string, excls excludes, removeLocal, verbose bool) error {
if excls.Contains(folder) {
log.Println("Excluding", folder)
return nil
}

log.Println("Fetching filelist for", folder)
fl, err := getFileList(address, url.QueryEscape(folder), 0)
if err != nil {
Expand Down Expand Up @@ -319,7 +332,7 @@ func main() {
absPath = outDir
}

err = syncFolder(address, dirToBackup, absPath, excls, removeLocal, verbose)
err = syncFolder(address, cleanPath(dirToBackup), absPath, excls, removeLocal, verbose)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit d30e2cc

Please sign in to comment.