Exemplo n.º 1
0
Arquivo: index.go Projeto: jbrukh/ggit
func parseIndexEntry(r *bufio.Reader) (entry *IndexEntry, err error) {
	var binEntry indexEntry
	err = binary.Read(r, ord, &binEntry)
	if err != nil {
		return nil, err
	}

	// TODO: what if it is corrupted and too long?
	name, e := r.ReadBytes(token.NUL)
	if e != nil {
		return nil, e
	}
	name = util.TrimLastByte(name) // get rid of NUL

	// don't ask me how I figured this out after
	// a 14 hour workday
	leftOver := 7 - (len(name)+6)%8
	for j := 0; j < leftOver; j++ {
		// TODO: read the bytes at once somehow
		if _, err = r.ReadByte(); err != nil {
			return nil, err
		}
	}

	// record the entry
	return toIndexEntry(&binEntry, string(name)), nil
}
Exemplo n.º 2
0
func writeTranslatedFile(context *FileContext, outFile *os.File) error {

	var sourceFile *os.File
	var sourceReader *bufio.Reader
	var line string
	var rawLine []byte
	var err error

	sourceFile, err = os.Open(context.fullPath)
	if err != nil {
		return err
	}
	defer sourceFile.Close()

	sourceReader = bufio.NewReader(sourceFile)

	for {
		rawLine, err = sourceReader.ReadBytes('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}

		line = context.TranslateLine(string(rawLine))
		outFile.Write([]byte(line))
	}
	return nil
}
Exemplo n.º 3
0
func Unpack(reader *bufio.Reader) ([]byte, error) {

	return reader.ReadBytes('\n')
	//if global.PackSplitType =="breakline" {

	//	return reader.ReadBytes('\n')
	//}

	lengthByte, _ := reader.Peek(4)
	lengthBuff := bytes.NewBuffer(lengthByte)
	var length int32
	err := binary.Read(lengthBuff, binary.LittleEndian, &length)
	if err != nil {
		return nil, err
	}
	fmt.Println("Get length :", length)
	if int32(reader.Buffered()) < length+4 {
		return nil, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		return nil, err
	}
	return pack[4:], nil
}
Exemplo n.º 4
0
func (p Delimiter) Process(r *bufio.Reader) ([]byte, error) {
	b, err := r.ReadBytes(p.Delim)
	if err != nil {
		return nil, err
	}
	return bytes.TrimSpace(b), nil
}
Exemplo n.º 5
0
func readSimpleStr(r *bufio.Reader) (Resp, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return Resp{}, err
	}
	return Resp{typ: SimpleStr, val: b[1 : len(b)-2]}, nil
}
Exemplo n.º 6
0
func readNetString(r *bufio.Reader) ([]byte, error) {

	lenBytes, err := r.ReadBytes(':')
	if err != nil {
		return []byte{}, err
	}
	i, err := strconv.ParseInt(string(lenBytes[:len(lenBytes)-1]), 10, 0)
	dataLen := int(i) // 0 on error, safe to check this first
	if dataLen > MAXLEN {
		err = fmt.Errorf("Proposed Length > MAXLEN")
		return []byte{}, err
	}
	if err != nil {
		err = fmt.Errorf("Error Parsing Length %#v", lenBytes)
		return []byte{}, err
	}

	data = data[:dataLen]
	_, err = io.ReadFull(r, data)
	if err != nil {
		err = fmt.Errorf("Error reading data")
		return []byte{}, err
	}
	b, err := r.ReadByte()
	if err != nil || b != ',' {
		err = fmt.Errorf("Missing terminator")
		return []byte{}, err
	}

	return data, nil
}
Exemplo n.º 7
0
func socksReadBytesUntil(r *bufio.Reader, end byte) ([]byte, error) {
	val, err := r.ReadBytes(end)
	if err != nil {
		return nil, err
	}
	return val, nil
}
Exemplo n.º 8
0
func readPacket(br *bufio.Reader) ([]byte, error) {
	// Drop bytes preceding HEADER_BYTE
	_, err := br.ReadBytes(HEADER_BYTE)
	if err != nil {
		return nil, err
	}

	// Get packet length field
	packet := make([]byte, 4)
	read_bytes := 0
	for read_bytes < 4 {
		tmp := make([]byte, 4)
		nread, err := br.Read(tmp)
		if err != nil {
			return nil, err
		}
		copy(packet[read_bytes:], tmp[:nread])
		read_bytes += nread
	}
	pktlen := int(binary.BigEndian.Uint32(packet))

	// Get rest of packet
	packet = make([]byte, pktlen)
	read_bytes = 0
	for read_bytes < pktlen {
		tmp := make([]byte, pktlen)
		nread, err := br.Read(tmp)
		if err != nil {
			return nil, err
		}
		copy(packet[read_bytes:], tmp[:nread])
		read_bytes += nread
	}
	return packet, nil
}
Exemplo n.º 9
0
func decodeIndex(rbuf *bufio.Reader, f *File) error {
	c, err := rbuf.Peek(len(blockIndex))
	if err != nil {
		if err == io.EOF {
			return nil
		}
		fmt.Printf("index-peek failed: %v\n", err)
		return err
	}
	if !bytes.Equal(c, blockIndex) {
		return nil
	}
	var ybuf []byte
	for {
		bline, err := rbuf.ReadBytes('\n')
		if err != nil {
			return err
		}
		ybuf = append(ybuf, bline...)
		if bytes.HasPrefix(bline, []byte("...")) {
			break
		}
	}
	err = yaml.Unmarshal(ybuf, &f.Index)
	return err
}
Exemplo n.º 10
0
func parse(in *bufio.Reader) []Item {
	// Create the slice of items to be returned.  (This slice will be extended as necessary.)
	items := make([]Item, 0)

	// Parse each of the rules one by one.
	for {
		c, e := in.ReadByte()
		if e != nil || c == '}' {
			break
		}
		if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
			continue
		}
		sel, e := in.ReadBytes('{')
		sel = bytes.Join([][]byte{[]byte{c}, sel}, []byte{})
		sel = sel[:len(sel)-1]
		sel = bytes.TrimSpace(sel)
		var t Item
		if c == '@' {
			t = parseQuery(sel[1:], in)
		} else {
			t = parseRule(sel, in)
		}
		if t != nil {
			items = append(items, t)
		}
	}

	return items
}
Exemplo n.º 11
0
// returns err == io.EOF when we hit EOF without any further data
func (self *GraphiteMetric) Read(reader *bufio.Reader) error {
	buf, err := reader.ReadBytes('\n')
	str := strings.TrimSpace(string(buf))
	if err != nil {
		if err != io.EOF {
			return fmt.Errorf("connection closed uncleanly/broken: %s\n", err.Error())
		}
		if str == "" {
			return err
		}
		// else we got EOF but also data, so just try to process it as valid data
	}
	elements := strings.Fields(str)
	if len(elements) != 3 {
		return fmt.Errorf("Received '%s' which doesn't have three fields", str)
	}
	self.name = elements[0]
	self.floatValue, err = strconv.ParseFloat(elements[1], 64)
	if err != nil {
		return err
	}
	if i := int64(self.floatValue); float64(i) == self.floatValue {
		self.isInt = true
		self.integerValue = int64(self.floatValue)
	}
	timestamp, err := strconv.ParseUint(elements[2], 10, 32)
	if err != nil {
		return err
	}
	self.timestamp = int64(timestamp * 1000000)
	return nil
}
Exemplo n.º 12
0
func main() {
	app := cli.NewApp()
	app.Name = "bbq"
	app.Usage = "Tool to help you BBQ"
	app.Commands = []cli.Command{
		{
			Name:      "item",
			ShortName: "i",
			Usage:     "items needed for bbq",
			Action: func(c *cli.Context) {
				file, err := os.Open("items.txt")
				if err != nil {
					log.Fatal(err)
				}
				var reader *bufio.Reader
				var line []byte
				reader = bufio.NewReader(file)
				for {
					if err == io.EOF {
						return
					}
					line, err = reader.ReadBytes('\n')
					print(string(line))
				}
			},
		},
	}

	app.Run(os.Args)
}
Exemplo n.º 13
0
func recvResponse(r *bufio.Reader) (resp [][]byte, err error) {
	var s, arg []byte
	var argc int
	if s, err = r.ReadBytes(lfSign); err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return nil, err
	}

	switch s[0] {
	case intSign:
		resp = append(resp, s[:len(s)-2])
	case okSign, errSign:
		resp = append(resp, s[:len(s)-2])
	case multiBulkSign:
		// read argc
		if argc, err = strconv.Atoi(string(s[1 : len(s)-2])); err != nil {
			return nil, err
		}
		// read argv
		for i := 0; i < argc; i++ {
			if arg, err = readArgv(r); err != nil {
				return nil, err
			}
			resp = append(resp, arg)
		}
	default:
		fmt.Printf("%s", s)
		return nil, fmt.Errorf("Protocol Error: unexpected '%c'", s[0])
	}

	return resp, nil
}
Exemplo n.º 14
0
func readArgv(r *bufio.Reader) (arg []byte, err error) {
	var s []byte
	if s, err = r.ReadBytes(lfSign); err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return nil, err
	}

	if s[0] == bulkSign {
		// read len
		var arglen, readlen int = 0, 0
		if arglen, err = strconv.Atoi(string(s[1 : len(s)-2])); err != nil {
			return nil, err
		}
		arg = make([]byte, arglen+2) // make sure to read "\r\n"
		if _, err := io.ReadFull(r, arg[readlen:]); err != nil {
			return nil, err
		}
		return arg[:len(arg)-2], nil
	} else if s[0] == intSign {
		return s[1 : len(s)-2], nil
	}

	return nil, errors.New("Protocol Error: expected $ or : but got " + string(s))
}
Exemplo n.º 15
0
// Parse
func (c *Config) Parse(r *bufio.Reader) error {
	var section string = "defaults"
	for {
		l, err := r.ReadBytes('\n')
		if err == io.EOF {
			break
		} else if err != nil {
			return err
		}
		switch {
		case PatternEmpty.Match(l):
			continue
		case PatternComment.Match(l):
			continue
		case PatternSection.Match(l):
			m := PatternSection.FindSubmatch(l)
			section = string(bytes.TrimSpace(m[1]))
		case PatternOption.Match(l):
			m := PatternOption.FindSubmatch(l)
			c.Set(section, string(bytes.TrimSpace(m[1])), string(bytes.TrimSpace(m[2])))
		default:
			return &ConfigSyntaxError{string(l)}
		}
	}
	return nil
}
Exemplo n.º 16
0
// readAndPublish reads to the delim from r and publishes the bytes
// to the map of producers.
func readAndPublish(r *bufio.Reader, delim byte, producers map[string]*nsq.Producer, lookupProducer *nsq.TopicProducerMgr) error {
	line, readErr := r.ReadBytes(delim)

	if len(line) > 0 {
		// trim the delimiter
		line = line[:len(line)-1]
	}

	if len(line) == 0 {
		return readErr
	}

	if lookupProducer != nil {
		err := lookupProducer.Publish(*topic, line)
		if err != nil {
			return err
		}
	} else {
		for _, producer := range producers {
			err := producer.Publish(*topic, line)
			if err != nil {
				return err
			}
		}
	}

	return readErr
}
Exemplo n.º 17
0
func (rb *ReaderBase) readToCrLf(r *bufio.Reader) (b []byte, err error) {
	b, err = r.ReadBytes('\n')
	if len(b) < 2 {
		return nil, errors.New("Unexpected response")
	}
	return b[0 : len(b)-2], nil
}
Exemplo n.º 18
0
func listenPort(cReq chan<- []byte, epollFd int, reader *bufio.Reader) {
	events := make([]syscall.EpollEvent, 32)

	var buf []byte
	var err error

	for {
		if _, err := syscall.EpollWait(epollFd, events, -1); err != nil {
			log.Fatalln("Error receiving epoll events:", err)
		}

		buf, err = reader.ReadBytes('\x0a')
		switch err {
		case nil:
		case io.EOF:
			time.Sleep(time.Second * 1)
			continue
		default:
			log.Fatalln(err)
		}

		break
	}

	cReq <- buf
}
Exemplo n.º 19
0
func (self *GraphiteMetric) Read(reader *bufio.Reader) error {
	buf, err := reader.ReadBytes('\n')
	str := strings.TrimSpace(string(buf))
	if err != nil {
		if err != io.EOF {
			return fmt.Errorf("GraphiteServer: connection closed uncleanly/broken: %s\n", err.Error())
		}
		if len(str) > 0 {
			return fmt.Errorf("GraphiteServer: incomplete read, line read: '%s'. neglecting line because connection closed because of %s\n", str, err.Error())
		}
		return err
	}
	elements := strings.Split(str, " ")
	if len(elements) != 3 {
		return fmt.Errorf("Received '%s' which doesn't have three fields", str)
	}
	self.name = elements[0]
	self.floatValue, err = strconv.ParseFloat(elements[1], 64)
	if err != nil {
		return err
	}
	if i := int64(self.floatValue); float64(i) == self.floatValue {
		self.isInt = true
		self.integerValue = int64(self.floatValue)
	}
	timestamp, err := strconv.ParseUint(elements[2], 10, 32)
	if err != nil {
		return err
	}
	self.timestamp = int64(timestamp * 1000000)
	return nil
}
Exemplo n.º 20
0
func readError(r *bufio.Reader) (*Message, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return nil, err
	}
	return &Message{Type: Err, val: b[1 : len(b)-2], raw: b}, nil
}
Exemplo n.º 21
0
func parseCmdSize(rd *bufio.Reader, prefix uint8) (int, error) {
	cmdBuf := make([]byte, 8)
	for {
		cmd, err := rd.ReadBytes('\n')
		if err != nil {
			Log.Printf("rd.ReadBytes('\n') failed (%s)", err.Error())
			return 0, err
		}

		cmdBuf = append(cmdBuf, cmd...)
		if len(cmd) > 0 && cmd[len(cmd)-1] == '\r' {
			break
		}
	}

	cmd := string(cmdBuf)
	cmdLen := len(cmd)
	if cmdLen <= 3 || cmd[0] != prefix {
		Log.Printf("tcp protocol cmd: %s number format error", cmd)
		return 0, CmdFmtErr
	}

	// skip the \r
	cmdSize, err := strconv.Atoi(cmd[1 : cmdLen-1])
	if err != nil {
		Log.Printf("tcp protocol cmd: %s number parse int failed (%s)", cmd, err.Error())
		return 0, CmdFmtErr
	}

	return cmdSize, nil
}
Exemplo n.º 22
0
func readTrimmedLine(r *bufio.Reader) ([]byte, error) {
	if line, err := r.ReadBytes('\n'); err != nil {
		return nil, err
	} else {
		return bytes.TrimRight(line, "\r\n"), nil
	}
}
Exemplo n.º 23
0
func newMessageReader(bufr *bufio.Reader) (*messageReader, error) {
	if _, err := bufr.ReadBytes('"'); err != nil {
		return nil, err
	}
	msg, err := bufr.ReadBytes('"')
	if err != nil {
		return nil, err
	}
	for {
		b, err := bufr.Peek(1)
		if err != nil {
			return nil, err
		}
		if b[0] == ',' {
			bufr.ReadByte()
			break
		}
		if b[0] != ' ' {
			break
		}
		bufr.ReadByte()
	}
	return &messageReader{
		reader:    bufr,
		message:   string(msg[:len(msg)-1]),
		firstRead: true,
	}, nil
}
Exemplo n.º 24
0
// parses Simple string response like +OK\r\n
func readSimpleString(reader *bufio.Reader) (*Reply, error) {
	raw_response, err := reader.ReadBytes('\n')
	if err != nil {
		return nil, err
	}
	return &Reply{Response: raw_response[0 : len(raw_response)-2], Rtype: RString}, nil
}
Exemplo n.º 25
0
func readInteger(reader *bufio.Reader) (*Reply, error) {
	raw_response, err := reader.ReadBytes('\n')
	if err != nil {
		return nil, err
	}
	return &Reply{Response: raw_response[0 : len(raw_response)-2], Rtype: RInt}, nil
}
Exemplo n.º 26
0
// reades errors like -ERR not working :D\r\n
func readError(reader *bufio.Reader) (*Reply, error) {
	raw_response, err := reader.ReadBytes('\n')
	if err != nil {
		return nil, err
	}
	return nil, errors.New(string(raw_response[0 : len(raw_response)-2]))
}
Exemplo n.º 27
0
func readline(reader *bufio.Reader, buffer *bytes.Buffer) (line string, size int, err error) {
	var (
		segment []byte
	)

	for {
		if segment, err = reader.ReadBytes('\n'); err != nil {
			if err != io.EOF {
				log.Errorf("read line failed: %s", err)
			}
			return
		}

		if _, err = buffer.Write(segment); err != nil {
			log.Errorf("write buffer failed: %s", err)
			return
		}

		if isPartialLine(segment) {
			time.Sleep(1 * time.Second)
		} else {
			size = buffer.Len()
			line = buffer.String()
			buffer.Reset()
			line = strings.TrimRight(line, "\r\n")
			return
		}
	}

	return
}
Exemplo n.º 28
0
func readBulkStr(r *bufio.Reader) (Resp, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return Resp{}, err
	}
	size, err := strconv.ParseInt(string(b[1:len(b)-2]), 10, 64)
	if err != nil {
		return Resp{}, errParse
	}
	if size < 0 {
		return Resp{typ: Nil}, nil
	}
	total := make([]byte, size)
	b2 := total
	var n int
	for len(b2) > 0 {
		n, err = r.Read(b2)
		if err != nil {
			return Resp{}, err
		}
		b2 = b2[n:]
	}

	// There's a hanging \r\n there, gotta read past it
	trail := make([]byte, 2)
	for i := 0; i < 2; i++ {
		c, err := r.ReadByte()
		if err != nil {
			return Resp{}, err
		}
		trail[i] = c
	}

	return Resp{typ: BulkStr, val: total}, nil
}
Exemplo n.º 29
0
Arquivo: auth.go Projeto: 40a/bootkube
// authReadLine reads a line and separates it into its fields.
func authReadLine(in *bufio.Reader) ([][]byte, error) {
	data, err := in.ReadBytes('\n')
	if err != nil {
		return nil, err
	}
	data = bytes.TrimSuffix(data, []byte("\r\n"))
	return bytes.Split(data, []byte{' '}), nil
}
Exemplo n.º 30
0
func readError(r *bufio.Reader) (Resp, error) {
	b, err := r.ReadBytes(delimEnd)
	if err != nil {
		return Resp{}, err
	}
	err = errors.New(string(b[1 : len(b)-2]))
	return Resp{typ: AppErr, val: err, Err: err}, nil
}