Merge branch 'fix-missing-close-on-csv' into 'master'

Add missing close when reading CSV file

See merge request nvidia/container-toolkit/container-toolkit!128
This commit is contained in:
Evan Lezar 2022-04-08 08:33:23 +00:00
commit 1296a0ecf4
2 changed files with 33 additions and 11 deletions

View File

@ -68,7 +68,7 @@ func NewFromCSVFiles(logger *logrus.Logger, files []string, root string) (Discov
// MountSpecType.
func NewFromCSVFile(logger *logrus.Logger, locators map[csv.MountSpecType]lookup.Locator, filename string) (Discover, error) {
// Create a discoverer for each file-kind combination
targets, err := csv.ParseFile(logger, filename)
targets, err := csv.NewCSVFileParser(logger, filename).Parse()
if err != nil {
return nil, fmt.Errorf("failed to parse CSV file: %v", err)
}

View File

@ -81,17 +81,39 @@ func BaseFilesOnly(filenames []string) []string {
return selected
}
// ParseFile parses the specified file and returns a list of required jetson mounts
func ParseFile(logger *logrus.Logger, filename string) ([]*MountSpec, error) {
csvFile, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open %v for reading: %v", filename, err)
}
return parseCSVFromReader(logger, csvFile), nil
// Parser specifies an interface for parsing MountSpecs
type Parser interface {
Parse() ([]*MountSpec, error)
}
// parseCSVFromReader parses the specified file and returns a list of required jetson mounts
func parseCSVFromReader(logger *logrus.Logger, reader io.Reader) []*MountSpec {
type csv struct {
logger *logrus.Logger
filename string
}
// NewCSVFileParser creates a new parser for reading MountSpecs from the specified CSV file
func NewCSVFileParser(logger *logrus.Logger, filename string) Parser {
p := csv{
logger: logger,
filename: filename,
}
return &p
}
// Parse parses the csv file and returns a list of MountSpecs in the file
func (p csv) Parse() ([]*MountSpec, error) {
reader, err := os.Open(p.filename)
if err != nil {
return nil, fmt.Errorf("failed to open %v for reading: %v", p.filename, err)
}
defer reader.Close()
return p.parseFromReader(reader), nil
}
// parseFromReader parses the specified file and returns a list of required jetson mounts
func (p csv) parseFromReader(reader io.Reader) []*MountSpec {
var targets []*MountSpec
scanner := bufio.NewScanner(reader)
@ -99,7 +121,7 @@ func parseCSVFromReader(logger *logrus.Logger, reader io.Reader) []*MountSpec {
line := scanner.Text()
target, err := NewMountSpecFromLine(line)
if err != nil {
logger.Debugf("Skipping invalid mount spec '%v': %v", line, err)
p.logger.Debugf("Skipping invalid mount spec '%v': %v", line, err)
continue
}
targets = append(targets, target)