Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support symlink file detect #5

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/reader/harvester/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"os"
"strings"

"infini.sh/agent/lib/reader"
"infini.sh/agent/lib/reader/linenumber"
Expand All @@ -30,7 +29,7 @@ type Harvester struct {

func NewHarvester(path string, offset int64) (*Harvester, error) {
f, err := readOpen(path)
if err != nil {
if f == nil || err != nil {
return nil, err
}
_, err = f.Seek(offset, io.SeekStart)
Expand All @@ -48,6 +47,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 +62,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 +102,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 +133,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
Loading