Example #1
0
func (lg *logger) next() ([]byte, bool, error) {
	if lg.pos+_OF_DATA >= lg.fileSize {
		return nil, false, nil
	}

	tmp := make([]byte, 4)
	_, err := lg.file.ReadAt(tmp, lg.pos)
	if err != nil {
		return nil, false, err
	}

	size := int64(utils.ParseUint32(tmp))
	if lg.pos+size+_OF_DATA > lg.fileSize {
		return nil, false, nil // bad tail
	}

	log := make([]byte, _OF_DATA+size)
	_, err = lg.file.ReadAt(log, lg.pos)
	if err != nil {
		return nil, false, err
	}

	checksum1 := calChecksum(0, log[_OF_DATA:])
	checksum2 := utils.ParseUint32(log[_OF_CHECKSUM:])
	if checksum1 != checksum2 {
		return nil, false, nil // bad tail
	}

	lg.pos += int64(len(log))

	return log, true, nil
}
Example #2
0
func (lg *logger) init() error {
	info, err := lg.file.Stat()
	if err != nil {
		return err
	}
	fileSize := info.Size()
	if fileSize < 4 {
		return ErrBadLogFile
	}

	raw := make([]byte, 4)
	_, err = lg.file.ReadAt(raw, 0)
	if err != nil {
		return err
	}
	xChecksum := utils.ParseUint32(raw)

	lg.fileSize = fileSize
	lg.xChecksum = xChecksum

	return lg.checkAndRemoveTail()
}