Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
refactor(input/file): resolve file paths once
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
Dominik Rosiek committed Jun 16, 2021
1 parent 4e3cb21 commit e2fcca1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion operator/builtin/input/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (f *InputOperator) newReader(file *os.File, fp *Fingerprint, firstCheck boo
if err != nil {
return nil, err
}
newReader.Path = file.Name()
newReader.SetPath(file.Name())
return newReader, nil
}

Expand Down
87 changes: 51 additions & 36 deletions operator/builtin/input/file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,47 @@ import (
"golang.org/x/text/encoding"
"golang.org/x/text/transform"

"github.com/open-telemetry/opentelemetry-log-collection/entry"
"github.com/open-telemetry/opentelemetry-log-collection/errors"
)

// File attributes contains information about file paths
type fileAttributes struct {
Name string
Path string
ResolvedName string
ResolvedPath string
}

// resolveFileAttributes resolves file attributes
// and sets it to empty string in case of error
func resolveFileAttributes(path string) *fileAttributes {
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
resolved = ""
}

abs, err := filepath.Abs(resolved)
if err != nil {
abs = ""
}

return &fileAttributes{
Path: path,
Name: filepath.Base(path),
ResolvedPath: abs,
ResolvedName: filepath.Base(abs),
}
}

// Reader manages a single file
type Reader struct {
Fingerprint *Fingerprint
Offset int64
Path string

generation int
fileInput *InputOperator
file *os.File
generation int
fileInput *InputOperator
file *os.File
fileAttributes *fileAttributes

decoder *encoding.Decoder
decodeBuffer []byte
Expand All @@ -49,20 +77,25 @@ type Reader struct {
// NewReader creates a new file reader
func (f *InputOperator) NewReader(path string, file *os.File, fp *Fingerprint) (*Reader, error) {
r := &Reader{
Fingerprint: fp,
file: file,
Path: path,
fileInput: f,
SugaredLogger: f.SugaredLogger.With("path", path),
decoder: f.encoding.Encoding.NewDecoder(),
decodeBuffer: make([]byte, 1<<12),
Fingerprint: fp,
file: file,
fileInput: f,
SugaredLogger: f.SugaredLogger.With("path", path),
decoder: f.encoding.Encoding.NewDecoder(),
decodeBuffer: make([]byte, 1<<12),
fileAttributes: resolveFileAttributes(path),
}
return r, nil
}

// SetPath sets fileAttributes based on new path
func (r *Reader) SetPath(path string) {
r.fileAttributes = resolveFileAttributes(path)
}

// Copy creates a deep copy of a Reader
func (f *Reader) Copy(file *os.File) (*Reader, error) {
reader, err := f.fileInput.NewReader(f.Path, file, f.Fingerprint.Copy())
reader, err := f.fileInput.NewReader(f.fileAttributes.Path, file, f.Fingerprint.Copy())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,40 +178,22 @@ func (f *Reader) emit(ctx context.Context, msgBuf []byte) error {
return fmt.Errorf("create entry: %s", err)
}

if err := e.Set(f.fileInput.FilePathField, f.Path); err != nil {
return err
}
if err := e.Set(f.fileInput.FileNameField, filepath.Base(f.Path)); err != nil {
return err
}
if err := f.resolvePath(e); err != nil {
return err
}

f.fileInput.Write(ctx, e)
return nil
}

// resolvePath resolves path to real and absolute form and add them into entry
func (f *Reader) resolvePath(e *entry.Entry) error {
resolved, err := filepath.EvalSymlinks(f.Path)
if err != nil {
if err := e.Set(f.fileInput.FilePathField, f.fileAttributes.Path); err != nil {
return err
}

abs, err := filepath.Abs(resolved)
if err != nil {
if err := e.Set(f.fileInput.FileNameField, f.fileAttributes.Name); err != nil {
return err
}

if err := e.Set(f.fileInput.FilePathResolvedField, abs); err != nil {
if err := e.Set(f.fileInput.FilePathResolvedField, f.fileAttributes.ResolvedPath); err != nil {
return err
}

if err := e.Set(f.fileInput.FileNameResolvedField, filepath.Base(abs)); err != nil {
if err := e.Set(f.fileInput.FileNameResolvedField, f.fileAttributes.ResolvedName); err != nil {
return err
}

f.fileInput.Write(ctx, e)
return nil
}

Expand Down

0 comments on commit e2fcca1

Please sign in to comment.