Debian Codegraph @

cmd/ctr-remote/commands/get-toc-digest.go

raw
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 commands
18
19 import (
20 "encoding/json"
21 "errors"
22 "fmt"
23 "io"
24
25 "github.com/containerd/containerd/cmd/ctr/commands"
26 "github.com/containerd/stargz-snapshotter/estargz"
27 "github.com/containerd/stargz-snapshotter/estargz/zstdchunked"
28 digest "github.com/opencontainers/go-digest"
29 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
30 "github.com/urfave/cli"
31 )
32
33 // GetTOCDigestCommand outputs TOC info of a layer
34 var GetTOCDigestCommand = cli.Command{
35 Name: "get-toc-digest",
36 Usage: "get the digest of TOC of a layer",
37 ArgsUsage: "<layer digest>",
38 Flags: []cli.Flag{
39 // zstd:chunked flags
40 cli.BoolFlag{
41 Name: "zstdchunked",
42 Usage: "parse layer as zstd:chunked",
43 },
44 // other flags for debugging
45 cli.BoolFlag{
46 Name: "dump-toc",
47 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",
48 },
49 },
50 Action: func(clicontext *cli.Context) error {
51 layerDgstStr := clicontext.Args().Get(0)
52 if layerDgstStr == "" {
53 return errors.New("layer digest need to be specified")
54 }
55
56 client, ctx, cancel, err := commands.NewClient(clicontext)
57 if err != nil {
58 return err
59 }
60 defer cancel()
61
62 layerDgst, err := digest.Parse(layerDgstStr)
63 if err != nil {
64 return err
65 }
66 ra, err := client.ContentStore().ReaderAt(ctx, ocispec.Descriptor{Digest: layerDgst})
67 if err != nil {
68 return err
69 }
70 defer ra.Close()
71
72 footerSize := estargz.FooterSize
73 if clicontext.Bool("zstdchunked") {
74 footerSize = zstdchunked.FooterSize
75 }
76 footer := make([]byte, footerSize)
77 if _, err := ra.ReadAt(footer, ra.Size()-int64(footerSize)); err != nil {
78 return fmt.Errorf("error reading footer: %w", err)
79 }
80
81 var decompressor estargz.Decompressor
82 decompressor = new(estargz.GzipDecompressor)
83 if clicontext.Bool("zstdchunked") {
84 decompressor = new(zstdchunked.Decompressor)
85 }
86
87 _, tocOff, tocSize, err := decompressor.ParseFooter(footer)
88 if err != nil {
89 return fmt.Errorf("error parsing footer: %w", err)
90 }
91 if tocSize <= 0 {
92 tocSize = ra.Size() - tocOff - int64(footerSize)
93 }
94 toc, tocDgst, err := decompressor.ParseTOC(io.NewSectionReader(ra, tocOff, tocSize))
95 if err != nil {
96 return fmt.Errorf("error parsing TOC: %w", err)
97 }
98
99 if clicontext.Bool("dump-toc") {
100 tocJSON, err := json.MarshalIndent(toc, "", "\t")
101 if err != nil {
102 return fmt.Errorf("failed to marshal toc: %w", err)
103 }
104 fmt.Println(string(tocJSON))
105 return nil
106 }
107 fmt.Println(tocDgst.String())
108 return nil
109 },
110 }