MCPcopy Create free account
hub / github.com/dropbox/dbxcli / spoolStdinToTemp

Function spoolStdinToTemp

cmd/stdin.go:12–38  ·  view source on GitHub ↗
(r io.Reader)

Source from the content-addressed store, hash-verified

10var removeFile = os.Remove
11
12func spoolStdinToTemp(r io.Reader) (path string, size int64, cleanup func() error, err error) {
13 f, err := os.CreateTemp("", "dbxcli-stdin-*")
14 if err != nil {
15 return "", 0, nil, fmt.Errorf("create temp file: %w", err)
16 }
17 if err := os.Chmod(f.Name(), 0600); err != nil {
18 _ = f.Close()
19 _ = os.Remove(f.Name())
20 return "", 0, nil, fmt.Errorf("chmod temp file: %w", err)
21 }
22
23 tmpPath := f.Name()
24 removeTmp := func() error { return removeFile(tmpPath) }
25
26 n, copyErr := io.Copy(f, r)
27 if closeErr := f.Close(); closeErr != nil {
28 if copyErr != nil {
29 return "", 0, nil, stdinSpoolFailureError(tmpPath, copyErr, removeTmp())
30 }
31 return "", 0, nil, stdinSpoolFailureError(tmpPath, closeErr, removeTmp())
32 }
33 if copyErr != nil {
34 return "", 0, nil, stdinSpoolFailureError(tmpPath, copyErr, removeTmp())
35 }
36
37 return tmpPath, n, removeTmp, nil
38}
39
40func stdinSpoolFailureError(tmpPath string, cause error, cleanupErr error) error {
41 if cleanupErr == nil {

Calls 3

stdinSpoolFailureErrorFunction · 0.85
CloseMethod · 0.80
CopyMethod · 0.80