Skip to content

Commit

Permalink
feat: support symlink file detect (#5)
Browse files Browse the repository at this point in the history
* feat: support symlink file detect
* fix: add point nil check
* fix: remove no use func
* fix: use setting error
  • Loading branch information
luohoufu authored Jan 10, 2025
1 parent b62994e commit d8dcbda
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
25 changes: 12 additions & 13 deletions lib/reader/harvester/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"fmt"
"io"
"os"
"strings"

"infini.sh/agent/lib/reader"
"infini.sh/agent/lib/reader/linenumber"
"infini.sh/agent/lib/reader/multiline"
"infini.sh/agent/lib/reader/readfile"
"infini.sh/agent/lib/reader/readfile/encoding"
"infini.sh/agent/lib/reader/readjson"
"infini.sh/framework/core/errors"
)

type Harvester struct {
Expand All @@ -30,8 +30,8 @@ type Harvester struct {

func NewHarvester(path string, offset int64) (*Harvester, error) {
f, err := readOpen(path)
if err != nil {
return nil, err
if f == nil || err != nil {
return nil, errors.Errorf("failed to open file(%s),%v", path, err)
}
_, err = f.Seek(offset, io.SeekStart)
if err != nil {
Expand All @@ -48,6 +48,9 @@ func NewHarvester(path string, offset int64) (*Harvester, error) {
}
h.encodingFactory = encodingFactory
h.encoding, err = h.encodingFactory(f)
if err != nil {
return nil, err
}
return h, nil
}

Expand All @@ -60,6 +63,9 @@ func readOpen(path string) (*os.File, error) {
func (h *Harvester) NewJsonFileReader(pattern string, showLineNumber bool) (reader.Reader, error) {
var r reader.Reader
var err error
if h == nil || h.file == nil {
return nil, fmt.Errorf("file is nil")
}

encReaderMaxBytes := h.config.MaxBytes * 4
r, err = readfile.NewEncodeReader(h.file, readfile.Config{
Expand Down Expand Up @@ -97,6 +103,9 @@ func (h *Harvester) NewLogFileReader(pattern string, showLineNumber bool) (reade
var r reader.Reader
var err error

if h == nil || h.file == nil {
return nil, fmt.Errorf("file is nil")
}
encReaderMaxBytes := h.config.MaxBytes * 4
r, err = readfile.NewEncodeReader(h.file, readfile.Config{
Codec: h.encoding,
Expand Down Expand Up @@ -125,16 +134,6 @@ func (h *Harvester) NewLogFileReader(pattern string, showLineNumber bool) (reade
return h.reader, nil
}

// NewPlainTextRead
// 返回一行内容,即使一条日志包含多行(如错误堆栈),也只返回一行。
func (h *Harvester) NewPlainTextRead(showLineNumber bool) (reader.Reader, error) {
if strings.HasSuffix(h.file.Name(), ".json") {
return h.NewJsonFileReader("", showLineNumber)
} else {
return h.NewLogFileReader("", showLineNumber)
}
}

func (h *Harvester) Close() error {
err := h.reader.Close()
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion lib/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package util
import (
"bufio"
"os"
"path/filepath"
)

func CountFileRows(filePath string) (int64, error){
func CountFileRows(filePath string) (int64, error) {
file, err := os.Open(filePath)
if err != nil {
return 0, err
Expand All @@ -23,3 +24,16 @@ func CountFileRows(filePath string) (int64, error){
}
return count, nil
}

// ResolveSymlink Parse the target file path of the soft link
func ResolveSymlink(link string) (string, error) {
realPath, err := filepath.EvalSymlinks(link)
if err != nil {
return "", err
}
absPath, err := filepath.Abs(realPath)
if err != nil {
return "", err
}
return absPath, nil
}
14 changes: 14 additions & 0 deletions plugin/logs/file_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"

log "github.com/cihub/seelog"
"infini.sh/agent/lib/util"
)

type Operation uint8
Expand Down Expand Up @@ -79,6 +80,19 @@ func (w *FileDetector) Detect(ctx context.Context) {
}

func (w *FileDetector) judgeEvent(ctx context.Context, path string, info os.FileInfo, pattern *Pattern) {
if info.Mode()&os.ModeSymlink != 0 {
realPath, err := util.ResolveSymlink(path)
if err != nil {
log.Error(err)
return
}
info, err = os.Lstat(realPath)
if err != nil {
log.Error(err)
return
}
path = realPath
}
preState, err := GetFileState(path)
isSameFile := w.IsSameFile(preState, info, path)
if err != nil || preState == (FileState{}) || !isSameFile {
Expand Down

0 comments on commit d8dcbda

Please sign in to comment.