MCPcopy Create free account
hub / github.com/bufbuild/buf / NewFile

Function NewFile

private/pkg/tmp/tmp.go:45–72  ·  view source on GitHub ↗

NewFile returns a new temporary file with the data copied from the Reader. It must be closed when done. This deletes this file. This file will be automatically closed on context cancellation. Usage of this function requires eng approval - ask before using.

(ctx context.Context, reader io.Reader)

Source from the content-addressed store, hash-verified

43//
44// Usage of this function requires eng approval - ask before using.
45func NewFile(ctx context.Context, reader io.Reader) (File, error) {
46 id, err := uuidutil.New()
47 if err != nil {
48 return nil, err
49 }
50 file, err := os.CreateTemp("", id.String())
51 if err != nil {
52 return nil, err
53 }
54 path := file.Name()
55 // just in case
56 absPath, err := filepath.Abs(filepath.Clean(path))
57 if err != nil {
58 return nil, err
59 }
60 closer := func() error { return os.Remove(absPath) }
61 go func() {
62 <-ctx.Done()
63 _ = closer()
64 }()
65 _, err = io.Copy(file, reader)
66 err = errors.Join(err, file.Close())
67 if err != nil {
68 err = errors.Join(err, closer())
69 return nil, err
70 }
71 return newFile(closerFunc(closer), absPath), nil
72}
73
74// NewDir returns a new temporary directory.
75//

Callers 3

HandleMethod · 0.92
TestFileFunction · 0.85
TestFileCancelFunction · 0.85

Calls 6

NewFunction · 0.92
closerFuncFuncType · 0.85
newFileFunction · 0.70
StringMethod · 0.65
NameMethod · 0.65
CloseMethod · 0.65

Tested by 2

TestFileFunction · 0.68
TestFileCancelFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…