Skip to content

Commit 4a5cc7c

Browse files
committed
feat: support symlink file detect
1 parent 9557b34 commit 4a5cc7c

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

lib/reader/harvester/harvester.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"os"
11-
"strings"
1211

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

3130
func NewHarvester(path string, offset int64) (*Harvester, error) {
3231
f, err := readOpen(path)
33-
if err != nil {
32+
if f == nil || err != nil {
3433
return nil, err
3534
}
3635
_, err = f.Seek(offset, io.SeekStart)
@@ -48,6 +47,9 @@ func NewHarvester(path string, offset int64) (*Harvester, error) {
4847
}
4948
h.encodingFactory = encodingFactory
5049
h.encoding, err = h.encodingFactory(f)
50+
if err != nil {
51+
return nil, err
52+
}
5153
return h, nil
5254
}
5355

@@ -60,6 +62,9 @@ func readOpen(path string) (*os.File, error) {
6062
func (h *Harvester) NewJsonFileReader(pattern string, showLineNumber bool) (reader.Reader, error) {
6163
var r reader.Reader
6264
var err error
65+
if h.file == nil {
66+
return nil, fmt.Errorf("file is nil")
67+
}
6368

6469
encReaderMaxBytes := h.config.MaxBytes * 4
6570
r, err = readfile.NewEncodeReader(h.file, readfile.Config{
@@ -97,6 +102,9 @@ func (h *Harvester) NewLogFileReader(pattern string, showLineNumber bool) (reade
97102
var r reader.Reader
98103
var err error
99104

105+
if h.file == nil {
106+
return nil, fmt.Errorf("file is nil")
107+
}
100108
encReaderMaxBytes := h.config.MaxBytes * 4
101109
r, err = readfile.NewEncodeReader(h.file, readfile.Config{
102110
Codec: h.encoding,
@@ -125,16 +133,6 @@ func (h *Harvester) NewLogFileReader(pattern string, showLineNumber bool) (reade
125133
return h.reader, nil
126134
}
127135

128-
// NewPlainTextRead
129-
// 返回一行内容,即使一条日志包含多行(如错误堆栈),也只返回一行。
130-
func (h *Harvester) NewPlainTextRead(showLineNumber bool) (reader.Reader, error) {
131-
if strings.HasSuffix(h.file.Name(), ".json") {
132-
return h.NewJsonFileReader("", showLineNumber)
133-
} else {
134-
return h.NewLogFileReader("", showLineNumber)
135-
}
136-
}
137-
138136
func (h *Harvester) Close() error {
139137
err := h.reader.Close()
140138
if err != nil {

lib/util/file.go

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
// CountFileRows counts the number of rows in the specified file.
2+
// It takes a file path as an argument and returns the row count and an error if any.
3+
// If the file cannot be opened, it returns an error.
4+
//
5+
// Parameters:
6+
// - filePath: The path to the file to be read.
7+
//
8+
// Returns:
9+
// - int64: The number of rows in the file.
10+
// - error: An error if the file cannot be opened or read.
11+
//
12+
// Example usage:
13+
// count, err := CountFileRows("/path/to/file")
14+
// if err != nil {
15+
// log.Fatal(err)
16+
// }
17+
// fmt.Println("Number of rows:", count)
18+
19+
// ResolveSymlink resolves the target file path of a symbolic link.
20+
// It takes a symbolic link path as an argument and returns the absolute path of the target file and an error if any.
21+
// If the symbolic link cannot be resolved, it returns an error.
22+
//
23+
// Parameters:
24+
// - link: The path to the symbolic link.
25+
//
26+
// Returns:
27+
// - string: The absolute path of the target file.
28+
// - error: An error if the symbolic link cannot be resolved.
29+
//
30+
// Example usage:
31+
// realPath, err := ResolveSymlink("/path/to/symlink")
32+
// if err != nil {
33+
// log.Fatal(err)
34+
// }
35+
// fmt.Println("Resolved path:", realPath)
136
/* Copyright © INFINI Ltd. All rights reserved.
237
* Web: https://infinilabs.com
338
* Email: hello#infini.ltd */
@@ -7,9 +42,10 @@ package util
742
import (
843
"bufio"
944
"os"
45+
"path/filepath"
1046
)
1147

12-
func CountFileRows(filePath string) (int64, error){
48+
func CountFileRows(filePath string) (int64, error) {
1349
file, err := os.Open(filePath)
1450
if err != nil {
1551
return 0, err
@@ -23,3 +59,16 @@ func CountFileRows(filePath string) (int64, error){
2359
}
2460
return count, nil
2561
}
62+
63+
// ResolveSymlink Parse the target file path of the soft link
64+
func ResolveSymlink(link string) (string, error) {
65+
realPath, err := filepath.EvalSymlinks(link)
66+
if err != nil {
67+
return "", err
68+
}
69+
absPath, err := filepath.Abs(realPath)
70+
if err != nil {
71+
return "", err
72+
}
73+
return absPath, nil
74+
}

plugin/logs/file_detect.go

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111

1212
log "github.com/cihub/seelog"
13+
"infini.sh/agent/lib/util"
1314
)
1415

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

8182
func (w *FileDetector) judgeEvent(ctx context.Context, path string, info os.FileInfo, pattern *Pattern) {
83+
if info.Mode()&os.ModeSymlink != 0 {
84+
realPath, err := util.ResolveSymlink(path)
85+
if err != nil {
86+
log.Error(err)
87+
return
88+
}
89+
info, err = os.Lstat(realPath)
90+
if err != nil {
91+
log.Error(err)
92+
return
93+
}
94+
path = realPath
95+
}
8296
preState, err := GetFileState(path)
8397
isSameFile := w.IsSameFile(preState, info, path)
8498
if err != nil || preState == (FileState{}) || !isSameFile {

0 commit comments

Comments
 (0)