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

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

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