<!-- PR description--> --- #### Does this PR need a docs update or release note? - [ ] ✅ Yes, it's included - [ ] 🕐 Yes, but in a later PR - [x] ⛔ No #### Type of change <!--- Please check the type of change your PR introduces: ---> - [ ] 🌻 Feature - [ ] 🐛 Bugfix - [ ] 🗺️ Documentation - [x] 🤖 Supportability/Tests - [ ] 💻 CI/Deployment - [ ] 🧹 Tech Debt/Cleanup #### Issue(s) <!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. --> * closes https://github.com/alcionai/corso/issues/4652 #### Test Plan <!-- How will this be tested prior to merging.--> - [ ] 💪 Manual - [ ] ⚡ Unit test - [ ] 💚 E2E
81 lines
1.6 KiB
Go
81 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.CountLeaves++
|
|
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
|
|
}
|