fullpath.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if dir == "/" {
  13. return dir, name
  14. }
  15. if len(dir) < 1 {
  16. return "/", ""
  17. }
  18. return dir[:len(dir)-1], name
  19. }
  20. func (fp FullPath) Name() string {
  21. _, name := filepath.Split(string(fp))
  22. return name
  23. }
  24. func (fp FullPath) Child(name string) FullPath {
  25. dir := string(fp)
  26. if strings.HasSuffix(dir, "/") {
  27. return FullPath(dir + name)
  28. }
  29. return FullPath(dir + "/" + name)
  30. }
  31. func (fp FullPath) AsInode() uint64 {
  32. return uint64(HashStringToLong(string(fp)))
  33. }
  34. // split, but skipping the root
  35. func (fp FullPath) Split() []string {
  36. if fp == "" || fp == "/" {
  37. return []string{}
  38. }
  39. return strings.Split(string(fp)[1:], "/")
  40. }
  41. func Join(names ...string) string {
  42. return filepath.ToSlash(filepath.Join(names...))
  43. }
  44. func JoinPath(names ...string) FullPath {
  45. return FullPath(Join(names...))
  46. }