diff --git a/fileSync b/fileSync index 0797917..a0920b1 100755 Binary files a/fileSync and b/fileSync differ diff --git a/fileSync.exe b/fileSync.exe index 173b584..0efd131 100644 Binary files a/fileSync.exe and b/fileSync.exe differ diff --git a/fileSync.go b/fileSync.go index 6844fd7..dde5b00 100644 --- a/fileSync.go +++ b/fileSync.go @@ -1,13 +1,10 @@ package main import ( - "bufio" "bytes" "encoding/binary" - //"fmt" "github.com/fsnotify/fsnotify" "io" - "net" "os" "path/filepath" "strings" @@ -15,18 +12,17 @@ import ( ) const ( - SendDur = 2 - SleepDur = 86400 + SendDur = 1500 // 发送时间间隔毫秒 + SleepDur = 86400000 // 定期器初始睡眠时间 ) var CollectFiles map[string]struct{} var SendTimer *time.Timer var Str bytes.Buffer -var Msg *bytes.Buffer var AddDirs []string var OnlyDirs []string var DelDirs []string -var Conn net.Conn +var LenBuff []byte const ( hrl = ".hrl" @@ -40,6 +36,7 @@ const ( git = ".git" lock = ".lock" bea = ".bea" + config = ".config" ) type Watch struct { @@ -49,33 +46,12 @@ type Watch struct { // 收集更改了的文件 func CollectFile(File string) { ext := filepath.Ext(File) - if ext != idea && ext != git && ext != svn && ext != lock && ext != bea && ext != "" && ext != idea && (ext == erl || ext == beam || ext == hrl || ext == ex || ext == dtl || ext == lfe) { + if ext != idea && ext != git && ext != svn && ext != lock && ext != bea && ext != "" && ext != idea && (ext == erl || ext == beam || ext == hrl || ext == config || ext == ex || ext == dtl || ext == lfe) { CollectFiles[File] = struct{}{} - SendTimer.Reset(time.Second * SendDur) + SendTimer.Reset(time.Millisecond * SendDur) } } -// 发送文件列表到erl层 -func SendToErl() { - // 拼写数据 - for k := range CollectFiles { - Str.WriteString(k) - Str.WriteString("\r\n") - } - CollectFiles = map[string]struct{}{} - - var length = int32(len(Str.Bytes())) - //写入消息头 - _ = binary.Write(Msg, binary.BigEndian, length) - //写入消息体 - _ = binary.Write(Msg, binary.BigEndian, Str.Bytes()) - Conn.Write(Msg.Bytes()) - Str.Reset() - Msg.Reset() - - SendTimer.Reset(time.Second * SleepDur) -} - func isHidden(path string) bool { for i := len(path) - 1; i > 0; i-- { if path[i] != '.' { @@ -119,6 +95,18 @@ func isDelDir(dirs []string, curDirs string) bool { return false } +// 判断所给路径文件/文件夹是否存在 +func existPath(path string) bool { + _, err := os.Stat(path) //os.Stat获取文件信息 + if err != nil { + if os.IsNotExist(err) { + return false + } + return true + } + return true +} + //监控目录 func (w *Watch) watchDir(dir string) { //通过Walk来遍历目录下的所有子目录 @@ -129,24 +117,17 @@ func (w *Watch) watchDir(dir string) { if err != nil { return err } - if !isHidden(path) { - if isOnlyDir(OnlyDirs, path) { - if !isDelDir(DelDirs, path) { - err = w.watch.Add(path) - if err != nil { - // fmt.Println("watch err : ", path, err) - return err - } - // fmt.Println("watch success : ", path) - } + if !isHidden(path) && isOnlyDir(OnlyDirs, path) && !isDelDir(DelDirs, path) { + err = w.watch.Add(path) + if err != nil { + return err } } } return nil }) - for _, v := range AddDirs { - if v != "" { + if v != "" && existPath(v) { //通过Walk来遍历目录下的所有子目录 filepath.Walk(v, func(path string, info os.FileInfo, err error) error { //这里判断是否为目录,只需监控目录即可 目录下的文件也在监控范围内,不需要我们一个一个加 @@ -158,10 +139,8 @@ func (w *Watch) watchDir(dir string) { if !isHidden(path) { err = w.watch.Add(path) if err != nil { - // fmt.Println("watch AddDirs err : ", err) return err } - // fmt.Println("watch AddDirs success : ", path) } } return nil @@ -180,36 +159,29 @@ func (w *Watch) watchDir(dir string) { fi, err := os.Stat(ev.Name) if err == nil && fi.IsDir() { // 新建了文件夹 - // fmt.Println("创建文件夹 : ", ev.Name) if !isHidden(ev.Name) { if isOnlyDir(OnlyDirs, ev.Name) { if !isDelDir(DelDirs, ev.Name) { w.watch.Add(ev.Name) - // fmt.Println("添加监控 : ", ev.Name) } } } } else { // 新建了文件 CollectFile(ev.Name) - // fmt.Println("创建文件 : ", ev.Name) } } if ev.Op&fsnotify.Write == fsnotify.Write { CollectFile(ev.Name) - // fmt.Println("写入文件 : ", ev.Name) } if ev.Op&fsnotify.Remove == fsnotify.Remove { - // fmt.Println("删除文件 : ", ev.Name) //如果删除文件是目录,则移除监控 fi, err := os.Stat(ev.Name) if err == nil && fi.IsDir() { w.watch.Remove(ev.Name) - // fmt.Println("删除监控 : ", ev.Name) } } if ev.Op&fsnotify.Rename == fsnotify.Rename { - // fmt.Println("重命名文件 : ", ev.Name) //如果重命名文件是目录,则移除监控 //注意这里无法使用os.Stat来判断是否是目录了 //因为重命名后,go已经无法找到原文件来获取信息了 @@ -217,12 +189,10 @@ func (w *Watch) watchDir(dir string) { w.watch.Remove(ev.Name) } if ev.Op&fsnotify.Chmod == fsnotify.Chmod { - // fmt.Println("修改权限 : ", ev.Name) } } case <-w.watch.Errors: { - // fmt.Println("error : ", err) return } case <-SendTimer.C: @@ -232,82 +202,68 @@ func (w *Watch) watchDir(dir string) { }() } -func read(reader *bufio.Reader) ([]byte, error) { - // Peek 返回缓存的一个切片,该切片引用缓存中前 n 个字节的数据, - // 该操作不会将数据读出,只是引用,引用的数据在下一次读取操作之 - // 前是有效的。如果切片长度小于 n,则返回一个错误信息说明原因。 - // 如果 n 大于缓存的总大小,则返回 ErrBufferFull。 - lengthByte, err := reader.Peek(4) - if err != nil { - return nil, err +//********************************************** port start ************************************************************ +func ReadLen() (int32, error) { + if _, err := io.ReadFull(os.Stdin, LenBuff); err != nil { + return 0, err } - //创建 Buffer缓冲器 - lengthBuff := bytes.NewBuffer(lengthByte) - var length int32 - // 通过Read接口可以将buf中得内容填充到data参数表示的数据结构中 - err = binary.Read(lengthBuff, binary.BigEndian, &length) + size := int32(binary.BigEndian.Uint32(LenBuff)) + + return size, nil +} + +func Read() ([]byte, error) { + len, err := ReadLen() if err != nil { return nil, err + } else if len == 0 { + return []byte{}, nil } - // Buffered 返回缓存中未读取的数据的长度 - if int32(reader.Buffered()) < length+4 { - return nil, err + data := make([]byte, len) + size, err := io.ReadFull(os.Stdin, data) + return data[:size], err +} + +func Write(data []byte) (int, error) { + size := len(data) + binary.BigEndian.PutUint32(LenBuff, uint32(size)) + + if _, err := os.Stdout.Write(LenBuff); err != nil { + return 0, err } - // 读取消息真正的内容 - pack := make([]byte, int(4+length)) - // Read 从 b 中读出数据到 p 中,返回读出的字节数和遇到的错误。 - // 如果缓存不为空,则只能读出缓存中的数据,不会从底层 io.Reader - // 中提取数据,如果缓存为空,则: - // 1、len(p) >= 缓存大小,则跳过缓存,直接从底层 io.Reader 中读 - // 出到 p 中。 - // 2、len(p) < 缓存大小,则先将数据从底层 io.Reader 中读取到缓存 - // 中,再从缓存读取到 p 中。 - _, err = reader.Read(pack) - if err != nil { - return nil, err + + return os.Stdout.Write(data) +} + +// 发送文件列表到erl层 +func SendToErl() { + for k := range CollectFiles { + Str.WriteString(k) + Str.WriteString("\r\n") } - return pack[4:], nil + CollectFiles = map[string]struct{}{} + Write(Str.Bytes()) + Str.Reset() } +//********************************************** port end ************************************************************ func main() { CollectFiles = map[string]struct{}{} - SendTimer = time.NewTimer(time.Second * SleepDur) + SendTimer = time.NewTimer(time.Millisecond * SleepDur) defer SendTimer.Stop() - - Addr := "localhost:" + os.Args[2] - var err error - Conn, err = net.Dial("tcp", Addr) - if err != nil { - //fmt.Println("IMY****************建立tcp失败 : ", Addr) - return - } - - // 建立tcp 连接后需要从erlSync 接受监听目录相关配置 - var reader *bufio.Reader - reader = bufio.NewReader(Conn) - data, err := read(reader) - if err == io.EOF { - //fmt.Println("IMY****************Tcp 断开链接 : ", Addr) + LenBuff = make([]byte, 4) + + Write([]byte("init")) + data, err := Read() + if err == io.EOF || err != nil { return } - if err != nil { - //fmt.Println("IMY****************Tcp read err : ", err) - return - } - //fmt.Println("IMY****************建立tcp成功 : ", os.Args[0]) - //fmt.Println("IMY****************Tcp read data : ", string(data)) dirs := strings.Split(string(data), "\r\n") AddDirs = strings.Split(dirs[0], "|") OnlyDirs = strings.Split(dirs[1], "|") DelDirs = strings.Split(dirs[2], "|") - - Msg = new(bytes.Buffer) watch, _ := fsnotify.NewWatcher() w := Watch{watch: watch} - w.watchDir(os.Args[1]) - - data = make([]byte, 10) - _, _ = Conn.Read(data) - Conn.Close() - // fmt.Println("IMY****************tcp关闭了 : ", os.Args[0]) + w.watchDir("./") + Read() }