You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. }