Example #1
0
// open does open the file given under h.Path and assigns the file handler to h.file
func (h *Harvester) open() error {
	// Special handling that "-" means to read from standard input
	if h.Path == "-" {
		h.file = os.Stdin
		return nil
	}

	for {
		var err error
		h.file, err = input.ReadOpen(h.Path)

		if err != nil {
			// TODO: This is currently end endless retry, should be set to a max?
			// retry on failure.
			logp.Err("Failed opening %s: %s", h.Path, err)
			time.Sleep(5 * time.Second)
		} else {
			break
		}
	}

	file := &input.File{
		File: h.file,
	}

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

	h.setFileOffset()

	return nil
}
Example #2
0
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
}