Skip to content

Commit 4527162

Browse files
ruflintsg
authored andcommitted
Improve handling of different path variants on Windows (elastic#3781)
* Improve handling of different path variants on Windows If forward slashes were used on Windows the glob was not matching on startup which lead to the issue that data was resent. This is solved in 5.3 by using path.Abs for all paths which also includes Clean. To make sure Path and Glob are always clean, Cleanup was added to the MatchFile part. This makes sure in case old data / incorrect data is loaded, the comparison will still work. More tests were added for windows to verify change. Before this change, option 1 below did not work. ``` "F:/wwwLogs/flights-wsapi/WebServicesWebApi.log" "F:\\wwwLogs\\flights-wsapi\\WebServicesWebApi.log" 'F:\wwwLogs\flights-wsapi\WebServicesWebApi.log' ``` Based on https://discuss.elastic.co/t/duplicate-events-with-filebeat-on-windows-on-service-restart/78743/10 * Move clean path outside for loop
1 parent 40556dd commit 4527162

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

CHANGELOG.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
8484
- Update regular expressions used for matching file names or lines (multiline, include/exclude functionality) to new matchers improving performance of simple string matches. {pull}3469[3469]
8585
- The `symlinks` and `harverster_limit` settings are now GA, instead of experimental. {pull}3525[3525]
8686
- close_timeout is also applied when the output is blocking. {pull}3511[3511]
87+
- Improve handling of different path variants on Windows. {pull}3781[3781]
8788

8889
*Heartbeat*
8990

filebeat/prospector/prospector_log.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"runtime"
8-
"strings"
97
"time"
108

119
"github.com/elastic/beats/filebeat/harvester"
@@ -180,13 +178,14 @@ func (p *ProspectorLog) getFiles() map[string]os.FileInfo {
180178

181179
// matchesFile returns true in case the given filePath is part of this prospector, means matches its glob patterns
182180
func (p *ProspectorLog) matchesFile(filePath string) bool {
181+
182+
// Path is cleaned to ensure we always compare clean paths
183+
filePath = filepath.Clean(filePath)
184+
183185
for _, glob := range p.config.Paths {
184186

185-
if runtime.GOOS == "windows" {
186-
// Windows allows / slashes which makes glob patterns with / work
187-
// But for match we need paths with \ as only file names are compared and no lookup happens
188-
glob = strings.Replace(glob, "/", "\\", -1)
189-
}
187+
// Glob is cleaned to ensure we always compare clean paths
188+
glob = filepath.Clean(glob)
190189

191190
// Evaluate if glob matches filePath
192191
match, err := filepath.Match(glob, filePath)

filebeat/prospector/prospector_log_windows_test.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,38 @@ var matchTestsWindows = []struct {
1616
result bool
1717
}{
1818
{
19-
"C:\\\\hello\\test\\test.log", // Path are always in windows format
20-
[]string{"C:\\\\hello/test/*.log"}, // Globs can also be with forward slashes
19+
`C:\\hello\test\test.log`,
20+
[]string{`C:\\hello/test/*.log`},
2121
nil,
2222
true,
2323
},
2424
{
25-
"C:\\\\hello\\test\\test.log", // Path are always in windows format
26-
[]string{"C:\\\\hello\\test/*.log"}, // Globs can also be mixed
25+
`C:\\hello\test\test.log`,
26+
[]string{`C:\\hello\test/*.log`},
27+
nil,
28+
true,
29+
},
30+
{
31+
`C:\\hello\test\test.log`,
32+
[]string{`C://hello/test/*.log`},
33+
nil,
34+
true,
35+
},
36+
{
37+
`C:\\hello\test\test.log`,
38+
[]string{`C://hello//test//*.log`},
39+
nil,
40+
true,
41+
},
42+
{
43+
`C://hello/test/test.log`,
44+
[]string{`C:\\hello\test\*.log`},
45+
nil,
46+
true,
47+
},
48+
{
49+
`C://hello/test/test.log`,
50+
[]string{`C:/hello/test/*.log`},
2751
nil,
2852
true,
2953
},

0 commit comments

Comments
 (0)