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.

58 lines
1.1 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. 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. }