-
Notifications
You must be signed in to change notification settings - Fork 1
/
gowml.go
158 lines (139 loc) · 3.09 KB
/
gowml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
var srcPath = flag.String("s", "", "source directory")
var desPath = flag.String("d", "", "destination directory")
func main() {
//创建一个监控对象
watch, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watch.Close()
//添加要监控的对象,文件或文件夹
flag.Parse()
UserSrc := *srcPath
UserDes := *desPath
if UserSrc == "" {
log.Fatal("Src can not be null")
os.Exit(0)
}
if UserDes == "" {
log.Fatal("Destination can not be null")
os.Exit(0)
}
err = watch.Add(UserSrc)
if err != nil {
log.Fatal(err)
}
err = filepath.Walk(UserSrc, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
watch.Add(path)
} else {
oldPath := UserSrc
newPath := UserDes
inPath := strings.Replace(path, oldPath, "", 1)
newFilePath := newPath + inPath
copy(path, newFilePath)
}
log.Println(path, info.Size())
return nil
})
log.Println(err)
//我们另启一个goroutine来处理监控对象的事件
go func(watcher *fsnotify.Watcher) {
for {
select {
case ev := <-watch.Events:
{
oldPath := UserSrc
newPath := UserDes
inPath := strings.Replace(ev.Name, oldPath, "", 1)
newFilePath := newPath + inPath
// fmt.Println(newFilePath)
//判断事件发生的类型,如下5种
// Create
// Write
// Remove
// Rename
// Chmod
if ev.Op&fsnotify.Create == fsnotify.Create {
if IsDir(ev.Name) {
log.Println("Create Directory: ", ev.Name)
watcher.Add(ev.Name)
} else {
log.Println("Create File: ", ev.Name)
copy(ev.Name, newFilePath)
}
}
if ev.Op&fsnotify.Write == fsnotify.Write {
log.Println("Write : ", ev.Name)
copy(ev.Name, newFilePath)
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("Remove : ", ev.Name)
}
if ev.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("Rename : ", ev.Name)
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
// log.Println("Chmod : ", ev.Name)
}
}
case err := <-watch.Errors:
{
log.Println("error : ", err)
return
}
}
}
}(watch)
//循环
select {}
}
func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
fileDirPath := path.Dir(dst)
fmt.Println("fileDirPath", fileDirPath)
err = os.MkdirAll(fileDirPath, 0777)
fmt.Println("error[mkdir]: ", err)
destination, err := os.Create(dst)
if err != nil {
fmt.Println(err)
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
fmt.Println("写入成功")
return nBytes, err
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}