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

Commit

Permalink
Introduce markerFile in created directories to not remove unmanaged
Browse files Browse the repository at this point in the history
directories
  • Loading branch information
wilriker committed Jun 5, 2019
1 parent 777a363 commit 3da9684
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions duetbackup.go
Expand Up @@ -22,6 +22,7 @@ const (
typeFile = "f"
fileDownloadURL = "rr_download?name="
fileListURL = "rr_filelist?dir="
dirMarker = ".duetbackup"
)

var multiSlashRegex = regexp.MustCompile(`/{2,}`)
Expand Down Expand Up @@ -145,6 +146,11 @@ func ensureOutDirExists(outDir string, verbose bool) error {
return err
}
}
markerFile, err := os.Create(filepath.Join(path, dirMarker))
if err != nil {
return err
}
markerFile.Close()
return nil
}

Expand Down Expand Up @@ -216,6 +222,21 @@ func updateLocalFiles(baseURL string, fl *filelist, outDir string, excls exclude
return nil
}

func isManagedDirectory(basePath string, f os.FileInfo) bool {
if !f.IsDir() {
return false
}
markerFile := filepath.Join(basePath, f.Name(), dirMarker)
fi, err := os.Stat(markerFile)
if err != nil && !os.IsNotExist(err) {
return false
}
if fi == nil {
return false
}
return true
}

func removeDeletedFiles(fl *filelist, outDir string, verbose bool) error {

existingFiles := make(map[string]struct{})
Expand All @@ -230,6 +251,11 @@ func removeDeletedFiles(fl *filelist, outDir string, verbose bool) error {

for _, f := range files {
if _, exists := existingFiles[f.Name()]; !exists {

// Skip directories not managed by us as well as our marker file
if !isManagedDirectory(outDir, f) || f.Name() == dirMarker {
continue
}
if err := os.RemoveAll(filepath.Join(outDir, f.Name())); err != nil {
return err
}
Expand Down

0 comments on commit 3da9684

Please sign in to comment.