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.

78 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/wdclient"
  7. )
  8. func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error {
  9. request := &filer_pb.LookupDirectoryEntryRequest{
  10. Directory: dir,
  11. Name: name,
  12. }
  13. respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
  14. if err != nil {
  15. return err
  16. }
  17. if len(respLookupEntry.Entry.Content) > 0 {
  18. _, err = byteBuffer.Write(respLookupEntry.Entry.Content)
  19. return err
  20. }
  21. return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, int64(FileSize(respLookupEntry.Entry)))
  22. }
  23. func ReadInsideFiler(filerClient filer_pb.SeaweedFilerClient, dir, name string) (content []byte, err error) {
  24. request := &filer_pb.LookupDirectoryEntryRequest{
  25. Directory: dir,
  26. Name: name,
  27. }
  28. respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
  29. if err != nil {
  30. return
  31. }
  32. content = respLookupEntry.Entry.Content
  33. return
  34. }
  35. func SaveInsideFiler(client filer_pb.SeaweedFilerClient, dir, name string, content []byte) error {
  36. resp, err := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  37. Directory: dir,
  38. Name: name,
  39. })
  40. if err == filer_pb.ErrNotFound {
  41. err = filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  42. Directory: dir,
  43. Entry: &filer_pb.Entry{
  44. Name: name,
  45. IsDirectory: false,
  46. Attributes: &filer_pb.FuseAttributes{
  47. Mtime: time.Now().Unix(),
  48. Crtime: time.Now().Unix(),
  49. FileMode: uint32(0644),
  50. FileSize: uint64(len(content)),
  51. },
  52. Content: content,
  53. },
  54. SkipCheckParentDirectory: false,
  55. })
  56. } else if err == nil {
  57. entry := resp.Entry
  58. entry.Content = content
  59. entry.Attributes.Mtime = time.Now().Unix()
  60. entry.Attributes.FileSize = uint64(len(content))
  61. err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  62. Directory: dir,
  63. Entry: entry,
  64. })
  65. }
  66. return err
  67. }