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.

119 lines
3.3 KiB

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