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.

92 lines
2.0 KiB

  1. package filer
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/viant/ptrie"
  9. )
  10. const (
  11. DirectoryEtc = "/etc"
  12. FilerConfName = "filer.conf"
  13. )
  14. type FilerConf struct {
  15. rules ptrie.Trie
  16. }
  17. func NewFilerConf() (fc *FilerConf) {
  18. fc = &FilerConf{
  19. rules: ptrie.New(),
  20. }
  21. return fc
  22. }
  23. func (fc *FilerConf) loadFromFiler(filer *Filer) (err error){
  24. filerConfPath := util.NewFullPath(DirectoryEtc, FilerConfName)
  25. entry, err := filer.FindEntry(context.Background(), filerConfPath)
  26. if err != nil {
  27. if err == filer_pb.ErrNotFound {
  28. return nil
  29. }
  30. glog.Errorf("read filer conf entry %s: %v", filerConfPath, err)
  31. return
  32. }
  33. return fc.loadFromChunks(filer, entry.Chunks)
  34. }
  35. func (fc *FilerConf) loadFromChunks(filer *Filer, chunks []*filer_pb.FileChunk) (err error) {
  36. data, err := filer.readEntry(chunks)
  37. if err != nil {
  38. glog.Errorf("read filer conf content: %v", err)
  39. return
  40. }
  41. return fc.loadFromBytes(data)
  42. }
  43. func (fc *FilerConf) loadFromBytes(data []byte) (err error) {
  44. conf := &filer_pb.FilerConf{}
  45. err = proto.UnmarshalText(string(data), conf)
  46. if err != nil {
  47. glog.Errorf("unable to parse filer conf: %v", err)
  48. // this is not recoverable
  49. return nil
  50. }
  51. return fc.doLoadConf(conf)
  52. }
  53. func (fc *FilerConf) doLoadConf(conf *filer_pb.FilerConf) (err error) {
  54. for _, location := range conf.Locations {
  55. err = fc.rules.Put([]byte(location.LocationPrefix), location)
  56. if err != nil {
  57. glog.Errorf("put location prefix: %v", err)
  58. // this is not recoverable
  59. return nil
  60. }
  61. }
  62. return nil
  63. }
  64. var (
  65. EmptyFilerConfPathConf = &filer_pb.FilerConf_PathConf{}
  66. )
  67. func (fc *FilerConf) MatchStorageRule(path string) (pathConf *filer_pb.FilerConf_PathConf){
  68. fc.rules.MatchPrefix([]byte(path), func(key []byte, value interface{}) bool {
  69. pathConf = value.(*filer_pb.FilerConf_PathConf)
  70. return true
  71. })
  72. if pathConf == nil {
  73. return EmptyFilerConfPathConf
  74. }
  75. return pathConf
  76. }