fullpath.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package util
  2. import (
  3. "path/filepath"
  4. "strings"
  5. )
  6. type FullPath string
  7. func NewFullPath(dir, name string) FullPath {
  8. return FullPath(dir).Child(name)
  9. }
  10. func (fp FullPath) DirAndName() (string, string) {
  11. dir, name := filepath.Split(string(fp))
  12. name = strings.ToValidUTF8(name, "?")
  13. if dir == "/" {
  14. return dir, name
  15. }
  16. if len(dir) < 1 {
  17. return "/", ""
  18. }
  19. return dir[:len(dir)-1], name
  20. }
  21. func (fp FullPath) Name() string {
  22. _, name := filepath.Split(string(fp))
  23. name = strings.ToValidUTF8(name, "?")
  24. return name
  25. }
  26. func (fp FullPath) Child(name string) FullPath {
  27. dir := string(fp)
  28. if strings.HasSuffix(dir, "/") {
  29. return FullPath(dir + name)
  30. }
  31. return FullPath(dir + "/" + name)
  32. }
  33. func (fp FullPath) AsInode() uint64 {
  34. return uint64(HashStringToLong(string(fp)))
  35. }
  36. // split, but skipping the root
  37. func (fp FullPath) Split() []string {
  38. if fp == "" || fp == "/" {
  39. return []string{}
  40. }
  41. return strings.Split(string(fp)[1:], "/")
  42. }
  43. func Join(names ...string) string {
  44. return filepath.ToSlash(filepath.Join(names...))
  45. }
  46. func JoinPath(names ...string) FullPath {
  47. return FullPath(Join(names...))
  48. }