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.

96 lines
1.7 KiB

3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "sync"
  6. )
  7. type InodeToPath struct {
  8. sync.RWMutex
  9. nextInodeId uint64
  10. inode2path map[uint64]util.FullPath
  11. path2inode map[util.FullPath]uint64
  12. }
  13. func NewInodeToPath() *InodeToPath {
  14. return &InodeToPath{
  15. inode2path: make(map[uint64]util.FullPath),
  16. path2inode: make(map[util.FullPath]uint64),
  17. nextInodeId: 2, // the root inode id is 1
  18. }
  19. }
  20. func (i *InodeToPath) GetInode(path util.FullPath) uint64 {
  21. if path == "/" {
  22. return 1
  23. }
  24. i.Lock()
  25. defer i.Unlock()
  26. inode, found := i.path2inode[path]
  27. if !found {
  28. inode = i.nextInodeId
  29. i.nextInodeId++
  30. i.path2inode[path] = inode
  31. i.inode2path[inode] = path
  32. }
  33. return inode
  34. }
  35. func (i *InodeToPath) GetPath(inode uint64) util.FullPath {
  36. if inode == 1 {
  37. return "/"
  38. }
  39. i.RLock()
  40. defer i.RUnlock()
  41. path, found := i.inode2path[inode]
  42. if !found {
  43. glog.Fatal("not found inode %d", inode)
  44. }
  45. return path
  46. }
  47. func (i *InodeToPath) HasPath(path util.FullPath) bool {
  48. if path == "/" {
  49. return true
  50. }
  51. i.RLock()
  52. defer i.RUnlock()
  53. _, found := i.path2inode[path]
  54. return found
  55. }
  56. func (i *InodeToPath) HasInode(inode uint64) bool {
  57. if inode == 1 {
  58. return true
  59. }
  60. i.RLock()
  61. defer i.RUnlock()
  62. _, found := i.inode2path[inode]
  63. return found
  64. }
  65. func (i *InodeToPath) RemovePath(path util.FullPath) {
  66. if path == "/" {
  67. return
  68. }
  69. i.Lock()
  70. defer i.Unlock()
  71. inode, found := i.path2inode[path]
  72. if found {
  73. delete(i.path2inode, path)
  74. delete(i.inode2path, inode)
  75. }
  76. }
  77. func (i *InodeToPath) RemoveInode(inode uint64) {
  78. if inode == 1 {
  79. return
  80. }
  81. i.RLock()
  82. defer i.RUnlock()
  83. path, found := i.inode2path[inode]
  84. if found {
  85. delete(i.path2inode, path)
  86. delete(i.inode2path, inode)
  87. }
  88. }