read_write.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package filer
  2. import (
  3. "bytes"
  4. "time"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/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.GetChunks(), 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. }