file_container.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package gdrivefs
  2. import (
  3. "sync"
  4. "github.com/jacobsa/fuse/fuseops"
  5. )
  6. type FileContainer struct {
  7. fileEntries map[fuseops.InodeID]*FileEntry
  8. tree *FileEntry
  9. fs *GDriveFS
  10. uid uint32
  11. gid uint32
  12. inodeMU *sync.Mutex
  13. }
  14. func NewFileContainer(fs *GDriveFS) *FileContainer {
  15. fc := &FileContainer{
  16. fileEntries: map[fuseops.InodeID]*FileEntry{},
  17. fs: fs,
  18. inodeMU: &sync.Mutex{},
  19. }
  20. fc.tree = fc.FileEntry(fuseops.RootInodeID)
  21. return fc
  22. }
  23. func (fc *FileContainer) FindByInode(inode fuseops.InodeID) *FileEntry {
  24. return fc.fileEntries[inode]
  25. }
  26. //Return or create inode
  27. func (fc *FileContainer) FileEntry(inodeOps ...fuseops.InodeID) *FileEntry {
  28. fc.inodeMU.Lock()
  29. defer fc.inodeMU.Unlock()
  30. var inode fuseops.InodeID
  31. if len(inodeOps) > 0 {
  32. inode = inodeOps[0]
  33. if fe, ok := fc.fileEntries[inode]; ok {
  34. return fe
  35. }
  36. } else { // generate new inode
  37. // Max Inode Number
  38. for inode = 2; inode < 99999; inode++ {
  39. _, ok := fc.fileEntries[inode]
  40. if !ok {
  41. break
  42. }
  43. }
  44. }
  45. fe := &FileEntry{
  46. Inode: inode,
  47. container: fc,
  48. //fs: fc.fs,
  49. children: []*FileEntry{},
  50. Attr: fuseops.InodeAttributes{},
  51. }
  52. fc.fileEntries[inode] = fe
  53. return fe
  54. }
  55. // RemoveEntry remove file entry
  56. func (fc *FileContainer) RemoveEntry(entry *FileEntry) {
  57. var inode fuseops.InodeID
  58. for k, e := range fc.fileEntries {
  59. if e == entry {
  60. inode = k
  61. }
  62. }
  63. delete(fc.fileEntries, inode)
  64. }