func (actx *ApplyContext) lazilyPatchFile(sctx *wsync.Context, targetContainer *tlc.Container, targetPool wsync.Pool, outputContainer *tlc.Container, outputPool wsync.WritablePool, fileIndex int64, onSourceWrite counter.CountCallback, ops chan wsync.Operation, inplace bool) (written int64, transposition *Transposition, err error) { var writer io.WriteCloser defer func() { if writer != nil { cErr := writer.Close() if cErr != nil && err == nil { err = cErr } } }() var realops chan wsync.Operation errs := make(chan error) first := true for op := range ops { if first { first = false // if the first operation is a blockrange that copies an // entire file from target into a file from source that has // the same name and size, then it's a no-op! if inplace && op.Type == wsync.OpBlockRange && op.BlockIndex == 0 { outputFile := outputContainer.Files[fileIndex] targetFile := targetContainer.Files[op.FileIndex] numOutputBlocks := ComputeNumBlocks(outputFile.Size) if op.BlockSpan == numOutputBlocks && outputFile.Size == targetFile.Size { transposition = &Transposition{ TargetPath: targetFile.Path, OutputPath: outputFile.Path, } } } if transposition != nil { go func() { errs <- nil }() } else { realops = make(chan wsync.Operation) writer, err = outputPool.GetWriter(fileIndex) if err != nil { errs <- errors.Wrap(err, 1) } else { writeCounter := counter.NewWriterCallback(onSourceWrite, writer) go func() { applyErr := sctx.ApplyPatch(writeCounter, targetPool, realops) if applyErr != nil { errs <- applyErr return } written = writeCounter.Count() errs <- nil }() } } } // if not a transposition, relay errors if transposition == nil { select { case cErr := <-errs: // if we get an error here, ApplyPatch failed so we no longer need to close realops if cErr != nil { return 0, nil, errors.Wrap(cErr, 1) } case realops <- op: // muffin } } } if transposition == nil { close(realops) } err = <-errs if err != nil { return 0, nil, errors.Wrap(err, 1) } return }