Skip to content

Commit

Permalink
Improve replacement from ioutil.ReadDir by io.ReadDir
Browse files Browse the repository at this point in the history
This commit improves the code necessary for replacing the deprecated
`ioutil.ReadDir` call by `io.ReadDir`. A single for loop is used to
iterate over the dirEntries and if there is an error reading the
FileInfo from a DirEntry, we log it and continue to the next
dirEntry. This effectively adds more resilience to the original code
that would not process any segment if the call to `ioutil.ReadDir` had
failed.
  • Loading branch information
belimawr committed May 17, 2024
1 parent cb92dcc commit 9cabaf0
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions libbeat/publisher/queue/diskqueue/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"sort"
Expand Down Expand Up @@ -170,22 +169,19 @@ func (s bySegmentID) Less(i, j int) bool { return s[i].id < s[j].id }
// Scan the given path for segment files, and return them in a list
// ordered by segment id.
func scanExistingSegments(logger *logp.Logger, pathStr string) ([]*queueSegment, error) {
segmentFiles, err := os.ReadDir(pathStr)
dirEntries, err := os.ReadDir(pathStr)
if err != nil {
return nil, fmt.Errorf("could not read queue directory '%s': %w", pathStr, err)
}
files := make([]fs.FileInfo, 0, len(segmentFiles))
for _, entry := range segmentFiles {
info, err := entry.Info()

segments := []*queueSegment{}
for _, dirEntry := range dirEntries {
file, err := dirEntry.Info()
if err != nil {
logger.Errorf("could not get info for file '%s': %w. Skipping it", info.Name(), err)
logger.Errorf("could not get info for file '%s', skipping. Error: %w", dirEntry.Name(), err)
continue
}
files = append(files, info)
}

segments := []*queueSegment{}
for _, file := range files {
components := strings.Split(file.Name(), ".")
if len(components) == 2 && strings.ToLower(components[1]) == "seg" {
// Parse the id as base-10 64-bit unsigned int. We ignore file names that
Expand Down

0 comments on commit 9cabaf0

Please sign in to comment.