blob: 77981c512d6f093fbe04d3b0d9e92a97dda5f24a [file] [log] [blame]
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !go1.24
package mcp
import (
"errors"
"os"
"path/filepath"
)
// withFile calls f on the file at join(dir, rel).
// It does not protect against path traversal attacks.
func withFile(dir, rel string, f func(*os.File) error) (err error) {
file, err := os.Open(filepath.Join(dir, rel))
if err != nil {
return err
}
// Record error, in case f writes.
defer func() { err = errors.Join(err, file.Close()) }()
return f(file)
}