read_write.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package filer
  2. import (
  3. "bytes"
  4. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  5. "github.com/chrislusf/seaweedfs/weed/wdclient"
  6. "time"
  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. Collection: "",
  51. Replication: "",
  52. FileSize: uint64(len(content)),
  53. },
  54. Content: content,
  55. },
  56. })
  57. } else if err == nil {
  58. entry := resp.Entry
  59. entry.Content = content
  60. entry.Attributes.Mtime = time.Now().Unix()
  61. entry.Attributes.FileSize = uint64(len(content))
  62. err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  63. Directory: dir,
  64. Entry: entry,
  65. })
  66. }
  67. return err
  68. }