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
|
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"github.com/colinmarc/hdfs/v2"
)
const tailSearchSize int64 = 16384
func cat(paths []string) {
expanded, client, err := getClientAndExpandedPaths(paths)
if err != nil {
fatal(err)
}
readers := make([]io.Reader, 0, len(expanded))
for _, p := range expanded {
file, err := client.Open(p)
if err != nil {
fatal(err)
} else if file.Stat().IsDir() {
fatal(&os.PathError{"cat", p, errors.New("file is a directory")})
}
readers = append(readers, file)
}
_, err = io.Copy(os.Stdout, io.MultiReader(readers...))
if err != nil {
fatal(err)
}
}
func printSection(paths []string, numLines, numBytes int64, fromEnd bool) {
if numLines != -1 && numBytes != -1 {
fatal("You can't specify both -n and -c.")
} else if numLines == -1 && numBytes == -1 {
numLines = 10
}
expanded, client, err := getClientAndExpandedPaths(paths)
if err != nil {
fatal(err)
}
for _, p := range expanded {
file, err := client.Open(p)
if err != nil || file.Stat().IsDir() {
if err == nil && file.Stat().IsDir() {
err = &os.PathError{"open", p, errors.New("file is a directory")}
}
fmt.Fprintln(os.Stderr, err)
status = 1
continue
}
if len(expanded) > 1 {
fmt.Fprintf(os.Stderr, "%s:\n", file.Name())
}
if numLines != -1 {
if fromEnd {
tailLines(file, numLines)
} else {
headLines(file, numLines)
}
} else {
var offset int64
if fromEnd {
offset = file.Stat().Size() - numBytes
}
reader := io.NewSectionReader(file, offset, numBytes)
io.Copy(os.Stdout, reader)
}
}
}
func headLines(file *hdfs.FileReader, numLines int64) {
reader := bufio.NewReader(file)
var newlines, offset int64
for newlines < numLines {
b, err := reader.ReadByte()
if err == io.EOF {
offset = -1
break
} else if err != nil {
fatal(err)
}
if b == '\n' {
newlines++
}
offset++
}
_, err := file.Seek(0, 0)
if err != nil {
fatal(err)
}
if offset < 0 {
io.Copy(os.Stdout, file)
} else {
io.CopyN(os.Stdout, file, offset)
}
}
func tailLines(file *hdfs.FileReader, numLines int64) {
fileSize := file.Stat().Size()
searchPoint := file.Stat().Size() - tailSearchSize
if searchPoint < 0 {
searchPoint = 0
}
readSize := tailSearchSize
if readSize > fileSize {
readSize = fileSize
}
var printOffset int64
for searchPoint >= 0 {
section := bufio.NewReader(io.NewSectionReader(file, searchPoint, readSize))
off := searchPoint
newlines := make([]int64, 0, tailSearchSize/64)
b, err := section.ReadByte()
for err == nil {
if b == '\n' && (off+1 != fileSize) {
newlines = append(newlines, off)
}
off++
b, err = section.ReadByte()
}
if err != nil && err != io.EOF {
fatal(err)
}
foundNewlines := int64(len(newlines))
if foundNewlines >= numLines {
printOffset = newlines[foundNewlines-numLines] + 1
break
}
numLines -= foundNewlines
searchPoint -= tailSearchSize
}
_, err := file.Seek(printOffset, 0)
if err != nil {
fatal(err)
}
io.Copy(os.Stdout, file)
}
|