forked from pingcap/tidb-tools
/
event.go
75 lines (67 loc) · 1.26 KB
/
event.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
package watcher
import (
"bytes"
"os"
)
// Op represents file operation type
type Op uint32
// Operations type current supported
const (
Create Op = 1 << iota
Remove
Modify
Rename
Chmod
Move
)
func (op Op) String() string {
var buffer bytes.Buffer
// now, only one Op will used in polling, but it can combine multi Ops if needed
if op&Create == Create {
buffer.WriteString("|CREATE")
}
if op&Remove == Remove {
buffer.WriteString("|REMOVE")
}
if op&Modify == Modify {
buffer.WriteString("|MODIFY")
}
if op&Rename == Rename {
buffer.WriteString("|RENAME")
}
if op&Chmod == Chmod {
buffer.WriteString("|CHMOD")
}
if op&Move == Move {
buffer.WriteString("|MOVE")
}
if buffer.Len() == 0 {
return ""
}
return buffer.String()[1:] // Strip leading pipe
}
// Event represents a single file operation event
type Event struct {
Path string
Op Op
FileInfo os.FileInfo
}
// IsDirEvent returns whether is a event for a directory
func (e *Event) IsDirEvent() bool {
if e == nil {
return false
}
return e.FileInfo.IsDir()
}
// HasOps checks whether has any specified operation types
func (e *Event) HasOps(ops ...Op) bool {
if e == nil {
return false
}
for _, op := range ops {
if e.Op&op != 0 {
return true
}
}
return false
}