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.

82 lines
1.7 KiB

5 years ago
7 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
  1. package util
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. )
  7. type FullPath string
  8. func NewFullPath(dir, name string) FullPath {
  9. return FullPath(dir).Child(name)
  10. }
  11. func (fp FullPath) DirAndName() (string, string) {
  12. dir, name := filepath.Split(string(fp))
  13. name = strings.ToValidUTF8(name, "?")
  14. if dir == "/" {
  15. return dir, name
  16. }
  17. if len(dir) < 1 {
  18. return "/", ""
  19. }
  20. return dir[:len(dir)-1], name
  21. }
  22. func (fp FullPath) Name() string {
  23. _, name := filepath.Split(string(fp))
  24. name = strings.ToValidUTF8(name, "?")
  25. return name
  26. }
  27. func (fp FullPath) Child(name string) FullPath {
  28. dir := string(fp)
  29. noPrefix := name
  30. if strings.HasPrefix(name, "/") {
  31. noPrefix = name[1:]
  32. }
  33. if strings.HasSuffix(dir, "/") {
  34. return FullPath(dir + noPrefix)
  35. }
  36. return FullPath(dir + "/" + noPrefix)
  37. }
  38. // AsInode an in-memory only inode representation
  39. func (fp FullPath) AsInode(fileMode os.FileMode) uint64 {
  40. inode := uint64(HashStringToLong(string(fp)))
  41. inode = inode - inode%16
  42. if fileMode == 0 {
  43. } else if fileMode&os.ModeDir > 0 {
  44. inode += 1
  45. } else if fileMode&os.ModeSymlink > 0 {
  46. inode += 2
  47. } else if fileMode&os.ModeDevice > 0 {
  48. inode += 3
  49. } else if fileMode&os.ModeNamedPipe > 0 {
  50. inode += 4
  51. } else if fileMode&os.ModeSocket > 0 {
  52. inode += 5
  53. } else if fileMode&os.ModeCharDevice > 0 {
  54. inode += 6
  55. } else if fileMode&os.ModeIrregular > 0 {
  56. inode += 7
  57. }
  58. return inode
  59. }
  60. // split, but skipping the root
  61. func (fp FullPath) Split() []string {
  62. if fp == "" || fp == "/" {
  63. return []string{}
  64. }
  65. return strings.Split(string(fp)[1:], "/")
  66. }
  67. func Join(names ...string) string {
  68. return filepath.ToSlash(filepath.Join(names...))
  69. }
  70. func JoinPath(names ...string) FullPath {
  71. return FullPath(Join(names...))
  72. }