mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
412c650e05
pkg/directory/directory.go:9:49: empty-lines: extra empty line at the start of a block (revive) pkg/pubsub/publisher.go:8:48: empty-lines: extra empty line at the start of a block (revive) pkg/loopback/attach_loopback.go:96:69: empty-lines: extra empty line at the start of a block (revive) pkg/devicemapper/devmapper_wrapper.go:136:48: empty-lines: extra empty line at the start of a block (revive) pkg/devicemapper/devmapper.go:391:35: empty-lines: extra empty line at the end of a block (revive) pkg/devicemapper/devmapper.go:676:35: empty-lines: extra empty line at the end of a block (revive) pkg/archive/changes_posix_test.go:15:38: empty-lines: extra empty line at the end of a block (revive) pkg/devicemapper/devmapper.go:241:51: empty-lines: extra empty line at the start of a block (revive) pkg/fileutils/fileutils_test.go:17:47: empty-lines: extra empty line at the end of a block (revive) pkg/fileutils/fileutils_test.go:34:48: empty-lines: extra empty line at the end of a block (revive) pkg/fileutils/fileutils_test.go:318:32: empty-lines: extra empty line at the end of a block (revive) pkg/tailfile/tailfile.go:171:6: empty-lines: extra empty line at the end of a block (revive) pkg/tarsum/fileinfosums_test.go:16:41: empty-lines: extra empty line at the end of a block (revive) pkg/tarsum/tarsum_test.go:198:42: empty-lines: extra empty line at the start of a block (revive) pkg/tarsum/tarsum_test.go:294:25: empty-lines: extra empty line at the start of a block (revive) pkg/tarsum/tarsum_test.go:407:34: empty-lines: extra empty line at the end of a block (revive) pkg/ioutils/fswriters_test.go:52:45: empty-lines: extra empty line at the end of a block (revive) pkg/ioutils/writers_test.go:24:39: empty-lines: extra empty line at the end of a block (revive) pkg/ioutils/bytespipe_test.go:78:26: empty-lines: extra empty line at the end of a block (revive) pkg/sysinfo/sysinfo_linux_test.go:13:37: empty-lines: extra empty line at the end of a block (revive) pkg/archive/archive_linux_test.go:57:64: empty-lines: extra empty line at the end of a block (revive) pkg/archive/changes.go:248:72: empty-lines: extra empty line at the start of a block (revive) pkg/archive/changes_posix_test.go:15:38: empty-lines: extra empty line at the end of a block (revive) pkg/archive/copy.go:248:124: empty-lines: extra empty line at the end of a block (revive) pkg/archive/diff_test.go:198:44: empty-lines: extra empty line at the end of a block (revive) pkg/archive/archive.go:304:12: empty-lines: extra empty line at the end of a block (revive) pkg/archive/archive.go:749:37: empty-lines: extra empty line at the end of a block (revive) pkg/archive/archive.go:812:81: empty-lines: extra empty line at the start of a block (revive) pkg/archive/copy_unix_test.go:347:34: empty-lines: extra empty line at the end of a block (revive) pkg/system/path.go:11:39: empty-lines: extra empty line at the end of a block (revive) pkg/system/meminfo_linux.go:29:21: empty-lines: extra empty line at the end of a block (revive) pkg/plugins/plugins.go:135:32: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/response.go:71:48: empty-lines: extra empty line at the start of a block (revive) pkg/authorization/api_test.go:18:51: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/middleware_test.go:23:44: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/middleware_unix_test.go:17:46: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/api_test.go:57:45: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/response.go:83:50: empty-lines: extra empty line at the start of a block (revive) pkg/authorization/api_test.go:66:47: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/middleware_unix_test.go:45:48: empty-lines: extra empty line at the end of a block (revive) pkg/authorization/response.go:145:75: empty-lines: extra empty line at the start of a block (revive) pkg/authorization/middleware_unix_test.go:56:51: empty-lines: extra empty line at the end of a block (revive) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
220 lines
4.9 KiB
Go
220 lines
4.9 KiB
Go
// Package tailfile provides helper functions to read the nth lines of any
|
|
// ReadSeeker.
|
|
package tailfile // import "github.com/docker/docker/pkg/tailfile"
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
const blockSize = 1024
|
|
|
|
var eol = []byte("\n")
|
|
|
|
// ErrNonPositiveLinesNumber is an error returned if the lines number was negative.
|
|
var ErrNonPositiveLinesNumber = errors.New("The number of lines to extract from the file must be positive")
|
|
|
|
// TailFile returns last n lines of the passed in file.
|
|
func TailFile(f *os.File, n int) ([][]byte, error) {
|
|
size, err := f.Seek(0, io.SeekEnd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rAt := io.NewSectionReader(f, 0, size)
|
|
r, nLines, err := NewTailReader(context.Background(), rAt, n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
buf := make([][]byte, 0, nLines)
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
buf = append(buf, scanner.Bytes())
|
|
}
|
|
return buf, nil
|
|
}
|
|
|
|
// SizeReaderAt is an interface used to get a ReaderAt as well as the size of the underlying reader.
|
|
// Note that the size of the underlying reader should not change when using this interface.
|
|
type SizeReaderAt interface {
|
|
io.ReaderAt
|
|
Size() int64
|
|
}
|
|
|
|
// NewTailReader scopes the passed in reader to just the last N lines passed in
|
|
func NewTailReader(ctx context.Context, r SizeReaderAt, reqLines int) (io.Reader, int, error) {
|
|
return NewTailReaderWithDelimiter(ctx, r, reqLines, eol)
|
|
}
|
|
|
|
// NewTailReaderWithDelimiter scopes the passed in reader to just the last N lines passed in
|
|
// In this case a "line" is defined by the passed in delimiter.
|
|
//
|
|
// Delimiter lengths should be generally small, no more than 12 bytes
|
|
func NewTailReaderWithDelimiter(ctx context.Context, r SizeReaderAt, reqLines int, delimiter []byte) (io.Reader, int, error) {
|
|
if reqLines < 1 {
|
|
return nil, 0, ErrNonPositiveLinesNumber
|
|
}
|
|
if len(delimiter) == 0 {
|
|
return nil, 0, errors.New("must provide a delimiter")
|
|
}
|
|
var (
|
|
size = r.Size()
|
|
tailStart int64
|
|
tailEnd = size
|
|
found int
|
|
)
|
|
|
|
if int64(len(delimiter)) >= size {
|
|
return bytes.NewReader(nil), 0, nil
|
|
}
|
|
|
|
scanner := newScanner(r, delimiter)
|
|
for scanner.Scan(ctx) {
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, 0, scanner.Err()
|
|
}
|
|
|
|
found++
|
|
if found == 1 {
|
|
tailEnd = scanner.End()
|
|
}
|
|
if found == reqLines {
|
|
break
|
|
}
|
|
}
|
|
|
|
tailStart = scanner.Start(ctx)
|
|
|
|
if found == 0 {
|
|
return bytes.NewReader(nil), 0, nil
|
|
}
|
|
|
|
if found < reqLines && tailStart != 0 {
|
|
tailStart = 0
|
|
}
|
|
return io.NewSectionReader(r, tailStart, tailEnd-tailStart), found, nil
|
|
}
|
|
|
|
func newScanner(r SizeReaderAt, delim []byte) *scanner {
|
|
size := r.Size()
|
|
readSize := blockSize
|
|
if readSize > int(size) {
|
|
readSize = int(size)
|
|
}
|
|
// silly case...
|
|
if len(delim) >= readSize/2 {
|
|
readSize = len(delim)*2 + 2
|
|
}
|
|
|
|
return &scanner{
|
|
r: r,
|
|
pos: size,
|
|
buf: make([]byte, readSize),
|
|
delim: delim,
|
|
}
|
|
}
|
|
|
|
type scanner struct {
|
|
r SizeReaderAt
|
|
pos int64
|
|
buf []byte
|
|
delim []byte
|
|
err error
|
|
idx int
|
|
}
|
|
|
|
func (s *scanner) Start(ctx context.Context) int64 {
|
|
if s.idx > 0 {
|
|
idx := bytes.LastIndex(s.buf[:s.idx], s.delim)
|
|
if idx >= 0 {
|
|
return s.pos + int64(idx) + int64(len(s.delim))
|
|
}
|
|
}
|
|
|
|
// slow path
|
|
buf := make([]byte, len(s.buf))
|
|
copy(buf, s.buf)
|
|
|
|
readAhead := &scanner{
|
|
r: s.r,
|
|
pos: s.pos,
|
|
delim: s.delim,
|
|
idx: s.idx,
|
|
buf: buf,
|
|
}
|
|
|
|
if !readAhead.Scan(ctx) {
|
|
return 0
|
|
}
|
|
return readAhead.End()
|
|
}
|
|
|
|
func (s *scanner) End() int64 {
|
|
return s.pos + int64(s.idx) + int64(len(s.delim))
|
|
}
|
|
|
|
func (s *scanner) Err() error {
|
|
return s.err
|
|
}
|
|
|
|
func (s *scanner) Scan(ctx context.Context) bool {
|
|
if s.err != nil {
|
|
return false
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
s.err = ctx.Err()
|
|
return false
|
|
default:
|
|
}
|
|
|
|
idx := s.idx - len(s.delim)
|
|
if idx < 0 {
|
|
readSize := int(s.pos)
|
|
if readSize > len(s.buf) {
|
|
readSize = len(s.buf)
|
|
}
|
|
|
|
if readSize < len(s.delim) {
|
|
return false
|
|
}
|
|
|
|
offset := s.pos - int64(readSize)
|
|
n, err := s.r.ReadAt(s.buf[:readSize], offset)
|
|
if err != nil && err != io.EOF {
|
|
s.err = err
|
|
return false
|
|
}
|
|
|
|
s.pos -= int64(n)
|
|
idx = n
|
|
}
|
|
|
|
s.idx = bytes.LastIndex(s.buf[:idx], s.delim)
|
|
if s.idx >= 0 {
|
|
return true
|
|
}
|
|
|
|
if len(s.delim) > 1 && s.pos > 0 {
|
|
// in this case, there may be a partial delimiter at the front of the buffer, so set the position forward
|
|
// up to the maximum size partial that could be there so it can be read again in the next iteration with any
|
|
// potential remainder.
|
|
// An example where delimiter is `####`:
|
|
// [##asdfqwerty]
|
|
// ^
|
|
// This resets the position to where the arrow is pointing.
|
|
// It could actually check if a partial exists and at the front, but that is pretty similar to the indexing
|
|
// code above though a bit more complex since each byte has to be checked (`len(delimiter)-1`) factorial).
|
|
// It's much simpler and cleaner to just re-read `len(delimiter)-1` bytes again.
|
|
s.pos += int64(len(s.delim)) - 1
|
|
}
|
|
}
|
|
}
|