vendor/github.com/containerd/containerd/snapshot.go
5bd902b5
 package containerd
7acea2a2
 
 import (
 	"context"
 	"io"
 
c2cb302d
 	snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
7acea2a2
 	"github.com/containerd/containerd/api/types"
 	"github.com/containerd/containerd/errdefs"
 	"github.com/containerd/containerd/mount"
c2cb302d
 	"github.com/containerd/containerd/snapshots"
7acea2a2
 	protobuftypes "github.com/gogo/protobuf/types"
 )
 
 // NewSnapshotterFromClient returns a new Snapshotter which communicates
 // over a GRPC connection.
c2cb302d
 func NewSnapshotterFromClient(client snapshotsapi.SnapshotsClient, snapshotterName string) snapshots.Snapshotter {
7acea2a2
 	return &remoteSnapshotter{
 		client:          client,
 		snapshotterName: snapshotterName,
 	}
 }
 
 type remoteSnapshotter struct {
c2cb302d
 	client          snapshotsapi.SnapshotsClient
7acea2a2
 	snapshotterName string
 }
 
c2cb302d
 func (r *remoteSnapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) {
7acea2a2
 	resp, err := r.client.Stat(ctx,
c2cb302d
 		&snapshotsapi.StatSnapshotRequest{
7acea2a2
 			Snapshotter: r.snapshotterName,
 			Key:         key,
 		})
 	if err != nil {
c2cb302d
 		return snapshots.Info{}, errdefs.FromGRPC(err)
7acea2a2
 	}
 	return toInfo(resp.Info), nil
 }
 
c2cb302d
 func (r *remoteSnapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
7acea2a2
 	resp, err := r.client.Update(ctx,
c2cb302d
 		&snapshotsapi.UpdateSnapshotRequest{
7acea2a2
 			Snapshotter: r.snapshotterName,
 			Info:        fromInfo(info),
 			UpdateMask: &protobuftypes.FieldMask{
 				Paths: fieldpaths,
 			},
 		})
 	if err != nil {
c2cb302d
 		return snapshots.Info{}, errdefs.FromGRPC(err)
7acea2a2
 	}
 	return toInfo(resp.Info), nil
 }
 
c2cb302d
 func (r *remoteSnapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) {
 	resp, err := r.client.Usage(ctx, &snapshotsapi.UsageRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Key:         key,
 	})
 	if err != nil {
c2cb302d
 		return snapshots.Usage{}, errdefs.FromGRPC(err)
7acea2a2
 	}
 	return toUsage(resp), nil
 }
 
 func (r *remoteSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
c2cb302d
 	resp, err := r.client.Mounts(ctx, &snapshotsapi.MountsRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Key:         key,
 	})
 	if err != nil {
 		return nil, errdefs.FromGRPC(err)
 	}
 	return toMounts(resp.Mounts), nil
 }
 
c2cb302d
 func (r *remoteSnapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
 	var local snapshots.Info
7acea2a2
 	for _, opt := range opts {
 		if err := opt(&local); err != nil {
 			return nil, err
 		}
 	}
c2cb302d
 	resp, err := r.client.Prepare(ctx, &snapshotsapi.PrepareSnapshotRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Key:         key,
 		Parent:      parent,
 		Labels:      local.Labels,
 	})
 	if err != nil {
 		return nil, errdefs.FromGRPC(err)
 	}
 	return toMounts(resp.Mounts), nil
 }
 
c2cb302d
 func (r *remoteSnapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
 	var local snapshots.Info
7acea2a2
 	for _, opt := range opts {
 		if err := opt(&local); err != nil {
 			return nil, err
 		}
 	}
c2cb302d
 	resp, err := r.client.View(ctx, &snapshotsapi.ViewSnapshotRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Key:         key,
 		Parent:      parent,
 		Labels:      local.Labels,
 	})
 	if err != nil {
 		return nil, errdefs.FromGRPC(err)
 	}
 	return toMounts(resp.Mounts), nil
 }
 
