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

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package vfs
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. "gitlink.org.cn/cloudream/common/utils/lo2"
  10. "gitlink.org.cn/cloudream/storage/client2/internal/mount/fuse"
  11. "gitlink.org.cn/cloudream/storage/client2/internal/mount/vfs/cache"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/db2"
  13. "gorm.io/gorm"
  14. )
  15. type FuseDir struct {
  16. vfs *Vfs
  17. pathComps []string
  18. modTime time.Time
  19. mode os.FileMode
  20. }
  21. func newDirFromCache(ch cache.CacheEntryInfo, vfs *Vfs) *FuseDir {
  22. return &FuseDir{
  23. vfs: vfs,
  24. pathComps: ch.PathComps,
  25. modTime: ch.ModTime,
  26. mode: ch.Mode,
  27. }
  28. }
  29. func (r *FuseDir) PathComps() []string {
  30. return r.pathComps
  31. }
  32. func (r *FuseDir) Name() string {
  33. return r.pathComps[len(r.pathComps)-1]
  34. }
  35. func (r *FuseDir) Size() int64 {
  36. return 0
  37. }
  38. func (r *FuseDir) Mode() os.FileMode {
  39. return os.ModeDir | r.mode
  40. }
  41. func (r *FuseDir) ModTime() time.Time {
  42. return r.modTime
  43. }
  44. func (r *FuseDir) IsDir() bool {
  45. return true
  46. }
  47. func (r *FuseDir) SetModTime(time time.Time) error {
  48. dir := r.loadCacheDir()
  49. if dir == nil {
  50. return fuse.ErrNotExists
  51. }
  52. return dir.SetModTime(time)
  53. }
  54. // 如果不存在,应该返回ErrNotExists
  55. func (r *FuseDir) Child(ctx context.Context, name string) (fuse.FsEntry, error) {
  56. childPathComps := lo2.AppendNew(r.pathComps, name)
  57. ca := r.vfs.cache.Stat(childPathComps)
  58. if ca == nil {
  59. var ret fuse.FsEntry
  60. db := r.vfs.db
  61. err := db.DoTx(func(tx db2.SQLContext) error {
  62. pkg, err := db.Package().GetUserPackageByName(tx, 1, r.pathComps[0], r.pathComps[1])
  63. if err != nil {
  64. return err
  65. }
  66. objPath := cdssdk.JoinObjectPath(childPathComps[2:]...)
  67. obj, err := db.Object().GetByPath(tx, pkg.PackageID, objPath)
  68. if err == nil {
  69. ret = newFileFromObject(r.vfs, childPathComps, obj)
  70. return nil
  71. }
  72. if err != gorm.ErrRecordNotFound {
  73. return err
  74. }
  75. has, err := db.Object().HasObjectWithPrefix(tx, pkg.PackageID, objPath+cdssdk.ObjectPathSeparator)
  76. if err != nil {
  77. return err
  78. }
  79. if has {
  80. dir := r.vfs.cache.LoadDir(childPathComps, &cache.CreateDirOption{
  81. ModTime: time.Now(),
  82. })
  83. if dir == nil {
  84. return nil
  85. }
  86. ret = newDirFromCache(dir.Info(), r.vfs)
  87. }
  88. return nil
  89. })
  90. if err != nil {
  91. return nil, err
  92. }
  93. if ret == nil {
  94. return nil, fuse.ErrNotExists
  95. }
  96. return ret, nil
  97. }
  98. if ca.IsDir {
  99. return newDirFromCache(*ca, r.vfs), nil
  100. }
  101. return newFileFromCache(*ca, r.vfs), nil
  102. }
  103. func (r *FuseDir) Children(ctx context.Context) ([]fuse.FsEntry, error) {
  104. return r.listChildren()
  105. }
  106. func (r *FuseDir) ReadChildren() (fuse.DirReader, error) {
  107. ens, err := r.listChildren()
  108. if err != nil {
  109. return nil, err
  110. }
  111. return newFuseDirReader(ens), nil
  112. }
  113. func (r *FuseDir) listChildren() ([]fuse.FsEntry, error) {
  114. var ens []fuse.FsEntry
  115. infos := r.vfs.cache.StatMany(r.pathComps)
  116. dbEntries := make(map[string]fuse.FsEntry)
  117. db := r.vfs.db
  118. db.DoTx(func(tx db2.SQLContext) error {
  119. // TODO UserID
  120. pkg, err := db.Package().GetUserPackageByName(tx, 1, r.pathComps[0], r.pathComps[1])
  121. if err != nil {
  122. return err
  123. }
  124. objPath := cdssdk.JoinObjectPath(r.pathComps[2:]...)
  125. coms, err := db.Object().GetCommonPrefixes(tx, pkg.PackageID, objPath+cdssdk.ObjectPathSeparator)
  126. if err != nil {
  127. return fmt.Errorf("getting common prefixes: %w", err)
  128. }
  129. objs, err := db.Object().GetDirectChildren(tx, pkg.PackageID, objPath+cdssdk.ObjectPathSeparator)
  130. if err != nil {
  131. return fmt.Errorf("getting direct children: %w", err)
  132. }
  133. for _, dir := range coms {
  134. dir = strings.TrimSuffix(dir, cdssdk.ObjectPathSeparator)
  135. pathComps := lo2.AppendNew(r.pathComps, cdssdk.BaseName(dir))
  136. cd := r.vfs.cache.LoadDir(pathComps, &cache.CreateDirOption{
  137. ModTime: time.Now(),
  138. })
  139. if cd == nil {
  140. continue
  141. }
  142. dbEntries[dir] = newDirFromCache(cd.Info(), r.vfs)
  143. }
  144. for _, obj := range objs {
  145. pathComps := lo2.AppendNew(r.pathComps, cdssdk.BaseName(obj.Path))
  146. file := newFileFromObject(r.vfs, pathComps, obj)
  147. dbEntries[file.Name()] = file
  148. }
  149. return nil
  150. })
  151. for _, c := range infos {
  152. delete(dbEntries, c.PathComps[len(c.PathComps)-1])
  153. if c.IsDir {
  154. ens = append(ens, newDirFromCache(c, r.vfs))
  155. } else {
  156. ens = append(ens, newFileFromCache(c, r.vfs))
  157. }
  158. }
  159. for _, e := range dbEntries {
  160. ens = append(ens, e)
  161. }
  162. return ens, nil
  163. }
  164. func (r *FuseDir) NewDir(ctx context.Context, name string) (fuse.FsDir, error) {
  165. cache := r.vfs.cache.CreateDir(lo2.AppendNew(r.pathComps, name))
  166. if cache == nil {
  167. return nil, fuse.ErrPermission
  168. }
  169. return newDirFromCache(cache.Info(), r.vfs), nil
  170. }
  171. func (r *FuseDir) NewFile(ctx context.Context, name string, flags uint32) (fuse.FileHandle, uint32, error) {
  172. cache := r.vfs.cache.CreateFile(lo2.AppendNew(r.pathComps, name))
  173. if cache == nil {
  174. return nil, 0, fuse.ErrPermission
  175. }
  176. // Open之后会给cache的引用计数额外+1,即使cache先于FileHandle被关闭,
  177. // 也有有FileHandle的计数保持cache的有效性
  178. fileNode := newFileFromCache(cache.Info(), r.vfs)
  179. hd := cache.Open(flags)
  180. return newFileHandle(fileNode, hd), flags, nil
  181. }
  182. func (r *FuseDir) RemoveChild(ctx context.Context, name string) error {
  183. pathComps := lo2.AppendNew(r.pathComps, name)
  184. joinedPath := cdssdk.JoinObjectPath(pathComps[2:]...)
  185. d := r.vfs.db
  186. // TODO 生成系统事件
  187. return r.vfs.db.DoTx(func(tx db2.SQLContext) error {
  188. pkg, err := d.Package().GetUserPackageByName(tx, 1, pathComps[0], pathComps[1])
  189. if err == nil {
  190. has, err := d.Object().HasObjectWithPrefix(tx, pkg.PackageID, joinedPath+cdssdk.ObjectPathSeparator)
  191. if err != nil {
  192. return err
  193. }
  194. if has {
  195. return fuse.ErrNotEmpty
  196. }
  197. }
  198. err = r.vfs.cache.Remove(pathComps)
  199. if err != nil {
  200. return err
  201. }
  202. if pkg.PackageID > 0 {
  203. // 存储系统不会保存目录结构,所以这里是尝试删除同名文件
  204. d.Object().DeleteByPath(tx, pkg.PackageID, joinedPath)
  205. }
  206. return nil
  207. })
  208. }
  209. func (r *FuseDir) MoveChild(ctx context.Context, oldName string, newName string, newParent fuse.FsDir) error {
  210. newParentNode := newParent.(FuseNode)
  211. newParentPath := newParentNode.PathComps()
  212. newChildPath := lo2.AppendNew(newParentPath, newName)
  213. newChildPathJoined := cdssdk.JoinObjectPath(newChildPath[2:]...)
  214. // 不允许移动任何内容到Package层级以上
  215. if len(newParentPath) < 2 {
  216. return fuse.ErrNotSupported
  217. }
  218. oldChildPath := lo2.AppendNew(r.PathComps(), oldName)
  219. oldChildPathJoined := cdssdk.JoinObjectPath(oldChildPath[2:]...)
  220. // 先更新远程,再更新本地,因为远程使用事务更新,可以回滚,而本地不行
  221. d := r.vfs.db
  222. return r.vfs.db.DoTx(func(tx db2.SQLContext) error {
  223. newPkg, err := d.Package().GetUserPackageByName(tx, 1, newParentPath[0], newParentPath[1])
  224. if err != nil {
  225. if err == gorm.ErrRecordNotFound {
  226. return fuse.ErrNotExists
  227. }
  228. return err
  229. }
  230. oldPkg, err := d.Package().GetUserPackageByName(tx, 1, oldChildPath[0], oldChildPath[1])
  231. if err != nil {
  232. if err == gorm.ErrRecordNotFound {
  233. return fuse.ErrNotExists
  234. }
  235. return err
  236. }
  237. // 检查目的文件或文件夹是否已经存在
  238. _, err = d.Object().GetByPath(tx, newPkg.PackageID, newChildPathJoined)
  239. if err == nil {
  240. return fuse.ErrExists
  241. }
  242. has, err := d.Object().HasObjectWithPrefix(tx, newPkg.PackageID, newChildPathJoined+cdssdk.ObjectPathSeparator)
  243. if err != nil {
  244. return err
  245. }
  246. if has {
  247. return fuse.ErrExists
  248. }
  249. // 都不存在,就开始移动文件
  250. oldObj, err := d.Object().GetByPath(tx, oldPkg.PackageID, oldChildPathJoined)
  251. if err == nil {
  252. oldObj.PackageID = newPkg.PackageID
  253. oldObj.Path = newChildPathJoined
  254. err = d.Object().BatchUpdate(tx, []cdssdk.Object{oldObj})
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. err = d.Object().MoveByPrefix(tx,
  260. oldPkg.PackageID, oldChildPathJoined+cdssdk.ObjectPathSeparator,
  261. newPkg.PackageID, newChildPathJoined+cdssdk.ObjectPathSeparator,
  262. )
  263. if err != nil {
  264. return err
  265. }
  266. return r.vfs.cache.Move(oldChildPath, newChildPath)
  267. })
  268. }
  269. func (r *FuseDir) loadCacheDir() *cache.CacheDir {
  270. var createOpt *cache.CreateDirOption
  271. err := r.vfs.db.DoTx(func(tx db2.SQLContext) error {
  272. pkg, err := r.vfs.db.Package().GetUserPackageByName(tx, 1, r.pathComps[0], r.pathComps[1])
  273. if err != nil {
  274. return err
  275. }
  276. has, err := r.vfs.db.Object().HasObjectWithPrefix(tx, pkg.PackageID, cdssdk.JoinObjectPath(r.pathComps[2:]...))
  277. if err != nil {
  278. return err
  279. }
  280. if has {
  281. createOpt = &cache.CreateDirOption{
  282. ModTime: time.Now(),
  283. }
  284. }
  285. return nil
  286. })
  287. if err != nil {
  288. return nil
  289. }
  290. return r.vfs.cache.LoadDir(r.pathComps, createOpt)
  291. }
  292. var _ fuse.FsDir = (*FuseDir)(nil)
  293. var _ FuseNode = (*FuseDir)(nil)

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