1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"encoding/json"
"errors"
"fmt"
"io"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/stargz-snapshotter/estargz"
"github.com/containerd/stargz-snapshotter/estargz/zstdchunked"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli"
)
// GetTOCDigestCommand outputs TOC info of a layer
var GetTOCDigestCommand = cli.Command{
Name: "get-toc-digest",
Usage: "get the digest of TOC of a layer",
ArgsUsage: "<layer digest>",
Flags: []cli.Flag{
// zstd:chunked flags
cli.BoolFlag{
Name: "zstdchunked",
Usage: "parse layer as zstd:chunked",
},
// other flags for debugging
cli.BoolFlag{
Name: "dump-toc",
Usage: "dump TOC instead of digest. Note that the dumped TOC might be formatted with indents so may have different digest against the original in the layer",
},
},
Action: func(clicontext *cli.Context) error {
layerDgstStr := clicontext.Args().Get(0)
if layerDgstStr == "" {
return errors.New("layer digest need to be specified")
}
client, ctx, cancel, err := commands.NewClient(clicontext)
if err != nil {
return err
}
defer cancel()
layerDgst, err := digest.Parse(layerDgstStr)
if err != nil {
return err
}
ra, err := client.ContentStore().ReaderAt(ctx, ocispec.Descriptor{Digest: layerDgst})
if err != nil {
return err
}
defer ra.Close()
footerSize := estargz.FooterSize
if clicontext.Bool("zstdchunked") {
footerSize = zstdchunked.FooterSize
}
footer := make([]byte, footerSize)
if _, err := ra.ReadAt(footer, ra.Size()-int64(footerSize)); err != nil {
return fmt.Errorf("error reading footer: %w", err)
}
var decompressor estargz.Decompressor
decompressor = new(estargz.GzipDecompressor)
if clicontext.Bool("zstdchunked") {
decompressor = new(zstdchunked.Decompressor)
}
_, tocOff, tocSize, err := decompressor.ParseFooter(footer)
if err != nil {
return fmt.Errorf("error parsing footer: %w", err)
}
if tocSize <= 0 {
tocSize = ra.Size() - tocOff - int64(footerSize)
}
toc, tocDgst, err := decompressor.ParseTOC(io.NewSectionReader(ra, tocOff, tocSize))
if err != nil {
return fmt.Errorf("error parsing TOC: %w", err)
}
if clicontext.Bool("dump-toc") {
tocJSON, err := json.MarshalIndent(toc, "", "\t")
if err != nil {
return fmt.Errorf("failed to marshal toc: %w", err)
}
fmt.Println(string(tocJSON))
return nil
}
fmt.Println(tocDgst.String())
return nil
},
}
|