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 3.0 kB

8 months ago
8 months ago
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package vfs
  2. import (
  3. "context"
  4. "os"
  5. "time"
  6. db2 "gitlink.org.cn/cloudream/jcs-pub/client/internal/db"
  7. "gitlink.org.cn/cloudream/jcs-pub/client/internal/mount/fuse"
  8. "gitlink.org.cn/cloudream/jcs-pub/client/internal/mount/vfs/cache"
  9. clitypes "gitlink.org.cn/cloudream/jcs-pub/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. info := r.vfs.cache.Stat(r.pathComps)
  40. if info == nil {
  41. return r.modTime
  42. }
  43. return info.ModTime
  44. }
  45. func (r *FuseDir) IsDir() bool {
  46. return true
  47. }
  48. func (r *FuseDir) SetModTime(time time.Time) error {
  49. dir := r.loadCacheDir()
  50. if dir == nil {
  51. return fuse.ErrNotExists
  52. }
  53. return dir.SetModTime(time)
  54. }
  55. // 如果不存在,应该返回ErrNotExists
  56. func (r *FuseDir) Child(ctx context.Context, name string) (fuse.FsEntry, error) {
  57. return child(r.vfs, ctx, r, name)
  58. }
  59. func (r *FuseDir) Children(ctx context.Context) ([]fuse.FsEntry, error) {
  60. return listChildren(r.vfs, ctx, r)
  61. }
  62. func (r *FuseDir) ReadChildren() (fuse.DirReader, error) {
  63. ens, err := listChildren(r.vfs, context.Background(), r)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return newFuseDirReader(ens), nil
  68. }
  69. func (r *FuseDir) NewDir(ctx context.Context, name string) (fuse.FsDir, error) {
  70. return newDir(r.vfs, ctx, name, r)
  71. }
  72. func (r *FuseDir) NewFile(ctx context.Context, name string, flags uint32) (fuse.FileHandle, uint32, error) {
  73. return newFile(r.vfs, ctx, name, r, flags)
  74. }
  75. func (r *FuseDir) RemoveChild(ctx context.Context, name string) error {
  76. return removeChild(r.vfs, ctx, name, r)
  77. }
  78. func (r *FuseDir) MoveChild(ctx context.Context, oldName string, newName string, newParent fuse.FsDir) error {
  79. return moveChild(r.vfs, ctx, oldName, r, newName, newParent.(FuseNode))
  80. }
  81. func (r *FuseDir) loadCacheDir() *cache.CacheDir {
  82. var createOpt *cache.CreateDirOption
  83. err := r.vfs.db.DoTx(func(tx db2.SQLContext) error {
  84. pkg, err := r.vfs.db.Package().GetByFullName(tx, r.pathComps[0], r.pathComps[1])
  85. if err != nil {
  86. return err
  87. }
  88. err = r.vfs.db.Object().HasObjectWithPrefix(tx, pkg.PackageID, clitypes.JoinObjectPath(r.pathComps[2:]...))
  89. if err == nil {
  90. createOpt = &cache.CreateDirOption{
  91. ModTime: time.Now(),
  92. }
  93. return nil
  94. }
  95. if err == gorm.ErrRecordNotFound {
  96. return nil
  97. }
  98. return err
  99. })
  100. if err != nil {
  101. return nil
  102. }
  103. return r.vfs.cache.LoadDir(r.pathComps, createOpt)
  104. }
  105. var _ fuse.FsDir = (*FuseDir)(nil)
  106. var _ FuseNode = (*FuseDir)(nil)

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