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.

86 lines
1.8 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. if fileMode&os.ModeCharDevice > 0 {
  49. inode += 6
  50. } else {
  51. inode += 3
  52. }
  53. } else if fileMode&os.ModeNamedPipe > 0 {
  54. inode += 4
  55. } else if fileMode&os.ModeSocket > 0 {
  56. inode += 5
  57. } else if fileMode&os.ModeCharDevice > 0 {
  58. inode += 6
  59. } else if fileMode&os.ModeIrregular > 0 {
  60. inode += 7
  61. }
  62. return inode
  63. }
  64. // split, but skipping the root
  65. func (fp FullPath) Split() []string {
  66. if fp == "" || fp == "/" {
  67. return []string{}
  68. }
  69. return strings.Split(string(fp)[1:], "/")
  70. }
  71. func Join(names ...string) string {
  72. return filepath.ToSlash(filepath.Join(names...))
  73. }
  74. func JoinPath(names ...string) FullPath {
  75. return FullPath(Join(names...))
  76. }