local_sink.go 3.7 KB

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