Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions oci/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ func (o *OrasRemote) FetchLayer(ctx context.Context, desc ocispec.Descriptor) (b
return content.FetchAll(ctx, src, desc)
}

// FetchLayerReader fetches the layer with the given descriptor from the remote repository.
func (o *OrasRemote) FetchLayerReader(ctx context.Context, desc ocispec.Descriptor) (*content.VerifyReader, error) {
var src oras.ReadOnlyTarget
src = o.repo
if o.cache != nil {
src = orasCache.New(o.repo, o.cache)
}
r, err := src.Fetch(ctx, desc)
if err != nil {
return nil, err
}
return content.NewVerifyReader(r, desc), nil
}

// FetchJSONFile fetches the given JSON file from the remote repository.
func FetchJSONFile[T any](ctx context.Context, fetcher func(ctx context.Context, desc ocispec.Descriptor) (bytes []byte, err error), manifest *Manifest, path string) (result T, err error) {
descriptor := manifest.Locate(path)
Expand Down
16 changes: 14 additions & 2 deletions oci/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package oci
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -86,7 +88,7 @@ func (o *OrasRemote) CopyToTarget(ctx context.Context, layers []ocispec.Descript

// PullPath pulls a layer from the remote repository and saves it to `destinationDir/annotationTitle`.
func (o *OrasRemote) PullPath(ctx context.Context, destinationDir string, desc ocispec.Descriptor) error {
b, err := o.FetchLayer(ctx, desc)
vr, err := o.FetchLayerReader(ctx, desc)
if err != nil {
return err
}
Expand All @@ -102,7 +104,17 @@ func (o *OrasRemote) PullPath(ctx context.Context, destinationDir string, desc o
return err
}

return os.WriteFile(fullPath, b, helpers.ReadWriteUser)
file, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, helpers.ReadWriteUser)
if err != nil {
return err
}
defer file.Close()

if _, err := io.Copy(file, vr); err != nil {
return fmt.Errorf("read failed: %w", err)
}

return vr.Verify()
}

// PullPaths pulls multiple files from the remote repository and saves them to `destinationDir`.
Expand Down
Loading