-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_reader.go
53 lines (47 loc) · 1 KB
/
file_reader.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
package utils
import (
"io"
"os"
"github.com/fsnotify/fsnotify"
)
type FollowedFile struct {
*os.File
watcher *fsnotify.Watcher
}
func FollowFile(path string) (file *FollowedFile, err error) {
file = &FollowedFile{}
if file.File, err = os.OpenFile(path, os.O_RDONLY, 0777); err != nil {
return nil, err
}
if file.watcher, err = fsnotify.NewWatcher(); err != nil {
return nil, err
}
if err = file.watcher.Add(path); err != nil {
return nil, err
}
return file, nil
}
func (f FollowedFile) Read(b []byte) (int, error) {
for {
n, err := f.File.Read(b)
if err == io.EOF { // 现有文件已经读取完毕
select {
case _, ok := <-f.watcher.Events: // 事件到来时唤醒,重新尝试读
if !ok { // 通道已关闭
return 0, io.EOF
}
case err, ok := <-f.watcher.Errors:
if !ok { // 通道已关闭
return 0, io.EOF
}
return 0, err
}
} else {
return n, err
}
}
}
func (f FollowedFile) Close() error {
f.File.Close()
return f.watcher.Close()
}