| 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | ) |
| 7 | |
| 8 | type CopyCallback func(totalSize int64, readSoFar int64, readSinceLast int) error |
| 9 | |
| 10 | type callbackState struct { |
| 11 | callback CopyCallback |
| 12 | totalSize int64 |
| 13 | readSize int64 |
| 14 | } |
| 15 | |
| 16 | func (cs *callbackState) doCallback(readSinceLast int) error { |
| 17 | if cs.callback == nil { |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | return cs.callback(cs.totalSize, cs.readSize, readSinceLast) |
| 22 | } |
| 23 | |
| 24 | func (cs *callbackState) doCallbackIfRequired(n int, err error) error { |
| 25 | if cs.callback == nil || (err != nil && err != io.EOF) { |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | if err == io.EOF && cs.readSize != cs.totalSize { |
| 30 | // If the total size was initially unknown or incorrect, |
| 31 | // make a final callback with the total amount of data read |
| 32 | // as the total size. |
| 33 | cs.totalSize = cs.readSize |
| 34 | } else if n == 0 { |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | return cs.doCallback(n) |
| 39 | } |
| 40 | |
| 41 | func (cs *callbackState) read(r io.Reader, p []byte) (int, error) { |
| 42 | n, err := r.Read(p) |
| 43 | |
| 44 | cs.readSize += int64(n) |
| 45 | |
| 46 | callbackErr := cs.doCallbackIfRequired(n, err) |
| 47 | |
| 48 | // We only perform a callback if Read() returned no error or EOF, |
| 49 | // so if the callback returned an error, we can return it to the |
| 50 | // caller without masking a non-repeatable error condition. |
| 51 | if callbackErr != nil { |
| 52 | err = callbackErr |
| 53 | } |
| 54 | |
| 55 | return n, err |
| 56 | } |
| 57 | |
| 58 | type BodyWithCallback struct { |
| 59 | state callbackState |
| 60 | ReadSeekCloser |
| 61 | } |
| 62 | |
| 63 | func NewFileBodyWithCallback(f *os.File, totalSize int64, cb CopyCallback) *BodyWithCallback { |
| 64 | return NewBodyWithCallback(newNopClosingFile(f), totalSize, cb) |
| 65 | } |
| 66 | |
| 67 | func NewBodyWithCallback(body ReadSeekCloser, totalSize int64, cb CopyCallback) *BodyWithCallback { |
| 68 | return &BodyWithCallback{ |
| 69 | state: callbackState{ |
| 70 | callback: cb, |
| 71 | totalSize: totalSize, |
| 72 | }, |
| 73 | ReadSeekCloser: body, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Read wraps the underlying Reader's "Read" method. It also captures the number |
| 78 | // of bytes read, and calls the callback. |
| 79 | func (r *BodyWithCallback) Read(p []byte) (int, error) { |
| 80 | return r.state.read(r.ReadSeekCloser, p) |
| 81 | } |
| 82 | |
| 83 | // Seek wraps the underlying Seeker's "Seek" method, updating the number of |
| 84 | // bytes that have been consumed by this reader. |
| 85 | func (r *BodyWithCallback) Seek(offset int64, whence int) (int64, error) { |
| 86 | switch whence { |
| 87 | case io.SeekStart: |
| 88 | r.state.readSize = offset |
| 89 | case io.SeekCurrent: |
| 90 | r.state.readSize += offset |
| 91 | case io.SeekEnd: |
| 92 | r.state.readSize = r.state.totalSize + offset |
| 93 | } |
| 94 | |
| 95 | return r.ReadSeekCloser.Seek(offset, whence) |
| 96 | } |
| 97 | |
| 98 | // ResetProgress calls the callback with a negative read size equal to the |
| 99 | // total number of bytes read so far, effectively "resetting" the progress. |
| 100 | func (r *BodyWithCallback) ResetProgress() error { |
| 101 | return r.state.doCallback(-int(r.state.readSize)) |
| 102 | } |
| 103 | |
| 104 | type CallbackReader struct { |
| 105 | state callbackState |
| 106 | io.Reader |
| 107 | } |
| 108 | |
| 109 | func (r *CallbackReader) Read(p []byte) (int, error) { |
| 110 | return r.state.read(r.Reader, p) |
| 111 | } |
| 112 | |
| 113 | func NewCallbackReader(r io.Reader, totalSize int64, cb CopyCallback) *CallbackReader { |
| 114 | return &CallbackReader{ |
| 115 | state: callbackState{ |
| 116 | callback: cb, |
| 117 | totalSize: totalSize, |
| 118 | }, |
| 119 | Reader: r, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // prevent import cycle |
| 124 | type ReadSeekCloser interface { |
| 125 | io.Seeker |
| 126 | io.ReadCloser |
| 127 | } |
| 128 | |
| 129 | func newNopClosingFile(f *os.File) ReadSeekCloser { |
| 130 | return &nopClosingFile{File: f} |
| 131 | } |
| 132 | |
| 133 | type nopClosingFile struct { |
| 134 | *os.File |
| 135 | } |
| 136 | |
| 137 | func (r *nopClosingFile) Close() error { |
| 138 | return nil |
| 139 | } |