You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2 KiB

5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. noPrefix := name
  29. if strings.HasPrefix(name, "/") {
  30. noPrefix = name[1:]
  31. }
  32. if strings.HasSuffix(dir, "/") {
  33. return FullPath(dir + noPrefix)
  34. }
  35. return FullPath(dir + "/" + noPrefix)
  36. }
  37. func (fp FullPath) AsInode() uint64 {
  38. return uint64(HashStringToLong(string(fp)))
  39. }
  40. // split, but skipping the root
  41. func (fp FullPath) Split() []string {
  42. if fp == "" || fp == "/" {
  43. return []string{}
  44. }
  45. return strings.Split(string(fp)[1:], "/")
  46. }
  47. func Join(names ...string) string {
  48. return filepath.ToSlash(filepath.Join(names...))
  49. }
  50. func JoinPath(names ...string) FullPath {
  51. return FullPath(Join(names...))
  52. }