/
fmonitor.go
179 lines (160 loc) · 5.32 KB
/
fmonitor.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package fmonitor
import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"time"
)
const (
MAX_SLEEP_TIME = 100 /* ms */
)
type Op int
const (
OP_ADD Op = 0x01
OP_DEL Op = 0x02
OP_MOD Op = 0x04
)
type FileOp struct {
op Op
fname string
}
type FileMonitor struct {
fpath string
f chan FileOp
}
func (fo FileOp) GetOpValue() Op {
return fo.op
}
func (fo FileOp) GetNameValue() string {
return fo.fname
}
func NewFileMonitor(name string) *FileMonitor {
fm := new(FileMonitor)
fm.fpath = name
fm.f = make(chan FileOp)
return fm
}
func (fm *FileMonitor) Release() {
close(fm.f)
}
func (fm *FileMonitor) GetFileOp() FileOp {
fo := <-fm.f
return fo
}
func getDirInfo(fpath string, fin map[string]os.FileInfo) {
for {
if fi, err := os.Stat(fpath); err != nil {
panic(err)
} else {
if fi.IsDir() {
if f, err := os.Open(fpath); err != nil {
panic(err)
} else {
v, err := f.Readdirnames(0)
if err != nil {
panic(err)
}
f.Close()
for _, v0 := range v {
v0 = filepath.Join(fpath, v0)
getDirInfo(v0, fin)
}
break
}
} else {
fin[fpath] = fi
break
}
}
}
}
func (cmc *FileMonitor) cmpStat(f_old map[string]os.FileInfo, f_new map[string]os.FileInfo) {
var fo FileOp
for i, v := range f_old {
if in, ok := f_new[i]; ok {
if !v.ModTime().Equal(in.ModTime()) {
// update
f_old[i] = in
delete(f_new, i)
fo.fname = i
fo.op = OP_MOD
cmc.f <- fo
}
delete(f_new, i)
// no changed
} else {
delete(f_old, i)
fo.fname = i
fo.op = OP_DEL
cmc.f <- fo
// delete
}
}
for i, v := range f_new {
// add
f_old[i] = v
delete(f_new, i)
fo.fname = i
fo.op = OP_ADD
cmc.f <- fo
}
debug.FreeOSMemory()
}
func (cmc *FileMonitor) Monitoring() {
var ffi *os.FileInfo
var fo FileOp
if fi, err := os.Stat(cmc.fpath); err != nil {
panic(err)
} else {
if fi.IsDir() {
e_new := make(map[string]os.FileInfo)
e_old := make(map[string]os.FileInfo)
// filepath.Walk(fpath, walkFunc)
getDirInfo(cmc.fpath, e_old)
for {
time.Sleep(time.Millisecond * time.Duration(MAX_SLEEP_TIME))
getDirInfo(cmc.fpath, e_new)
cmc.cmpStat(e_old, e_new)
}
}
ffi = &fi
}
for {
time.Sleep(time.Millisecond * time.Duration(MAX_SLEEP_TIME))
if fi, err := os.Stat(cmc.fpath); err != nil {
if os.IsNotExist(err) {
fo.fname = cmc.fpath
fo.op = OP_DEL
cmc.f <- fo
return
}
} else {
if (*ffi).ModTime().Equal(fi.ModTime()) {
continue
}
fo.fname = cmc.fpath
fo.op = OP_MOD
cmc.f <- fo
ffi = &fi
debug.FreeOSMemory()
}
}
}
func print_stat(cmc *FileMonitor) {
for {
select {
case s := <-cmc.f:
switch s.op {
case OP_ADD:
fmt.Printf("The file <%s> has been added.\n", s.fname)
case OP_DEL:
fmt.Printf("The file <%s> has been deleted.\n", s.fname)
case OP_MOD:
fmt.Printf("The file <%s> has been changed.\n", s.fname)
default:
fmt.Printf("Unknown operation on the file <%s>.\n", s.fname)
}
}
}
}