You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fuse_file.go 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package vfs
  2. import (
  3. "os"
  4. "time"
  5. "gitlink.org.cn/cloudream/jcs-pub/client/internal/mount/fuse"
  6. "gitlink.org.cn/cloudream/jcs-pub/client/internal/mount/vfs/cache"
  7. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  8. "gorm.io/gorm"
  9. )
  10. type FuseFileNode struct {
  11. vfs *Vfs
  12. pathComps []string
  13. size int64
  14. modTime time.Time
  15. mode os.FileMode
  16. }
  17. func newFileFromCache(info cache.CacheEntryInfo, vfs *Vfs) *FuseFileNode {
  18. return &FuseFileNode{
  19. vfs: vfs,
  20. pathComps: info.PathComps,
  21. size: info.Size,
  22. modTime: info.ModTime,
  23. mode: info.Mode,
  24. }
  25. }
  26. func newFileFromObject(vfs *Vfs, pathComps []string, obj clitypes.Object) *FuseFileNode {
  27. return &FuseFileNode{
  28. vfs: vfs,
  29. pathComps: pathComps,
  30. size: obj.Size,
  31. modTime: obj.UpdateTime,
  32. mode: os.FileMode(0755), // TODO Object元数据中是没有保存权限的
  33. }
  34. }
  35. func (n *FuseFileNode) Name() string {
  36. return n.pathComps[len(n.pathComps)-1]
  37. }
  38. func (n *FuseFileNode) Size() int64 {
  39. info := n.vfs.cache.Stat(n.pathComps)
  40. if info == nil {
  41. return n.size
  42. }
  43. return info.Size
  44. }
  45. func (n *FuseFileNode) Mode() os.FileMode {
  46. return n.mode
  47. }
  48. func (n *FuseFileNode) ModTime() time.Time {
  49. info := n.vfs.cache.Stat(n.pathComps)
  50. if info == nil {
  51. return n.modTime
  52. }
  53. return info.ModTime
  54. }
  55. func (n *FuseFileNode) IsDir() bool {
  56. return false
  57. }
  58. func (n *FuseFileNode) Truncate(size uint64) error {
  59. cacheFile := n.loadCacheFile()
  60. if cacheFile == nil {
  61. return fuse.ErrNotExists
  62. }
  63. defer cacheFile.Release()
  64. return cacheFile.Truncate(int64(size))
  65. }
  66. func (n *FuseFileNode) SetModTime(time time.Time) error {
  67. cacheFile := n.loadCacheFile()
  68. if cacheFile == nil {
  69. return fuse.ErrNotExists
  70. }
  71. defer cacheFile.Release()
  72. return cacheFile.SetModTime(time)
  73. }
  74. func (n *FuseFileNode) Open(flags uint32) (fuse.FileHandle, uint32, error) {
  75. cacheFile := n.loadCacheFile()
  76. if cacheFile == nil {
  77. // 如果文件不存在,也不进行创建,因为创建不应该调用这个接口
  78. return nil, 0, fuse.ErrNotExists
  79. }
  80. defer cacheFile.Release()
  81. hd := cacheFile.Open(flags)
  82. return newFileHandle(n, hd), flags, nil
  83. }
  84. func (n *FuseFileNode) loadCacheFile() *cache.CacheFile {
  85. if len(n.pathComps) <= 2 {
  86. return n.vfs.cache.LoadFile(n.pathComps, nil)
  87. }
  88. cdsObj, err := n.vfs.db.Object().GetByFullPath(n.vfs.db.DefCtx(), n.pathComps[0], n.pathComps[1], clitypes.JoinObjectPath(n.pathComps[2:]...))
  89. if err == nil {
  90. file := n.vfs.cache.LoadFile(n.pathComps, &cdsObj)
  91. if file == nil {
  92. return nil
  93. }
  94. return file
  95. }
  96. if err == gorm.ErrRecordNotFound {
  97. return n.vfs.cache.LoadFile(n.pathComps, nil)
  98. }
  99. return nil
  100. }
  101. type FuseFileHandle struct {
  102. entry *FuseFileNode
  103. chd *cache.CacheFileHandle
  104. }
  105. func newFileHandle(entry *FuseFileNode, chd *cache.CacheFileHandle) *FuseFileHandle {
  106. return &FuseFileHandle{
  107. entry: entry,
  108. chd: chd,
  109. }
  110. }
  111. func (hd *FuseFileHandle) Entry() fuse.FsFile {
  112. return hd.entry
  113. }
  114. func (hd *FuseFileHandle) ReadAt(buf []byte, off int64) (int, error) {
  115. return hd.chd.ReadAt(buf, off)
  116. }
  117. func (hd *FuseFileHandle) WriteAt(buf []byte, off int64) (int, error) {
  118. return hd.chd.WriteAt(buf, off)
  119. }
  120. func (hd *FuseFileHandle) Sync() error {
  121. return hd.chd.Sync()
  122. }
  123. func (hd *FuseFileHandle) Close() error {
  124. return hd.chd.Close()
  125. }
  126. func (hd *FuseFileHandle) Release() error {
  127. // TODO 其他清理工作
  128. return nil
  129. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。