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_dir.go 2.9 kB

8 months ago
8 months ago
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package vfs
  2. import (
  3. "context"
  4. "os"
  5. "time"
  6. db2 "gitlink.org.cn/cloudream/storage2/client/internal/db"
  7. "gitlink.org.cn/cloudream/storage2/client/internal/mount/fuse"
  8. "gitlink.org.cn/cloudream/storage2/client/internal/mount/vfs/cache"
  9. clitypes "gitlink.org.cn/cloudream/storage2/client/types"
  10. "gorm.io/gorm"
  11. )
  12. type FuseDir struct {
  13. vfs *Vfs
  14. pathComps []string
  15. modTime time.Time
  16. mode os.FileMode
  17. }
  18. func newDirFromCache(ch cache.CacheEntryInfo, vfs *Vfs) *FuseDir {
  19. return &FuseDir{
  20. vfs: vfs,
  21. pathComps: ch.PathComps,
  22. modTime: ch.ModTime,
  23. mode: ch.Mode,
  24. }
  25. }
  26. func (r *FuseDir) PathComps() []string {
  27. return r.pathComps
  28. }
  29. func (r *FuseDir) Name() string {
  30. return r.pathComps[len(r.pathComps)-1]
  31. }
  32. func (r *FuseDir) Size() int64 {
  33. return 0
  34. }
  35. func (r *FuseDir) Mode() os.FileMode {
  36. return os.ModeDir | r.mode
  37. }
  38. func (r *FuseDir) ModTime() time.Time {
  39. return r.modTime
  40. }
  41. func (r *FuseDir) IsDir() bool {
  42. return true
  43. }
  44. func (r *FuseDir) SetModTime(time time.Time) error {
  45. dir := r.loadCacheDir()
  46. if dir == nil {
  47. return fuse.ErrNotExists
  48. }
  49. return dir.SetModTime(time)
  50. }
  51. // 如果不存在,应该返回ErrNotExists
  52. func (r *FuseDir) Child(ctx context.Context, name string) (fuse.FsEntry, error) {
  53. return child(r.vfs, ctx, r, name)
  54. }
  55. func (r *FuseDir) Children(ctx context.Context) ([]fuse.FsEntry, error) {
  56. return listChildren(r.vfs, ctx, r)
  57. }
  58. func (r *FuseDir) ReadChildren() (fuse.DirReader, error) {
  59. ens, err := listChildren(r.vfs, context.Background(), r)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return newFuseDirReader(ens), nil
  64. }
  65. func (r *FuseDir) NewDir(ctx context.Context, name string) (fuse.FsDir, error) {
  66. return newDir(r.vfs, ctx, name, r)
  67. }
  68. func (r *FuseDir) NewFile(ctx context.Context, name string, flags uint32) (fuse.FileHandle, uint32, error) {
  69. return newFile(r.vfs, ctx, name, r, flags)
  70. }
  71. func (r *FuseDir) RemoveChild(ctx context.Context, name string) error {
  72. return removeChild(r.vfs, ctx, name, r)
  73. }
  74. func (r *FuseDir) MoveChild(ctx context.Context, oldName string, newName string, newParent fuse.FsDir) error {
  75. return moveChild(r.vfs, ctx, oldName, r, newName, newParent.(FuseNode))
  76. }
  77. func (r *FuseDir) loadCacheDir() *cache.CacheDir {
  78. var createOpt *cache.CreateDirOption
  79. err := r.vfs.db.DoTx(func(tx db2.SQLContext) error {
  80. pkg, err := r.vfs.db.Package().GetByFullName(tx, r.pathComps[0], r.pathComps[1])
  81. if err != nil {
  82. return err
  83. }
  84. err = r.vfs.db.Object().HasObjectWithPrefix(tx, pkg.PackageID, clitypes.JoinObjectPath(r.pathComps[2:]...))
  85. if err == nil {
  86. createOpt = &cache.CreateDirOption{
  87. ModTime: time.Now(),
  88. }
  89. return nil
  90. }
  91. if err == gorm.ErrRecordNotFound {
  92. return nil
  93. }
  94. return err
  95. })
  96. if err != nil {
  97. return nil
  98. }
  99. return r.vfs.cache.LoadDir(r.pathComps, createOpt)
  100. }
  101. var _ fuse.FsDir = (*FuseDir)(nil)
  102. var _ FuseNode = (*FuseDir)(nil)

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