Keepers 7419faab23
refactor drive restore & export to use sanitree (#4425)
refactors the common drive sanity checks to use the sanitree data container.  Also expands the sanitree in two ways:
1. adds leaves (individual items) to nodes for granular data comparison
2. adds multi-type support for comparing nodes of different types.

---

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🤖 Supportability/Tests

#### Issue(s)

* #3988

#### Test Plan

- [x] 💪 Manual
- [x] 💚 E2E
2023-10-13 22:55:35 +00:00

80 lines
1.6 KiB
Go

package common
import (
"context"
"io/fs"
"os"
"path/filepath"
"github.com/alcionai/corso/src/pkg/path"
)
func BuildFilepathSanitree(
ctx context.Context,
rootDir string,
) *Sanitree[fs.FileInfo, fs.FileInfo] {
var root *Sanitree[fs.FileInfo, fs.FileInfo]
walker := func(
p string,
info os.FileInfo,
err error,
) error {
if err != nil {
Fatal(ctx, "error passed to filepath walker", err)
}
relPath, err := filepath.Rel(rootDir, p)
if err != nil {
Fatal(ctx, "getting relative filepath", err)
}
if info != nil {
Debugf(ctx, "adding: %s", relPath)
}
if root == nil {
root = &Sanitree[fs.FileInfo, fs.FileInfo]{
Self: info,
ID: info.Name(),
Name: info.Name(),
Leaves: map[string]*Sanileaf[fs.FileInfo, fs.FileInfo]{},
Children: map[string]*Sanitree[fs.FileInfo, fs.FileInfo]{},
}
return nil
}
elems := path.Split(relPath)
node := root.NodeAt(ctx, elems[:len(elems)-1])
if info.IsDir() {
node.Children[info.Name()] = &Sanitree[fs.FileInfo, fs.FileInfo]{
Parent: node,
Self: info,
ID: info.Name(),
Name: info.Name(),
Leaves: map[string]*Sanileaf[fs.FileInfo, fs.FileInfo]{},
Children: map[string]*Sanitree[fs.FileInfo, fs.FileInfo]{},
}
} else {
node.Leaves[info.Name()] = &Sanileaf[fs.FileInfo, fs.FileInfo]{
Parent: node,
Self: info,
ID: info.Name(),
Name: info.Name(),
Size: info.Size(),
}
}
return nil
}
err := filepath.Walk(rootDir, walker)
if err != nil {
Fatal(ctx, "walking filepath", err)
}
return root
}