xattr.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package filesys
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/fuse"
  9. )
  10. func getxattr(entry *filer_pb.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_pb.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_pb.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_pb.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(ctx context.Context, dir, name string) (entry *filer_pb.Entry, err error) {
  81. fullpath := filer2.NewFullPath(dir, name)
  82. entry = wfs.cacheGet(fullpath)
  83. if entry != nil {
  84. return
  85. }
  86. // glog.V(3).Infof("read entry cache miss %s", fullpath)
  87. err = wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  88. request := &filer_pb.LookupDirectoryEntryRequest{
  89. Name: name,
  90. Directory: dir,
  91. }
  92. resp, err := client.LookupDirectoryEntry(ctx, request)
  93. if err != nil || resp == nil || resp.Entry == nil {
  94. if err == filer2.ErrNotFound || strings.Contains(err.Error(), filer2.ErrNotFound.Error()) {
  95. glog.V(3).Infof("file attr read not found file %v: %v", request, err)
  96. return fuse.ENOENT
  97. }
  98. glog.V(3).Infof("attr read %v: %v", request, err)
  99. return fuse.EIO
  100. }
  101. entry = resp.Entry
  102. wfs.cacheSet(fullpath, entry, wfs.option.EntryCacheTtl)
  103. return nil
  104. })
  105. return
  106. }