unix_path_wrap.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package mountree
  2. import (
  3. libpath "path"
  4. "strings"
  5. )
  6. type SimpleUnixLikePathTree[T PayloadType] struct {
  7. baseTree *Tree[T]
  8. }
  9. func NewSimpleUnixLikePathTree[T PayloadType]() *SimpleUnixLikePathTree[T] {
  10. return &SimpleUnixLikePathTree[T]{
  11. baseTree: NewTree[T](),
  12. }
  13. }
  14. func (t *SimpleUnixLikePathTree[T]) splitPath(path string) []string {
  15. if path == "" {
  16. return []string{}
  17. }
  18. sp := strings.Split(libpath.Clean(path), "/")
  19. filtered := make([]string, 0, len(sp))
  20. for _, str := range sp {
  21. if str != "" {
  22. filtered = append(filtered, str)
  23. }
  24. }
  25. return filtered
  26. }
  27. func (t *SimpleUnixLikePathTree[T]) Mount(path string, payload T, forceRemount bool) error {
  28. pseq := t.splitPath(path)
  29. return t.baseTree.Mount(pseq, payload, forceRemount)
  30. }
  31. func (t *SimpleUnixLikePathTree[T]) Umount(path string) error {
  32. pseq := t.splitPath(path)
  33. return t.baseTree.Umount(pseq)
  34. }
  35. func (t *SimpleUnixLikePathTree[T]) GetPayload(path string) (*T, string, error) {
  36. pseq := t.splitPath(path)
  37. payload, rpseq, err := t.baseTree.GetPayload(pseq)
  38. if err != nil {
  39. return nil, path, err
  40. }
  41. rp := strings.Join(rpseq, "/")
  42. return payload, rp, nil
  43. }
  44. func (t *SimpleUnixLikePathTree[T]) ListAllMount(prt func(path string, payload T)) {
  45. t.baseTree.ListAllMount(prt, "/")
  46. }