Esempio n. 1
0
File: log.go Progetto: tsg/beats
func (h *Harvester) openFile() (encoding.Encoding, error) {
	var file *os.File
	var err error
	var encoding encoding.Encoding

	// TODO: This is currently end endless retry, should be set to a max?
	// retry on failure.
	for {
		file, err = input.ReadOpen(h.Path)
		if err == nil {
			// Check we are not following a rabbit hole (symlinks, etc.)
			if !input.IsRegularFile(file) {
				file.Close()
				return nil, errors.New("Given file is not a regular file.")
			}

			encoding, err = h.encoding(file)
			if err == nil {
				break
			}

			file.Close()
			if err != transform.ErrShortSrc {
				return nil, err
			}
			logp.Info("Initialising encoding for '%v' failed due to file being to short")
		}

		logp.Err("Failed opening %s: %s", h.Path, err)
		time.Sleep(5 * time.Second)
	}

	// update file offset
	err = h.initFileOffset(file)
	if err != nil {
		file.Close()
		return nil, err
	}

	// yay, open file
	h.file = fileSource{file}
	return encoding, nil
}
Esempio n. 2
0
File: log.go Progetto: amundi/beats
// openFile opens a file and checks for the encoding. In case the encoding cannot be detected
// or the file cannot be opened because for example of failing read permissions, an error
// is returned and the harvester is closed. The file will be picked up again the next time
// the file system is scanned
func (h *Harvester) openFile() (encoding.Encoding, error) {
	var file *os.File
	var err error
	var encoding encoding.Encoding

	file, err = input.ReadOpen(h.Path)
	if err == nil {
		// Check we are not following a rabbit hole (symlinks, etc.)
		if !input.IsRegularFile(file) {
			return nil, errors.New("Given file is not a regular file.")
		}

		encoding, err = h.encoding(file)
		if err != nil {

			if err == transform.ErrShortSrc {
				logp.Info("Initialising encoding for '%v' failed due to file being too short", file)
			} else {
				logp.Err("Initialising encoding for '%v' failed: %v", file, err)
			}
			return nil, err
		}

	} else {
		logp.Err("Failed opening %s: %s", h.Path, err)
		return nil, err
	}

	// update file offset
	err = h.initFileOffset(file)
	if err != nil {
		return nil, err
	}

	// yay, open file
	h.file = fileSource{file}
	return encoding, nil
}