SisMaker před 4 roky
rodič
revize
d62313361e
5 změnil soubory, kde provedl 113 přidání a 16 odebrání
  1. +5
    -2
      .gitignore
  2. +13
    -11
      LICENSE
  3. +2
    -3
      README.md
  4. +88
    -0
      fileSync.go
  5. +5
    -0
      go.mod

+ 5
- 2
.gitignore Zobrazit soubor

@ -1,4 +1,3 @@
# ---> Go
# Binaries for programs and plugins
*.exe
*.exe~
@ -12,6 +11,10 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
go.sum
.idea
*.iml
# Dependency directories (remove the comment below to include it)
# vendor/

+ 13
- 11
LICENSE Zobrazit soubor

@ -1,19 +1,21 @@
MIT License Copyright (c) <year> <copyright holders>
MIT License
Copyright (c) 2020 AICells
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 2
- 3
README.md Zobrazit soubor

@ -1,3 +1,2 @@
# fileSync
基于fsnotify的文件监控模块,为erlSync自动编译加载提供更改监听功能!
# fileSync 同步文件更改相关

+ 88
- 0
fileSync.go Zobrazit soubor

@ -0,0 +1,88 @@
package main
import (
"github.com/fsnotify/fsnotify"
"fmt"
"path/filepath"
"os"
)
type Watch struct {
watch *fsnotify.Watcher
}
//监控目录
func (w *Watch) watchDir(dir string) {
//通过Walk来遍历目录下的所有子目录
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
//这里判断是否为目录,只需监控目录即可
//目录下的文件也在监控范围内,不需要我们一个一个加
if info.IsDir() {
path, err := filepath.Abs(path)
if err != nil {
return err
}
err = w.watch.Add(path)
if err != nil {
return err
}
fmt.Println("监控 : ", path)
}
return nil
})
go func() {
for {
select {
case ev := <-w.watch.Events:
{
if ev.Op&fsnotify.Create == fsnotify.Create {
fmt.Println("创建文件 : ", ev.Name)
//这里获取新创建文件的信息,如果是目录,则加入监控中
fi, err := os.Stat(ev.Name)
if err == nil && fi.IsDir() {
w.watch.Add(ev.Name)
fmt.Println("添加监控 : ", ev.Name)
}
}
if ev.Op&fsnotify.Write == fsnotify.Write {
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已经无法找到原文件来获取信息了
//所以这里就简单粗爆的直接remove好了
w.watch.Remove(ev.Name)
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
fmt.Println("修改权限 : ", ev.Name)
}
}
case err := <-w.watch.Errors:
{
fmt.Println("error : ", err)
return
}
}
}
}()
}
func main() {
watch, _ := fsnotify.NewWatcher()
w := Watch{
watch: watch,
}
w.watchDir("./")
select {}
}

+ 5
- 0
go.mod Zobrazit soubor

@ -0,0 +1,5 @@
module fileSync
go 1.15
require github.com/fsnotify/fsnotify v1.4.9

Načítá se…
Zrušit
Uložit