github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/internal/ioutil/io.go (about)

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package ioutil
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"io"
    22  	"reflect"
    23  	"strconv"
    24  
    25  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    26  	"oras.land/oras-go/v2/content"
    27  	"oras.land/oras-go/v2/internal/spec"
    28  )
    29  
    30  // CloserFunc is the basic Close method defined in io.Closer.
    31  type CloserFunc func() error
    32  
    33  // Close performs close operation by the CloserFunc.
    34  func (fn CloserFunc) Close() error {
    35  	return fn()
    36  }
    37  
    38  // CopyBuffer copies from src to dst through the provided buffer
    39  // until either EOF is reached on src, or an error occurs.
    40  // The copied content is verified against the size and the digest.
    41  func CopyBuffer(dst io.Writer, src io.Reader, buf []byte, desc ocispec.Descriptor) error {
    42  	// verify while copying
    43  	ingestSize, _ := strconv.ParseInt(desc.Annotations[spec.AnnotationResumeOffset], 10, 64)
    44  	vr := content.NewVerifyReader(src, desc)
    45  	if n, err := io.CopyBuffer(dst, vr, buf); err != nil {
    46  		if errors.Is(err, io.ErrUnexpectedEOF) {
    47  			// Check if amount read == partial size
    48  			if n != (desc.Size - ingestSize) {
    49  				// sumthin's up
    50  				return fmt.Errorf("copy failed: %w", err)
    51  			}
    52  		}
    53  	}
    54  	return vr.Verify()
    55  }
    56  
    57  // Types returned by `io.NopCloser()`.
    58  var (
    59  	nopCloserType         = reflect.TypeOf(io.NopCloser(nil))
    60  	nopCloserWriterToType = reflect.TypeOf(io.NopCloser(struct {
    61  		io.Reader
    62  		io.WriterTo
    63  	}{}))
    64  )
    65  
    66  // UnwrapNopCloser unwraps the reader wrapped by `io.NopCloser()`.
    67  // Similar implementation can be found in the built-in package `net/http`.
    68  // Reference: https://github.com/golang/go/blob/go1.22.1/src/net/http/transfer.go#L1090-L1105
    69  func UnwrapNopCloser(r io.Reader) io.Reader {
    70  	switch reflect.TypeOf(r) {
    71  	case nopCloserType, nopCloserWriterToType:
    72  		return reflect.ValueOf(r).Field(0).Interface().(io.Reader)
    73  	default:
    74  		return r
    75  	}
    76  }