-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchFile.go
84 lines (71 loc) · 1.47 KB
/
watchFile.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
package watchFile
import (
"os"
"syscall"
)
type WatchFile struct {
F *os.File
Name string
Flag int
Perm os.FileMode
Dev uint64
Ino uint64
}
func (wf *WatchFile) Open() error {
f, err := os.OpenFile(wf.Name, wf.Flag, wf.Perm)
if err != nil {
return err
}
wf.F = f
fileInfo, err := os.Stat(wf.Name)
if err != nil {
wf.F.Close()
return err
}
stat := fileInfo.Sys().(*syscall.Stat_t)
wf.Dev = uint64(stat.Dev)
wf.Ino = uint64(stat.Ino)
return nil
}
// Reopen file if needed.
func (wf *WatchFile) Reopen() error {
fileInfo, err := os.Stat(wf.Name)
if err != nil {
wf.F.Close()
return wf.Open()
} else {
stat := fileInfo.Sys().(*syscall.Stat_t)
if wf.Dev != uint64(stat.Dev) || wf.Ino != uint64(stat.Ino) {
wf.F.Close()
return wf.Open()
}
}
return nil
}
func (wf *WatchFile) WriteString(s string) (int, error) {
err := wf.Reopen()
if err != nil {
return 0, err
}
return wf.F.WriteString(s)
}
func (wf *WatchFile) Write(p []byte) (int, error) {
err := wf.Reopen()
if err != nil {
return 0, err
}
return wf.F.Write(p)
}
func (wf *WatchFile) Close() error {
return wf.F.Close()
}
func OpenWatchFile(name string) (*WatchFile, error) {
var wf = WatchFile{Name: name, Flag: os.O_WRONLY | os.O_APPEND | os.O_CREATE, Perm: 0666}
err := wf.Open()
return &wf, err
}
func OpenWatchFile2(name string, flag int, perm os.FileMode) (*WatchFile, error) {
var wf = WatchFile{Name: name, Flag: flag, Perm: perm}
err := wf.Open()
return &wf, err
}