c2cb302d
 func (r *remoteSnapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
 	var local snapshots.Info
7acea2a2
 	for _, opt := range opts {
 		if err := opt(&local); err != nil {
 			return err
 		}
 	}
c2cb302d
 	_, err := r.client.Commit(ctx, &snapshotsapi.CommitSnapshotRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Name:        name,
 		Key:         key,
 		Labels:      local.Labels,
 	})
 	return errdefs.FromGRPC(err)
 }
 
 func (r *remoteSnapshotter) Remove(ctx context.Context, key string) error {
c2cb302d
 	_, err := r.client.Remove(ctx, &snapshotsapi.RemoveSnapshotRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 		Key:         key,
 	})
 	return errdefs.FromGRPC(err)
 }
 
c2cb302d
 func (r *remoteSnapshotter) Walk(ctx context.Context, fn func(context.Context, snapshots.Info) error) error {
 	sc, err := r.client.List(ctx, &snapshotsapi.ListSnapshotsRequest{
7acea2a2
 		Snapshotter: r.snapshotterName,
 	})
 	if err != nil {
 		return errdefs.FromGRPC(err)
 	}
 	for {
 		resp, err := sc.Recv()
 		if err != nil {
 			if err == io.EOF {
 				return nil
 			}
 			return errdefs.FromGRPC(err)
 		}
 		if resp == nil {
 			return nil
 		}
 		for _, info := range resp.Info {
 			if err := fn(ctx, toInfo(info)); err != nil {
 				return err
 			}
 		}
 	}
 }
 
5bd902b5
 func (r *remoteSnapshotter) Close() error {
 	return nil
 }
 
c2cb302d
 func toKind(kind snapshotsapi.Kind) snapshots.Kind {
 	if kind == snapshotsapi.KindActive {
 		return snapshots.KindActive
7acea2a2
 	}
c2cb302d
 	if kind == snapshotsapi.KindView {
 		return snapshots.KindView
7acea2a2
 	}
c2cb302d
 	return snapshots.KindCommitted
7acea2a2
 }
 
c2cb302d
 func toInfo(info snapshotsapi.Info) snapshots.Info {
 	return snapshots.Info{
7acea2a2
 		Name:    info.Name,
 		Parent:  info.Parent,
 		Kind:    toKind(info.Kind),
 		Created: info.CreatedAt,
 		Updated: info.UpdatedAt,
 		Labels:  info.Labels,
 	}
 }
 
c2cb302d
 func toUsage(resp *snapshotsapi.UsageResponse) snapshots.Usage {
 	return snapshots.Usage{
7acea2a2
 		Inodes: resp.Inodes,
 		Size:   resp.Size_,
 	}
 }
 
 func toMounts(mm []*types.Mount) []mount.Mount {
 	mounts := make([]mount.Mount, len(mm))
 	for i, m := range mm {
 		mounts[i] = mount.Mount{
 			Type:    m.Type,
 			Source:  m.Source,
 			Options: m.Options,
 		}
 	}
 	return mounts
 }
5bd902b5
 
c2cb302d
 func fromKind(kind snapshots.Kind) snapshotsapi.Kind {
 	if kind == snapshots.KindActive {
 		return snapshotsapi.KindActive
5bd902b5
 	}
c2cb302d
 	if kind == snapshots.KindView {
 		return snapshotsapi.KindView
5bd902b5
 	}
c2cb302d
 	return snapshotsapi.KindCommitted
5bd902b5
 }
 
c2cb302d
 func fromInfo(info snapshots.Info) snapshotsapi.Info {
 	return snapshotsapi.Info{
5bd902b5
 		Name:      info.Name,
 		Parent:    info.Parent,
 		Kind:      fromKind(info.Kind),
 		CreatedAt: info.Created,
 		UpdatedAt: info.Updated,
 		Labels:    info.Labels,
 	}
 }