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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. perm 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. perm: info.Perm,
  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. perm: 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) Perm() os.FileMode {
  46. return n.perm
  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. ch := n.loadCacheFile()
  60. if ch == nil {
  61. return fuse.ErrNotExists
  62. }
  63. defer ch.Release()
  64. if !ch.LevelUp(cache.LevelComplete) {
  65. return fuse.ErrIOError
  66. }
  67. return ch.Truncate(int64(size))
  68. }
  69. func (n *FuseFileNode) SetModTime(time time.Time) error {
  70. ch := n.loadCacheFile()
  71. if ch == nil {
  72. return fuse.ErrNotExists
  73. }
  74. defer ch.Release()
  75. if !ch.LevelUp(cache.LevelMetaLoaded) {
  76. return fuse.ErrIOError
  77. }
  78. return ch.SetModTime(time)
  79. }
  80. func (n *FuseFileNode) Open(flags uint32) (fuse.FileHandle, uint32, error) {
  81. ch := n.loadCacheFile()
  82. if ch == nil {
  83. // 如果文件不存在,也不进行创建,因为创建不应该调用这个接口
  84. return nil, 0, fuse.ErrNotExists
  85. }
  86. defer ch.Release()
  87. if !ch.LevelUp(cache.LevelComplete) {
  88. return nil, 0, fuse.ErrIOError
  89. }
  90. hd := ch.Open(flags)
  91. return newFileHandle(n, hd), flags, nil
  92. }
  93. func (n *FuseFileNode) loadCacheFile() *cache.CacheFile {
  94. if len(n.pathComps) <= 2 {
  95. return n.vfs.cache.LoadFile(n.pathComps, nil)
  96. }
  97. cdsObj, err := n.vfs.db.Object().GetByFullPath(n.vfs.db.DefCtx(), n.pathComps[0], n.pathComps[1], clitypes.JoinObjectPath(n.pathComps[2:]...))
  98. if err == nil {
  99. file := n.vfs.cache.LoadFile(n.pathComps, &cdsObj)
  100. if file == nil {
  101. return nil
  102. }
  103. return file
  104. }
  105. if err == gorm.ErrRecordNotFound {
  106. return n.vfs.cache.LoadFile(n.pathComps, nil)
  107. }
  108. return nil
  109. }
  110. type FuseFileHandle struct {
  111. entry *FuseFileNode
  112. chd *cache.CacheFileHandle
  113. }
  114. func newFileHandle(entry *FuseFileNode, chd *cache.CacheFileHandle) *FuseFileHandle {
  115. return &FuseFileHandle{
  116. entry: entry,
  117. chd: chd,
  118. }
  119. }
  120. func (hd *FuseFileHandle) Entry() fuse.FsFile {
  121. return hd.entry
  122. }
  123. func (hd *FuseFileHandle) ReadAt(buf []byte, off int64) (int, error) {
  124. return hd.chd.ReadAt(buf, off)
  125. }
  126. func (hd *FuseFileHandle) WriteAt(buf []byte, off int64) (int, error) {
  127. return hd.chd.WriteAt(buf, off)
  128. }
  129. func (hd *FuseFileHandle) Sync() error {
  130. return hd.chd.Sync()
  131. }
  132. func (hd *FuseFileHandle) Flush() error {
  133. return nil
  134. }
  135. func (hd *FuseFileHandle) Close() error {
  136. return hd.chd.Close()
  137. }

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