MCPcopy
hub / github.com/crowdsecurity/crowdsec / readFile

Method readFile

pkg/acquisition/modules/file/run.go:369–434  ·  view source on GitHub ↗
(ctx context.Context, filename string, out chan pipeline.Event)

Source from the content-addressed store, hash-verified

367}
368
369func (s *Source) readFile(ctx context.Context, filename string, out chan pipeline.Event) error {
370 var scanner *bufio.Scanner
371
372 logger := s.logger.WithField("oneshot", filename)
373
374 fd, err := os.Open(filename)
375 if err != nil {
376 return fmt.Errorf("failed opening %s: %w", filename, err)
377 }
378
379 defer fd.Close()
380
381 if strings.HasSuffix(filename, ".gz") {
382 gz, err := gzip.NewReader(fd)
383 if err != nil {
384 logger.Errorf("Failed to read gz file: %s", err)
385 return fmt.Errorf("failed to read gz %s: %w", filename, err)
386 }
387
388 defer gz.Close()
389
390 scanner = bufio.NewScanner(gz)
391 } else {
392 scanner = bufio.NewScanner(fd)
393 }
394
395 scanner.Split(bufio.ScanLines)
396
397 if s.config.MaxBufferSize > 0 {
398 buf := make([]byte, 0, 64*1024)
399 scanner.Buffer(buf, s.config.MaxBufferSize)
400 }
401
402 for scanner.Scan() {
403 select {
404 case <-ctx.Done():
405 logger.Info("File datasource stopping")
406 return nil
407 default:
408 if scanner.Text() == "" {
409 continue
410 }
411
412 l := pipeline.Line{
413 Raw: scanner.Text(),
414 Time: time.Now().UTC(),
415 Src: filename,
416 Labels: s.config.Labels,
417 Process: true,
418 Module: s.GetName(),
419 }
420 logger.Debugf("line %s", l.Raw)
421 metrics.FileDatasourceLinesRead.With(prometheus.Labels{"source": filename, "datasource_type": ModuleName, "acquis_type": l.Labels["type"]}).Inc()
422
423 // we're reading logs at once, it must be time-machine buckets
424 out <- pipeline.Event{Line: l, Process: true, Type: pipeline.LOG, ExpectMode: pipeline.TIMEMACHINE, Unmarshaled: make(map[string]any)}
425 }
426 }

Callers 1

OneShotMethod · 0.95

Calls 10

GetNameMethod · 0.95
OpenMethod · 0.80
NewReaderMethod · 0.80
TextMethod · 0.80
IncMethod · 0.80
ErrMethod · 0.80
CloseMethod · 0.45
ScanMethod · 0.45
InfoMethod · 0.45
WithMethod · 0.45

Tested by

no test coverage detected