Skip to content

Commit

Permalink
Merge pull request #50 from PlakarLabs/fix-case-folding-for-insane-fi…
Browse files Browse the repository at this point in the history
…lesystems

normalize root path when doing backup
  • Loading branch information
poolpOrg authored Nov 8, 2024
2 parents cefff6c + e24c418 commit 6c0032c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/plakar/subcommands/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"

"github.com/PlakarLabs/plakar/cmd/plakar/subcommands"
"github.com/PlakarLabs/plakar/cmd/plakar/utils"
"github.com/PlakarLabs/plakar/context"
"github.com/PlakarLabs/plakar/logger"
"github.com/PlakarLabs/plakar/repository"
Expand Down Expand Up @@ -76,6 +77,12 @@ func cmd_backup(ctx *context.Context, repo *repository.Repository, args []string
return 1
}

dir, err = utils.NormalizePath(dir)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return 1
}

if opt_excludes != "" {
fp, err := os.Open(opt_excludes)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions cmd/plakar/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,44 @@ const VERSION = "v0.4.22-alpha"
func GetVersion() string {
return VERSION
}

func NormalizePath(path string) (string, error) {
path = filepath.Clean(path)
parts := strings.Split(path, string(filepath.Separator))[1:]

if len(parts) == 0 || parts[0] == "" {
return "", fmt.Errorf("invalid path")
}

var normalizedPath string
// For Windows, start with the drive letter.
if filepath.IsAbs(path) {
normalizedPath = string(filepath.Separator)
}

for _, part := range parts {
if part == "" {
continue
}

dirEntries, err := os.ReadDir(normalizedPath)
if err != nil {
return "", err
}

matched := false
for _, entry := range dirEntries {
if strings.EqualFold(entry.Name(), part) {
normalizedPath = filepath.Join(normalizedPath, entry.Name())
matched = true
break
}
}

if !matched {
return "", fmt.Errorf("path not found: %s", path)
}
}

return normalizedPath, nil
}
2 changes: 0 additions & 2 deletions snapshot/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,12 @@ func (snap *Snapshot) Backup(scanDir string, options *PushOptions) error {
return err
}
for record := range directories {

dirEntry := vfs.NewDirectoryEntry(filepath.Dir(record.Pathname), &record)

for _, child := range record.Children {
value, err := sc.GetChecksum(filepath.Join(record.Pathname, child.Name()))
if err != nil {
continue
// return err
}
dirEntry.AddChild(value, child)
}
Expand Down

0 comments on commit 6c0032c

Please sign in to comment.