| 10 | var removeFile = os.Remove |
| 11 | |
| 12 | func 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 | |
| 40 | func stdinSpoolFailureError(tmpPath string, cause error, cleanupErr error) error { |
| 41 | if cleanupErr == nil { |