SubmitTargetData takes received blocks of target data and uses them to fill in missing chunks of the target file. Public for advanced use only: most callers should use FetchRemainingBlocks instead and let the Syncer fetch and submit the blocks. This is provided for callers that want to handle data d
(offset int64, in io.Reader)
| 179 | // callers that want to handle data downloading via another protocol or |
| 180 | // approach feed the syncer themselves. |
| 181 | func (zs *Syncer) SubmitTargetData(offset int64, in io.Reader) (int64, error) { |
| 182 | bytesReceived := int64(0) |
| 183 | |
| 184 | if offset%zs.blocksize != 0 { |
| 185 | panic(fmt.Sprintf("misaligned data block passed as target data (%d, blocksize %d)", offset, zs.blocksize)) |
| 186 | } |
| 187 | id := rcksum.BlockID(offset / zs.blocksize) |
| 188 | |
| 189 | buf := make([]byte, zs.blocksize) |
| 190 | var n int |
| 191 | var err error |
| 192 | for { |
| 193 | n, err = io.ReadFull(in, buf) |
| 194 | if err != nil { |
| 195 | break |
| 196 | } |
| 197 | bytesReceived += int64(n) |
| 198 | // err == nil implies a full buffer. |
| 199 | err = zs.rs.SubmitBlocks(buf, id, id) |
| 200 | if err != nil { |
| 201 | return bytesReceived, err |
| 202 | } |
| 203 | id++ |
| 204 | } |
| 205 | switch err { |
| 206 | case io.EOF: |
| 207 | return bytesReceived, nil |
| 208 | case io.ErrUnexpectedEOF: |
| 209 | if id == rcksum.BlockID(zs.blocks-1) { |
| 210 | // Short last block. rcksum expects a full block, padded with 0s, so pad and submit. |
| 211 | for i := range buf { |
| 212 | if i >= n { |
| 213 | buf[i] = 0 |
| 214 | } |
| 215 | } |
| 216 | err = zs.rs.SubmitBlocks(buf, id, id) |
| 217 | return bytesReceived, err |
| 218 | } // else fall through; any other incomplete block is an error. |
| 219 | default: |
| 220 | // Any other error is returned as-is |
| 221 | } |
| 222 | return bytesReceived, err |
| 223 | } |
| 224 | |
| 225 | // NewSeedSink returns a io.ReaderFrom that can be used to stream local seed |
| 226 | // local seed data to the Syncer. progressCallback, if provided, will be |
no test coverage detected