MCPcopy
hub / github.com/safing/portmaster / parseFile

Function parseFile

service/intel/customlists/lists.go:59–146  ·  view source on GitHub ↗
(filePath string)

Source from the content-addressed store, hash-verified

57}
58
59func parseFile(filePath string) error {
60 // Reset all maps, previous (if any) settings will be lost.
61 for key := range countryCodesFilterList {
62 delete(countryCodesFilterList, key)
63 }
64 for key := range ipAddressesFilterList {
65 delete(ipAddressesFilterList, key)
66 }
67 for key := range autonomousSystemsFilterList {
68 delete(autonomousSystemsFilterList, key)
69 }
70 for key := range domainsFilterList {
71 delete(domainsFilterList, key)
72 }
73
74 // Ignore empty file path.
75 if filePath == "" {
76 return nil
77 }
78
79 // Open the file if possible
80 file, err := os.Open(filePath)
81 if err != nil {
82 log.Warningf("intel/customlists: failed to parse file %s", err)
83 module.states.Add(mgr.State{
84 ID: parseWarningNotificationID,
85 Name: "Failed to open custom filter list",
86 Message: err.Error(),
87 Type: mgr.StateTypeWarning,
88 })
89 return err
90 } else {
91 module.states.Remove(parseWarningNotificationID)
92 }
93 defer func() { _ = file.Close() }()
94
95 var allLinesCount uint64
96 var invalidLinesCount uint64
97
98 // Read filter file line by line.
99 scanner := bufio.NewScanner(file)
100 // The scanner will error out if the line is greater than 64K, in this case it is enough.
101 for scanner.Scan() {
102 allLinesCount++
103 // Parse and count invalid lines (comment, empty lines, zero IPs...)
104 if !parseLine(scanner.Text()) {
105 invalidLinesCount++
106 }
107 }
108
109 // Check for scanner error.
110 if err := scanner.Err(); err != nil {
111 return err
112 }
113
114 invalidLinesRation := float32(invalidLinesCount) / float32(allLinesCount)
115
116 if invalidLinesRation > rationForInvalidLinesUntilWarning {

Callers 1

checkAndUpdateFilterListFunction · 0.85

Calls 11

WarningfFunction · 0.92
WarningFunction · 0.92
InfofFunction · 0.92
NotifyInfoFunction · 0.92
parseLineFunction · 0.85
ErrMethod · 0.80
ErrorMethod · 0.65
CloseMethod · 0.65
TextMethod · 0.65
AddMethod · 0.45
RemoveMethod · 0.45

Tested by

no test coverage detected