local_sink.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package localsink
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/filer"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
  7. "github.com/seaweedfs/seaweedfs/weed/replication/sink"
  8. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. )
  15. type LocalSink struct {
  16. Dir string
  17. filerSource *source.FilerSource
  18. isIncremental bool
  19. }
  20. func init() {
  21. sink.Sinks = append(sink.Sinks, &LocalSink{})
  22. }
  23. func (localsink *LocalSink) SetSourceFiler(s *source.FilerSource) {
  24. localsink.filerSource = s
  25. }
  26. func (localsink *LocalSink) GetName() string {
  27. return "local"
  28. }
  29. func (localsink *LocalSink) isMultiPartEntry(key string) bool {
  30. return strings.HasSuffix(key, ".part") && strings.Contains(key, "/"+s3_constants.MultipartUploadsFolder+"/")
  31. }
  32. func (localsink *LocalSink) initialize(dir string, isIncremental bool) error {
  33. localsink.Dir = dir
  34. localsink.isIncremental = isIncremental
  35. return nil
  36. }
  37. func (localsink *LocalSink) Initialize(configuration util.Configuration, prefix string) error {
  38. dir := configuration.GetString(prefix + "directory")
  39. isIncremental := configuration.GetBool(prefix + "is_incremental")
  40. glog.V(4).Infof("sink.local.directory: %v", dir)
  41. return localsink.initialize(dir, isIncremental)
  42. }
  43. func (localsink *LocalSink) GetSinkToDirectory() string {
  44. return localsink.Dir
  45. }
  46. func (localsink *LocalSink) IsIncremental() bool {
  47. return localsink.isIncremental
  48. }
  49. func (localsink *LocalSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  50. if localsink.isMultiPartEntry(key) {
  51. return nil
  52. }
  53. glog.V(4).Infof("Delete Entry key: %s", key)
  54. if err := os.Remove(key); err != nil {
  55. glog.V(0).Infof("remove entry key %s: %s", key, err)
  56. }
  57. return nil
  58. }
  59. func (localsink *LocalSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  60. if entry.IsDirectory || localsink.isMultiPartEntry(key) {
  61. return nil
  62. }
  63. glog.V(4).Infof("Create Entry key: %s", key)
  64. totalSize := filer.FileSize(entry)
  65. chunkViews := filer.ViewFromChunks(localsink.filerSource.LookupFileId, entry.GetChunks(), 0, int64(totalSize))
  66. dir := filepath.Dir(key)
  67. if _, err := os.Stat(dir); os.IsNotExist(err) {
  68. glog.V(4).Infof("Create Directory key: %s", dir)
  69. if err = os.MkdirAll(dir, 0755); err != nil {
  70. return err
  71. }
  72. }
  73. if entry.IsDirectory {
  74. return os.Mkdir(key, os.FileMode(entry.Attributes.FileMode))
  75. }
  76. dstFile, err := os.OpenFile(key, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(entry.Attributes.FileMode))
  77. if err != nil {
  78. return err
  79. }
  80. defer dstFile.Close()
  81. writeFunc := func(data []byte) error {
  82. _, writeErr := dstFile.Write(data)
  83. return writeErr
  84. }
  85. if len(entry.Content) > 0 {
  86. return writeFunc(entry.Content)
  87. }
  88. if err := repl_util.CopyFromChunkViews(chunkViews, localsink.filerSource, writeFunc); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (localsink *LocalSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  94. if localsink.isMultiPartEntry(key) {
  95. return true, nil
  96. }
  97. glog.V(4).Infof("Update Entry key: %s", key)
  98. // do delete and create
  99. foundExistingEntry = util.FileExists(key)
  100. err = localsink.CreateEntry(key, newEntry, signatures)
  101. return
  102. }