Skip to content

Commit

Permalink
walkpaths: fix messageParser not closing files
Browse files Browse the repository at this point in the history
Using defer to close the file in general was a smart move, but I did not
consider that all the parsing happens within the same function, thus
leaving all the defer statements to after all the files have been
opened. Create an emlParser in analogue to the mboxParser, opening each
file in an individual function call to make use of defer. Make the
messageParser logic a bit cleaner in the process.

Reported-by: Federico Ferri <federico.ferri@cern.ch>
Fixes: 7e0f2bb
Closes: #14
  • Loading branch information
ferdinandyb committed Jan 24, 2025
1 parent bd42ff8 commit 7c72d9f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions parseMail.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,38 @@ func mboxParser(path string, headers chan<- *mail.Header) error {
return nil
}

func emlParser(path string, headers chan<- *mail.Header) error {
f, err := os.Open(path)
defer f.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
r, err := mail.CreateReader(f)
if err != nil {
return err
}
h := &mail.Header{Header: r.Header.Header}
headers <- h
return nil
}

func messageParser(
paths chan string,
headers chan<- *mail.Header,
) {
for path := range paths {
f, err := os.Open(path)
defer f.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
r, err := mail.CreateReader(f)
err := emlParser(path, headers)
if err != nil {
mboxerr := mboxParser(path, headers)
if mboxerr == nil {
continue
}
if utf8.ValidString(err.Error()) {
// do nothing
} else if utf8.ValidString(err.Error()) {
fmt.Fprintln(os.Stderr, path, err)
} else {
fmt.Fprintln(os.Stderr, path, "mail reader error, probably tried reading binary")
}
continue
}
h := &mail.Header{Header: r.Header.Header}
headers <- h
}
}

Expand Down

0 comments on commit 7c72d9f

Please sign in to comment.