|
| 1 | +package canal |
| 2 | + |
| 3 | +import ( |
| 4 | +"bytes" |
| 5 | +"os" |
| 6 | + |
| 7 | +"github.com/BurntSushi/toml" |
| 8 | +"github.com/instructure/mc-go-mysql/mysql" |
| 9 | +"github.com/pingcap/errors" |
| 10 | +"github.com/siddontang/go-log/log" |
| 11 | +"github.com/siddontang/go/ioutil2" |
| 12 | +) |
| 13 | + |
| 14 | +type fsInfoLoader struct { |
| 15 | +path string |
| 16 | +} |
| 17 | + |
| 18 | +func NewFsInfoLoader(path string) MasterInfoLoader { |
| 19 | +return &fsInfoLoader{path: path} |
| 20 | +} |
| 21 | + |
| 22 | +func (l *fsInfoLoader) Load(setValues MasterInfoSetter) error { |
| 23 | +f, err := os.Open(l.path) |
| 24 | +if err != nil && !os.IsNotExist(errors.Cause(err)) { |
| 25 | +return errors.Trace(err) |
| 26 | +} else if os.IsNotExist(errors.Cause(err)) { |
| 27 | +return nil |
| 28 | +} |
| 29 | +defer f.Close() |
| 30 | + |
| 31 | +var m masterInfo |
| 32 | +_, err = toml.DecodeReader(f, &m) |
| 33 | + |
| 34 | +if err != nil { |
| 35 | +return err |
| 36 | +} |
| 37 | + |
| 38 | +return setValues(m.Addr, m.pos.Name, m.pos.Pos) |
| 39 | +} |
| 40 | + |
| 41 | +func (l *fsInfoLoader) Save(addr, name string, position uint32, force bool) error { |
| 42 | +var buf bytes.Buffer |
| 43 | +e := toml.NewEncoder(&buf) |
| 44 | +pos := mysql.Position{Name: name, Pos: position} |
| 45 | + |
| 46 | +m := &masterInfo{ |
| 47 | +Addr: addr, |
| 48 | +pos: pos, |
| 49 | +} |
| 50 | + |
| 51 | +enc_err := e.Encode(m) |
| 52 | +if enc_err != nil { |
| 53 | +log.Errorf("canal save master info to file %s err %v", l.path, enc_err) |
| 54 | +} |
| 55 | + |
| 56 | +var err error |
| 57 | +if err = ioutil2.WriteFileAtomic(l.path, buf.Bytes(), 0644); err != nil { |
| 58 | +log.Errorf("canal save master info to file %s err %v", l.path, err) |
| 59 | +} |
| 60 | + |
| 61 | +return errors.Trace(err) |
| 62 | +} |
0 commit comments