forked from lomik/zapwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
166 lines (137 loc) · 2.83 KB
/
file.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
package zapwriter
import (
"fmt"
"net/url"
"os"
"os/user"
"path/filepath"
"strconv"
"sync"
"time"
)
// with external rotate support
type FileOutput struct {
sync.Mutex
timeout time.Duration
interval time.Duration
checkNext time.Time
f *os.File
path string // filename
exit chan interface{}
exitOnce sync.Once
exitWg sync.WaitGroup
}
func newFileOutput(path string) (*FileOutput, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
}
var timeout time.Duration
var interval time.Duration
params := DSN(u.Query())
if timeout, err = params.Duration("timeout", "1s"); err != nil {
return nil, err
}
if interval, err = params.Duration("interval", "1s"); err != nil {
return nil, err
}
f, err := os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
r := &FileOutput{
checkNext: time.Now().Add(timeout),
timeout: timeout,
interval: interval,
f: f,
path: u.Path,
exit: make(chan interface{}),
}
r.exitWg.Add(1)
go func() {
r.reopenChecker(r.exit)
r.exitWg.Done()
}()
return r, nil
}
func File(path string) (*FileOutput, error) {
return newFileOutput(path)
}
func (r *FileOutput) reopenChecker(exit chan interface{}) {
ticker := time.NewTicker(r.interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
r.doWithCheck(func() {})
case <-exit:
return
}
}
}
func (r *FileOutput) reopen() *os.File {
prev := r.f
next, err := os.OpenFile(r.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err.Error())
return r.f
}
r.f = next
prev.Close()
return r.f
}
func (r *FileOutput) Write(p []byte) (n int, err error) {
r.doWithCheck(func() { n, err = r.f.Write(p) })
return
}
func (r *FileOutput) Sync() (err error) {
r.doWithCheck(func() { err = r.f.Sync() })
return
}
func (r *FileOutput) Close() (err error) {
r.exitOnce.Do(func() {
close(r.exit)
})
r.exitWg.Wait()
err = r.f.Close()
return
}
func PrepareFileForUser(filename string, owner *user.User) error {
u, err := url.Parse(filename)
if err != nil {
return err
}
if u.Path == "" || u.Path == "stderr" || u.Path == "stdout" || u.Path == "none" {
return nil
}
if u.Scheme != "" && u.Scheme != "file" {
return nil
}
if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil {
return err
}
fd, err := os.OpenFile(u.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if fd != nil {
fd.Close()
}
if err != nil {
return err
}
if err := os.Chmod(u.Path, 0644); err != nil {
return err
}
if owner != nil {
uid, err := strconv.ParseInt(owner.Uid, 10, 0)
if err != nil {
return err
}
gid, err := strconv.ParseInt(owner.Gid, 10, 0)
if err != nil {
return err
}
if err := os.Chown(u.Path, int(uid), int(gid)); err != nil {
return err
}
}
return nil
}