func (p *SyncPair) loopMsg() { p.tokens = make(chan bool, runtime.NumCPU()) for { select { case e := <-p.watcher.Events: URIString := p.formatLeftUriString(e.Name) u, err := uri.Parse(URIString) ext := filepath.Ext(u.Abs()) if p.IgnoreMap[ext] { logger.Info("ignored", u.Uri()) continue } if err != nil { logger.Error(err.Error()) continue } go p.handle(SyncMsg{e.Op, u}) case e := <-p.watcher.Errors: logger.Info(e.Error()) } } }
func (s *Syncer) newPair(config settings.SyncConfig, source, target string, IgnoreRules []string) error { lURI, err := uri.Parse(source) if err != nil { return err } rURI, err := uri.Parse(target) if err != nil { return err } var m = make(map[string]bool, 6) for _, ignore := range IgnoreRules { m[ignore] = true } pair := &SyncPair{ Left: lURI, Right: rURI, Config: config, IgnoreMap: m, } fmt.Println("s.ignore", IgnoreRules) pair.watcher, err = fsnotify.NewWatcher() if err != nil { return err } if !pair.Left.IsAbs() || !pair.Right.IsAbs() { err = PairNotValidError{pair.Left.Abs(), pair.Right.Abs(), "Pair Uris not absolute"} return err } if !pair.Left.Exist() { err = PairNotValidError{pair.Left.Abs(), pair.Right.Abs(), "Res of left Uri not exist"} return err } s.SyncPairs = append(s.SyncPairs, pair) s.PairMap[pair.Left.Uri()] = pair.Right.Uri() go func(p *SyncPair) { p.BeginWatch() }(pair) return nil }
func (p *SyncPair) ToRight(u uri.Uri) (uri.Uri, error) { lTmp := p.Left.Uri() rTmp := p.Right.Uri() lTmplen := len(lTmp) rTmplen := len(rTmp) if lTmp[lTmplen-1] == '/' { lTmp = lTmp[0 : lTmplen-1] } if rTmp[rTmplen-1] == '/' { rTmp = rTmp[0 : rTmplen-1] } Uris := strings.Replace(u.Uri(), lTmp, rTmp, -1) return uri.Parse(Uris) }
func TestToRight(t *testing.T) { ln, _ := uri.Parse("local://D:/pictures/") rn, _ := uri.Parse("local://E:/pictures") p := &SyncPair{ Left: ln, Right: rn, } lnn, _ := uri.Parse("local://D:/pictures/2015-2.jpg") rnn, err := p.ToRight(lnn) if err != nil { t.Error(err.Error()) } if rnn.Uri() != "local://E:/pictures/2015-2.jpg" { t.Error("ToRight: " + rnn.Uri()) } ln, _ = uri.Parse("local://D:/pictures/") rn, _ = uri.Parse("local://E:/pictures/") p = &SyncPair{ Left: ln, Right: rn, } lnn, _ = uri.Parse("local://D:/pictures/2015-2.jpg") rnn, err = p.ToRight(lnn) if err != nil { t.Error(err.Error()) } if rnn.Uri() != "local://E:/pictures/2015-2.jpg" { t.Error("ToRight: " + rnn.Uri()) } ln, _ = uri.Parse("local://D:/pictures") rn, _ = uri.Parse("local://E:/pictures") p = &SyncPair{ Left: ln, Right: rn, } lnn, _ = uri.Parse("local://D:/pictures/2015-2.jpg") rnn, err = p.ToRight(lnn) if err != nil { t.Error(err.Error()) } if rnn.Uri() != "local://E:/pictures/2015-2.jpg" { t.Error("ToRight: " + rnn.Uri()) } ln, _ = uri.Parse("local://D:/pictures") rn, _ = uri.Parse("local://E:/pictures/") p = &SyncPair{ Left: ln, Right: rn, } lnn, _ = uri.Parse("local://D:/pictures/2015-2.jpg") rnn, err = p.ToRight(lnn) if err != nil { t.Error(err.Error()) } if rnn.Uri() != "local://E:/pictures/2015-2.jpg" { t.Error("ToRight: " + rnn.Uri()) } }