read_write.go 2.0 KB

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