github.com/containerd/Containerd@v1.4.13/content/proxy/content_reader.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package proxy
    18  
    19  import (
    20  	"context"
    21  
    22  	contentapi "github.com/containerd/containerd/api/services/content/v1"
    23  	digest "github.com/opencontainers/go-digest"
    24  )
    25  
    26  type remoteReaderAt struct {
    27  	ctx    context.Context
    28  	digest digest.Digest
    29  	size   int64
    30  	client contentapi.ContentClient
    31  }
    32  
    33  func (ra *remoteReaderAt) Size() int64 {
    34  	return ra.size
    35  }
    36  
    37  func (ra *remoteReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
    38  	rr := &contentapi.ReadContentRequest{
    39  		Digest: ra.digest,
    40  		Offset: off,
    41  		Size_:  int64(len(p)),
    42  	}
    43  	rc, err := ra.client.Read(ra.ctx, rr)
    44  	if err != nil {
    45  		return 0, err
    46  	}
    47  
    48  	for len(p) > 0 {
    49  		var resp *contentapi.ReadContentResponse
    50  		// fill our buffer up until we can fill p.
    51  		resp, err = rc.Recv()
    52  		if err != nil {
    53  			return n, err
    54  		}
    55  
    56  		copied := copy(p, resp.Data)
    57  		n += copied
    58  		p = p[copied:]
    59  	}
    60  	return n, nil
    61  }
    62  
    63  func (ra *remoteReaderAt) Close() error {
    64  	return nil
    65  }