(p []byte)
| 236 | } |
| 237 | |
| 238 | func (w *headTailBuffer) Write(p []byte) (int, error) { |
| 239 | n := len(p) |
| 240 | if room := bashOutputHead - len(w.head); room > 0 { |
| 241 | take := min(room, len(p)) |
| 242 | w.head = append(w.head, p[:take]...) |
| 243 | p = p[take:] |
| 244 | } |
| 245 | if len(p) == 0 { |
| 246 | return n, nil |
| 247 | } |
| 248 | if w.ring == nil { |
| 249 | w.ring = make([]byte, bashOutputTail) |
| 250 | } |
| 251 | w.tailBytes += int64(len(p)) |
| 252 | if len(p) >= bashOutputTail { |
| 253 | copy(w.ring, p[len(p)-bashOutputTail:]) |
| 254 | w.pos = 0 |
| 255 | return n, nil |
| 256 | } |
| 257 | k := copy(w.ring[w.pos:], p) |
| 258 | w.pos = (w.pos + k) % bashOutputTail |
| 259 | if k < len(p) { |
| 260 | w.pos = copy(w.ring, p[k:]) |
| 261 | } |
| 262 | return n, nil |
| 263 | } |
| 264 | |
| 265 | // droppedBytes is how many middle bytes the ring discarded; 0 while the whole |
| 266 | // output still fits head+tail. |
no outgoing calls