Esempio n. 1
0
func (h *Harvester) validateFile(f *os.File) error {
	// Check we are not following a rabbit hole (symlinks, etc.)
	if !file.IsRegular(f) {
		return errors.New("Given file is not a regular file.")
	}

	info, err := f.Stat()
	if err != nil {
		logp.Err("Failed getting stats for file %s: %s", h.state.Source, err)
		return err
	}
	// Compares the stat of the opened file to the state given by the prospector. Abort if not match.
	if !os.SameFile(h.state.Fileinfo, info) {
		return errors.New("File info is not identical with opened file. Aborting harvesting and retrying file later again.")
	}

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

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

	// get file offset. Only update offset if no error
	offset, err := h.initFileOffset(f)
	if err != nil {
		return err
	}

	logp.Debug("harvester", "Setting offset for file: %s. Offset: %d ", h.state.Source, offset)
	h.state.Offset = offset

	return nil
}
Esempio n. 2
0
File: log.go Progetto: yhyang1/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 encoding encoding.Encoding

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

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

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

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

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

	// yay, open file
	h.file = source.File{f}
	return encoding, nil
}