xattr.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package filesys
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/fuse"
  6. "github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func getxattr(entry *filer.Entry, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  11. if entry == nil {
  12. return fuse.ErrNoXattr
  13. }
  14. if entry.Extended == nil {
  15. return fuse.ErrNoXattr
  16. }
  17. data, found := entry.Extended[req.Name]
  18. if !found {
  19. return fuse.ErrNoXattr
  20. }
  21. if req.Position < uint32(len(data)) {
  22. size := req.Size
  23. if req.Position+size >= uint32(len(data)) {
  24. size = uint32(len(data)) - req.Position
  25. }
  26. if size == 0 {
  27. resp.Xattr = data[req.Position:]
  28. } else {
  29. resp.Xattr = data[req.Position : req.Position+size]
  30. }
  31. }
  32. return nil
  33. }
  34. func setxattr(entry *filer.Entry, req *fuse.SetxattrRequest) error {
  35. if entry == nil {
  36. return fuse.EIO
  37. }
  38. if entry.Extended == nil {
  39. entry.Extended = make(map[string][]byte)
  40. }
  41. data, _ := entry.Extended[req.Name]
  42. newData := make([]byte, int(req.Position)+len(req.Xattr))
  43. copy(newData, data)
  44. copy(newData[int(req.Position):], req.Xattr)
  45. entry.Extended[req.Name] = newData
  46. return nil
  47. }
  48. func removexattr(entry *filer.Entry, req *fuse.RemovexattrRequest) error {
  49. if entry == nil {
  50. return fuse.ErrNoXattr
  51. }
  52. if entry.Extended == nil {
  53. return fuse.ErrNoXattr
  54. }
  55. _, found := entry.Extended[req.Name]
  56. if !found {
  57. return fuse.ErrNoXattr
  58. }
  59. delete(entry.Extended, req.Name)
  60. return nil
  61. }
  62. func listxattr(entry *filer.Entry, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  63. if entry == nil {
  64. return fuse.EIO
  65. }
  66. for k := range entry.Extended {
  67. resp.Append(k)
  68. }
  69. size := req.Size
  70. if req.Position+size >= uint32(len(resp.Xattr)) {
  71. size = uint32(len(resp.Xattr)) - req.Position
  72. }
  73. if size == 0 {
  74. resp.Xattr = resp.Xattr[req.Position:]
  75. } else {
  76. resp.Xattr = resp.Xattr[req.Position : req.Position+size]
  77. }
  78. return nil
  79. }
  80. func (wfs *WFS) maybeLoadEntry(dir, name string) (entry *filer.Entry, err error) {
  81. fullpath := util.NewFullPath(dir, name)
  82. // glog.V(3).Infof("read entry cache miss %s", fullpath)
  83. // read from async meta cache
  84. meta_cache.EnsureVisited(wfs.metaCache, wfs, util.FullPath(dir))
  85. cachedEntry, cacheErr := wfs.metaCache.FindEntry(context.Background(), fullpath)
  86. if cacheErr == filer_pb.ErrNotFound {
  87. return nil, fuse.ENOENT
  88. }
  89. return cachedEntry, cacheErr
  90. }