Repository navigation
Expand file tree
/
Copy pathfile_linux.go
More file actions
163 lines (147 loc) · 4.75 KB
/
Copy pathfile_linux.go
File metadata and controls
163 lines (147 loc) · 4.75 KB
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
//go:build linux
package log
import (
"errors"
"io"
"os"
"path/filepath"
"syscall"
"unsafe"
)
// writevFunc is the syscall used by writevFull. It is a variable so tests can
// inject short writes and errors without a real filesystem failure.
var writevFunc = writev
// errWritevOverflow is reported when writev claims to have written more bytes
// than were requested, which would corrupt the iovec progress accounting.
var errWritevOverflow = errors.New("log: writev wrote more bytes than requested")
// WriteV writes all iovecs to the writer. It returns the number of bytes
// actually written, which can be smaller than the total iovec length when a
// write error occurs. The iovecs are advanced in place, so a caller that
// wants to retry a partial write can inspect the leftover iovecs.
func (w *FileWriter) WriteV(iovs []syscall.Iovec) (n uintptr, err error) {
n, _, err = w.writevAll(iovs)
return
}
// writevAll writes every byte of iovs while holding the file lock, and returns
// the number of bytes written together with the iovec suffix that has not been
// fully written. The suffix is what makes a partial write recoverable: the
// caller knows exactly which entries are done and which bytes still need to be
// written.
func (w *FileWriter) writevAll(iovs []syscall.Iovec) (written uintptr, remaining []syscall.Iovec, err error) {
if len(iovs) == 0 {
return 0, nil, nil
}
w.mu.Lock()
defer w.mu.Unlock()
if w.file == nil {
if w.Filename == "" {
return writevFull(syscall.Stderr, iovs)
}
if w.EnsureFolder {
err = os.MkdirAll(filepath.Dir(w.Filename), 0755)
if err != nil {
return 0, iovs, err
}
}
err = w.create()
if err != nil {
return 0, iovs, err
}
}
written, remaining, err = writevFull(int(w.file.Fd()), iovs)
// Count partially written bytes too, otherwise the rotation size drifts
// and the next rotation happens too late.
w.size += int64(written)
if err == nil && w.MaxSize > 0 && w.size > w.MaxSize && w.Filename != "" {
err = w.rotate()
}
return
}
// writevFull writes all bytes described by iovs to fd, advancing iovs in
// place. It returns the number of bytes written and the iovec suffix that has
// not been fully written, so callers can account for partial progress and
// retry only the leftover. A syscall that makes no progress is reported as
// io.ErrShortWrite instead of spinning forever.
func writevFull(fd int, iovs []syscall.Iovec) (written uintptr, remaining []syscall.Iovec, err error) {
return writevFullWith(fd, iovs, writevFunc)
}
// writevFullWith is writevFull with an injectable syscall, which keeps the
// iovec consumption logic testable without real short writes.
func writevFullWith(fd int, iovs []syscall.Iovec, write func(fd int, iovecs []syscall.Iovec) (uintptr, error)) (written uintptr, remaining []syscall.Iovec, err error) {
pending := iovecsTrim(iovs)
for len(pending) > 0 {
n, e := write(fd, pending)
if n == ^uintptr(0) { // -1 means aborted
n = 0
}
if n > iovecsLen(pending) {
return written, pending, errWritevOverflow
}
written += n
pending = consumeIovecs(pending, n)
// EINTR is retried here instead of being returned, so an injected
// writev does not have to loop and writever never sees EINTR.
if e == syscall.EINTR {
continue
}
if e != nil {
return written, pending, e
}
if len(pending) == 0 {
return written, nil, nil
}
if n == 0 {
return written, pending, io.ErrShortWrite
}
}
return written, nil, nil
}
// iovecsTrim drops leading zero-length iovecs, which writev ignores anyway.
func iovecsTrim(iovs []syscall.Iovec) []syscall.Iovec {
for len(iovs) > 0 && iovs[0].Len == 0 {
iovs = iovs[1:]
}
return iovs
}
// iovecsLen returns the total number of bytes described by iovs.
func iovecsLen(iovs []syscall.Iovec) (n uintptr) {
for i := range iovs {
n += uintptr(iovs[i].Len)
}
return
}
// consumeIovecs advances iovs by n bytes in place and returns the remainder.
// Fully consumed iovecs are dropped; a partially consumed iovec keeps its
// buffer pointer advanced by the consumed bytes.
func consumeIovecs(iovs []syscall.Iovec, n uintptr) []syscall.Iovec {
for len(iovs) > 0 {
if n >= uintptr(iovs[0].Len) {
n -= uintptr(iovs[0].Len)
iovs = iovs[1:]
continue
}
if n > 0 {
iovs[0].Base = (*byte)(unsafe.Add(unsafe.Pointer(iovs[0].Base), n))
iovs[0].SetLen(int(iovs[0].Len) - int(n))
}
return iovs
}
return nil
}
// from https://github.com/golang/go/blob/master/src/internal/poll/fd_writev_unix.go
func writev(fd int, iovecs []syscall.Iovec) (uintptr, error) {
var (
r uintptr
e syscall.Errno
)
for {
r, _, e = syscall.Syscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovecs[0])), uintptr(len(iovecs)))
if e != syscall.EINTR {
break
}
}
if e != 0 {
return r, e
}
return r, nil
}