func (p *SyncPair) handleRemove(lName uri.Uri) { fmt.Println(lName.Abs(), "removed") rName, err := p.ToRight(lName) if err != nil { logger.Error(err.Error()) return } if !rName.Exist() { return } for { err := rName.Remove() if err != nil { fmt.Println(err.Error()) time.Sleep(time.Second * 1) } else { break } } fmt.Println(rName.Abs(), "removed") err = p.watcher.Remove(lName.Abs()) if err != nil { logger.Warn(err.Error()) } logger.Info("Remove successfully: " + rName.Abs()) return }
func (p *SyncPair) WatchLeft(left uri.Uri) error { for { err := p.watcher.Add(left.Abs()) if err != nil { err = p.watcher.Add(left.Abs()) } else { break } } return nil }
func copyFile(rFile, lFile uri.Uri) (io.ReadCloser, io.WriteCloser) { var err error lFd, err := lFile.OpenRead() for { if err != nil { time.Sleep(time.Second * 1) } else { break } lFd, err = lFile.OpenRead() } rFd, err := rFile.OpenWrite() for { if err != nil { time.Sleep(time.Second * 1) } else { break } rFd, err = rFile.OpenWrite() } io.Copy(rFd, lFd) return lFd, rFd }
func (p *SyncPair) handleCreate(lName uri.Uri) { rName, err := p.ToRight(lName) if err != nil { logger.Error(err.Error()) return } if !lName.ModTime().After(rName.ModTime()) { return } for { err := rName.Create(lName.IsDir(), lName.Mode()) if err == nil { break } else { time.Sleep(time.Second * 1) } } if rName.IsDir() { return } //first time copy file. lFd, rFd := copyFile(rName, lName) defer lFd.Close() defer rFd.Close() logger.Info("Sync succesfully: " + lName.Abs() + " ==> " + rName.Abs()) }
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 (p *SyncPair) handleWrite(lFile uri.Uri) { var err error rFile, err := p.ToRight(lFile) if err != nil { logger.Error(err.Error()) return } lFd, rFd := copyFile(rFile, lFile) defer lFd.Close() defer rFd.Close() logger.Info("Sync file succesfully: " + lFile.Abs() + " ==> " + rFile.Abs()) }
func (p *SyncPair) handleRename(lName uri.Uri) { fmt.Println(lName.Abs(), "rename") }