| 29 | } |
| 30 | |
| 31 | func downloadToStdout(dbx filesClient, src string, w io.Writer) error { |
| 32 | ignoreBrokenPipeSignal() |
| 33 | |
| 34 | arg := files.NewDownloadArg(src) |
| 35 | var bytesWritten int64 |
| 36 | |
| 37 | err := retryWithBackoff(func() error { |
| 38 | if bytesWritten > 0 { |
| 39 | return partialStdoutError(bytesWritten) |
| 40 | } |
| 41 | |
| 42 | _, contents, err := dbx.DownloadContext(currentContext(), arg) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | defer func() { _ = contents.Close() }() |
| 47 | |
| 48 | n, copyErr := io.Copy(stdoutBrokenPipeWriter{w: w}, contents) |
| 49 | bytesWritten += n |
| 50 | |
| 51 | if errors.Is(copyErr, errStdoutBrokenPipe) { |
| 52 | return nil |
| 53 | } |
| 54 | if copyErr != nil && bytesWritten > 0 { |
| 55 | return partialStdoutError(bytesWritten) |
| 56 | } |
| 57 | return copyErr |
| 58 | }) |
| 59 | |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | func partialStdoutError(bytesWritten int64) error { |
| 64 | return partialTransferError{bytesWritten: bytesWritten} |