Files
yq/pkg/yqlib/utils.go
T
cb7b707457 Close input files after processing each one (#2796) (#2808)
readStream opened an *os.File but returned a *bufio.Reader, so the
*os.File type switch in streamEvaluator.EvaluateFiles never matched and
no input file was ever closed. allAtOnceEvaluator.EvaluateFiles did not
attempt to close them at all. Descriptors were only released at process
exit or by the os.File finaliser, so a multi-file run could exhaust the
file descriptor limit.

readStream now returns an explicit cleanup function alongside the reader,
which both evaluators call once the file has been processed.

Co-authored-by: Pablo Garcia <pablito@MacBook-Neo-de-Pablo.local>
Co-authored-by: Mike Farah <mikefarah@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-19 14:17:53 +10:00

91 lines
2.5 KiB
Go

package yqlib
import (
"bufio"
"container/list"
"errors"
"fmt"
"io"
"os"
)
// filenameAliases maps real file paths to display names.
// Used by front matter handling to preserve original filenames
// when the actual content is read from temporary files.
var filenameAliases = map[string]string{}
// SetFilenameAlias registers a display name for a file path so that
// the filename operator returns the original name instead of a temp path.
func SetFilenameAlias(realPath string, displayName string) {
filenameAliases[realPath] = displayName
}
// ClearFilenameAliases removes all filename aliases.
func ClearFilenameAliases() {
filenameAliases = map[string]string{}
}
func resolveFilename(filename string) string {
if alias, ok := filenameAliases[filename]; ok {
return alias
}
return filename
}
// readStream returns a reader for the given file, along with a cleanup function
// that must be called once the reader is no longer needed. The cleanup is a no-op
// for stdin.
func readStream(filename string) (io.Reader, func(), error) {
if filename == "-" {
return bufio.NewReader(os.Stdin), func() {}, nil
}
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
// and ensuring that it's not possible to give a path to a file outside that directory.
file, err := os.Open(filename) // #nosec
if err != nil {
return nil, nil, err
}
return bufio.NewReader(file), func() { safelyCloseFile(file) }, nil
}
func writeString(writer io.Writer, txt string) error {
_, errorWriting := io.WriteString(writer, txt)
return errorWriting
}
func ReadDocuments(reader io.Reader, decoder Decoder) (*list.List, error) {
return readDocuments(reader, "", 0, decoder)
}
func readDocuments(reader io.Reader, filename string, fileIndex int, decoder Decoder) (*list.List, error) {
filename = resolveFilename(filename)
err := decoder.Init(reader)
if err != nil {
return nil, err
}
inputList := list.New()
var currentIndex uint
for {
candidateNode, errorReading := decoder.Decode()
if errors.Is(errorReading, io.EOF) {
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader)
}
return inputList, nil
} else if errorReading != nil {
return nil, fmt.Errorf("bad file '%v': %w", filename, errorReading)
}
candidateNode.document = currentIndex
candidateNode.filename = filename
candidateNode.fileIndex = fileIndex
candidateNode.EvaluateTogether = true
inputList.PushBack(candidateNode)
currentIndex = currentIndex + 1
}
}