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.

79 lines
2.1 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
4 years ago
  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. SkipCheckParentDirectory: true,
  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. }