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.go 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package vfs
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/utils/lo2"
  7. "gitlink.org.cn/cloudream/storage2/client/internal/db"
  8. "gitlink.org.cn/cloudream/storage2/client/internal/mount/fuse"
  9. "gitlink.org.cn/cloudream/storage2/client/internal/mount/vfs/cache"
  10. clitypes "gitlink.org.cn/cloudream/storage2/client/types"
  11. "gorm.io/gorm"
  12. )
  13. type FuseNode interface {
  14. PathComps() []string
  15. }
  16. func child(vfs *Vfs, ctx context.Context, parent FuseNode, name string) (fuse.FsEntry, error) {
  17. parentPathComps := parent.PathComps()
  18. childPathComps := lo2.AppendNew(parentPathComps, name)
  19. ca := vfs.cache.Stat(childPathComps)
  20. if ca == nil {
  21. var ret fuse.FsEntry
  22. d := vfs.db
  23. err := d.DoTx(func(tx db.SQLContext) error {
  24. pkg, err := d.Package().GetByFullName(tx, childPathComps[0], childPathComps[1])
  25. if err != nil {
  26. if err != gorm.ErrRecordNotFound {
  27. return err
  28. }
  29. return nil
  30. }
  31. objPath := clitypes.JoinObjectPath(childPathComps[2:]...)
  32. obj, err := d.Object().GetByPath(tx, pkg.PackageID, objPath)
  33. if err == nil {
  34. ret = newFileFromObject(vfs, childPathComps, obj)
  35. return nil
  36. }
  37. if err != gorm.ErrRecordNotFound {
  38. return err
  39. }
  40. err = d.Object().HasObjectWithPrefix(tx, pkg.PackageID, objPath+clitypes.ObjectPathSeparator)
  41. if err == nil {
  42. dir := vfs.cache.LoadDir(childPathComps, &cache.CreateDirOption{
  43. ModTime: time.Now(),
  44. })
  45. if dir == nil {
  46. return nil
  47. }
  48. ret = newDirFromCache(dir.Info(), vfs)
  49. return nil
  50. }
  51. if err == gorm.ErrRecordNotFound {
  52. return nil
  53. }
  54. return err
  55. })
  56. if err != nil {
  57. return nil, err
  58. }
  59. if ret == nil {
  60. return nil, fuse.ErrNotExists
  61. }
  62. return ret, nil
  63. }
  64. if ca.IsDir {
  65. return newDirFromCache(*ca, vfs), nil
  66. }
  67. return newFileFromCache(*ca, vfs), nil
  68. }
  69. func listChildren(vfs *Vfs, ctx context.Context, parent FuseNode) ([]fuse.FsEntry, error) {
  70. var ens []fuse.FsEntry
  71. myPathComps := parent.PathComps()
  72. infos := vfs.cache.StatMany(myPathComps)
  73. dbEntries := make(map[string]fuse.FsEntry)
  74. d := vfs.db
  75. d.DoTx(func(tx db.SQLContext) error {
  76. pkg, err := d.Package().GetByFullName(tx, myPathComps[0], myPathComps[1])
  77. if err != nil {
  78. return err
  79. }
  80. objPath := clitypes.JoinObjectPath(myPathComps[2:]...)
  81. objs, coms, err := d.Object().GetByPrefixGrouped(tx, pkg.PackageID, objPath+clitypes.ObjectPathSeparator)
  82. if err != nil {
  83. return err
  84. }
  85. for _, dir := range coms {
  86. dir = strings.TrimSuffix(dir, clitypes.ObjectPathSeparator)
  87. pathComps := lo2.AppendNew(myPathComps, clitypes.BaseName(dir))
  88. cd := vfs.cache.LoadDir(pathComps, &cache.CreateDirOption{
  89. ModTime: time.Now(),
  90. })
  91. if cd == nil {
  92. continue
  93. }
  94. dbEntries[dir] = newDirFromCache(cd.Info(), vfs)
  95. }
  96. for _, obj := range objs {
  97. pathComps := lo2.AppendNew(myPathComps, clitypes.BaseName(obj.Path))
  98. file := newFileFromObject(vfs, pathComps, obj)
  99. dbEntries[file.Name()] = file
  100. }
  101. return nil
  102. })
  103. for _, c := range infos {
  104. delete(dbEntries, c.PathComps[len(c.PathComps)-1])
  105. if c.IsDir {
  106. ens = append(ens, newDirFromCache(c, vfs))
  107. } else {
  108. ens = append(ens, newFileFromCache(c, vfs))
  109. }
  110. }
  111. for _, e := range dbEntries {
  112. ens = append(ens, e)
  113. }
  114. return ens, nil
  115. }
  116. func newDir(vfs *Vfs, ctx context.Context, name string, parent FuseNode) (fuse.FsDir, error) {
  117. cache := vfs.cache.CreateDir(lo2.AppendNew(parent.PathComps(), name))
  118. if cache == nil {
  119. return nil, fuse.ErrPermission
  120. }
  121. return newDirFromCache(cache.Info(), vfs), nil
  122. }
  123. func newFile(vfs *Vfs, ctx context.Context, name string, parent FuseNode, flags uint32) (fuse.FileHandle, uint32, error) {
  124. cache := vfs.cache.CreateFile(lo2.AppendNew(parent.PathComps(), name))
  125. if cache == nil {
  126. return nil, 0, fuse.ErrPermission
  127. }
  128. defer cache.Release()
  129. // Open之后会给cache的引用计数额外+1,即使cache先于FileHandle被关闭,
  130. // 也有有FileHandle的计数保持cache的有效性
  131. fileNode := newFileFromCache(cache.Info(), vfs)
  132. hd := cache.Open(flags)
  133. return newFileHandle(fileNode, hd), flags, nil
  134. }
  135. func removeChild(vfs *Vfs, ctx context.Context, name string, parent FuseNode) error {
  136. pathComps := lo2.AppendNew(parent.PathComps(), name)
  137. joinedPath := clitypes.JoinObjectPath(pathComps[2:]...)
  138. d := vfs.db
  139. // TODO 生成系统事件
  140. return vfs.db.DoTx(func(tx db.SQLContext) error {
  141. pkg, err := d.Package().GetByFullName(tx, pathComps[0], pathComps[1])
  142. if err == nil {
  143. err := d.Object().HasObjectWithPrefix(tx, pkg.PackageID, joinedPath+clitypes.ObjectPathSeparator)
  144. if err == nil {
  145. return fuse.ErrNotEmpty
  146. }
  147. if err != gorm.ErrRecordNotFound {
  148. return err
  149. }
  150. // 存储系统不会保存目录结构,所以这里是尝试删除同名文件
  151. err = d.Object().DeleteByPath(tx, pkg.PackageID, joinedPath)
  152. if err != nil {
  153. return err
  154. }
  155. } else if err != gorm.ErrRecordNotFound {
  156. return err
  157. }
  158. return vfs.cache.Remove(pathComps)
  159. })
  160. }
  161. func moveChild(vfs *Vfs, ctx context.Context, oldName string, oldParent FuseNode, newName string, newParent FuseNode) error {
  162. newParentPath := newParent.PathComps()
  163. newChildPath := lo2.AppendNew(newParentPath, newName)
  164. newChildPathJoined := clitypes.JoinObjectPath(newChildPath[2:]...)
  165. // 不允许移动任何内容到Package层级以上
  166. if len(newParentPath) < 2 {
  167. return fuse.ErrNotSupported
  168. }
  169. oldChildPath := lo2.AppendNew(oldParent.PathComps(), oldName)
  170. oldChildPathJoined := clitypes.JoinObjectPath(oldChildPath[2:]...)
  171. // 先更新远程,再更新本地,因为远程使用事务更新,可以回滚,而本地不行
  172. return vfs.db.DoTx(func(tx db.SQLContext) error {
  173. err := moveRemote(vfs, tx, oldChildPath, newParentPath, oldChildPathJoined, newChildPathJoined)
  174. if err == fuse.ErrExists {
  175. return err
  176. }
  177. if err != nil && err != fuse.ErrNotExists {
  178. return err
  179. }
  180. err2 := vfs.cache.Move(oldChildPath, newChildPath)
  181. if err == fuse.ErrNotExists && err2 == fuse.ErrNotExists {
  182. return fuse.ErrNotExists
  183. }
  184. return err2
  185. })
  186. }
  187. func moveRemote(vfs *Vfs, tx db.SQLContext, oldChildPath []string, newParentPath []string, oldChildPathJoined string, newChildPathJoined string) error {
  188. d := vfs.db
  189. newPkg, err := d.Package().GetByFullName(tx, newParentPath[0], newParentPath[1])
  190. if err != nil {
  191. if err == gorm.ErrRecordNotFound {
  192. return fuse.ErrNotExists
  193. }
  194. return err
  195. }
  196. // 检查目的文件或文件夹是否已经存在
  197. _, err = d.Object().GetByPath(tx, newPkg.PackageID, newChildPathJoined)
  198. if err == nil {
  199. return fuse.ErrExists
  200. }
  201. err = d.Object().HasObjectWithPrefix(tx, newPkg.PackageID, newChildPathJoined+clitypes.ObjectPathSeparator)
  202. if err == nil {
  203. return fuse.ErrExists
  204. }
  205. if err != gorm.ErrRecordNotFound {
  206. return err
  207. }
  208. // 按理来说还需要检查远程文件所在的文件夹是否存在,但对象存储是不存文件夹的,所以不检查,导致的后果就是移动时会创建不存在的文件夹
  209. oldPkg, err := d.Package().GetByFullName(tx, oldChildPath[0], oldChildPath[1])
  210. if err != nil {
  211. if err == gorm.ErrRecordNotFound {
  212. return fuse.ErrNotExists
  213. }
  214. return err
  215. }
  216. // 都不存在,就开始移动文件
  217. oldObj, err := d.Object().GetByPath(tx, oldPkg.PackageID, oldChildPathJoined)
  218. if err == nil {
  219. oldObj.PackageID = newPkg.PackageID
  220. oldObj.Path = newChildPathJoined
  221. return d.Object().BatchUpdate(tx, []clitypes.Object{oldObj})
  222. }
  223. if err == gorm.ErrRecordNotFound {
  224. return fuse.ErrNotExists
  225. }
  226. err = d.Object().HasObjectWithPrefix(tx, oldObj.PackageID, oldChildPathJoined+clitypes.ObjectPathSeparator)
  227. if err == nil {
  228. return d.Object().MoveByPrefix(tx,
  229. oldPkg.PackageID, oldChildPathJoined+clitypes.ObjectPathSeparator,
  230. newPkg.PackageID, newChildPathJoined+clitypes.ObjectPathSeparator,
  231. )
  232. }
  233. if err == gorm.ErrRecordNotFound {
  234. return fuse.ErrNotExists
  235. }
  236. return err
  237. }

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