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.

101 lines
2.7 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. "io/ioutil"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. )
  15. type LocalSink struct {
  16. Dir string
  17. filerSource *source.FilerSource
  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) error {
  32. localsink.Dir = dir
  33. return nil
  34. }
  35. func (localsink *LocalSink) Initialize(configuration util.Configuration, prefix string) error {
  36. dir := configuration.GetString(prefix + "directory")
  37. glog.V(4).Infof("sink.local.directory: %v", dir)
  38. return localsink.initialize(dir)
  39. }
  40. func (localsink *LocalSink) GetSinkToDirectory() string {
  41. return localsink.Dir
  42. }
  43. func (localsink *LocalSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  44. if localsink.isMultiPartEntry(key) {
  45. return nil
  46. }
  47. glog.V(4).Infof("Delete Entry key: %s", key)
  48. if err := os.Remove(key); err != nil {
  49. glog.V(0).Infof("remove entry key %s: %s", key, err)
  50. }
  51. return nil
  52. }
  53. func (localsink *LocalSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  54. if entry.IsDirectory || localsink.isMultiPartEntry(key) {
  55. return nil
  56. }
  57. glog.V(4).Infof("Create Entry key: %s", key)
  58. totalSize := filer.FileSize(entry)
  59. chunkViews := filer.ViewFromChunks(localsink.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  60. dir := filepath.Dir(key)
  61. if _, err := os.Stat(dir); os.IsNotExist(err) {
  62. glog.V(4).Infof("Create Direcotry key: %s", dir)
  63. if err = os.MkdirAll(dir, 0); err != nil {
  64. return err
  65. }
  66. }
  67. writeFunc := func(data []byte) error {
  68. writeErr := ioutil.WriteFile(key, data, 0)
  69. return writeErr
  70. }
  71. if err := repl_util.CopyFromChunkViews(chunkViews, localsink.filerSource, writeFunc); err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. func (localsink *LocalSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  77. if localsink.isMultiPartEntry(key) {
  78. return true, nil
  79. }
  80. glog.V(4).Infof("Update Entry key: %s", key)
  81. // do delete and create
  82. return false, nil
  83. }