Debian Codegraph @

cmd/soci/commands/ztoc/info.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter 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 ztoc
18
19 import (
20 "context"
21 "encoding/json"
22 "fmt"
23
24 "github.com/awslabs/soci-snapshotter/soci"
25 "github.com/awslabs/soci-snapshotter/soci/store"
26 "github.com/awslabs/soci-snapshotter/ztoc"
27 "github.com/awslabs/soci-snapshotter/ztoc/compression"
28 "github.com/opencontainers/go-digest"
29 v1 "github.com/opencontainers/image-spec/specs-go/v1"
30 "github.com/urfave/cli"
31 )
32
33 type Info struct {
34 Version string `json:"version"`
35 BuildTool string `json:"build_tool"`
36 Size int64 `json:"size"`
37 SpanSize compression.Offset `json:"span_size"`
38 NumSpans compression.SpanID `json:"num_spans"`
39 NumFiles int `json:"num_files"`
40 NumMultiSpanFiles int `json:"num_multi_span_files"`
41 Files []FileInfo `json:"files"`
42 }
43
44 type FileInfo struct {
45 Filename string `json:"filename"`
46 Offset int64 `json:"offset"`
47 Size int64 `json:"size"`
48 Type string `json:"type"`
49 StartSpan compression.SpanID `json:"start_span"`
50 EndSpan compression.SpanID `json:"end_span"`
51 }
52
53 var infoCommand = cli.Command{
54 Name: "info",
55 Usage: "get detailed info about a ztoc",
56 ArgsUsage: "<digest>",
57 Action: func(cliContext *cli.Context) error {
58 digest, err := digest.Parse(cliContext.Args().First())
59 if err != nil {
60 return err
61 }
62 db, err := soci.NewDB(soci.ArtifactsDbPath())
63 if err != nil {
64 return err
65 }
66 entry, err := db.GetArtifactEntry(digest.String())
67 if err != nil {
68 return err
69 }
70 if entry.MediaType == soci.SociIndexArtifactType {
71 return fmt.Errorf("the provided digest belongs to a SOCI index. Use `soci index info` to get the detailed information about it")
72 }
73 ctx, cancel := context.WithTimeout(context.Background(), cliContext.GlobalDuration("timeout"))
74 defer cancel()
75 ctx, store, err := store.NewContentStore(ctx, store.WithType(store.ContentStoreType(cliContext.GlobalString("content-store"))), store.WithNamespace(cliContext.GlobalString("namespace")))
76 if err != nil {
77 return err
78 }
79 reader, err := store.Fetch(ctx, v1.Descriptor{Digest: digest})
80 if err != nil {
81 return err
82 }
83 defer reader.Close()
84 ztoc, err := ztoc.Unmarshal(reader)
85 if err != nil {
86 return err
87 }
88 gzInfo, err := ztoc.Zinfo()
89 if err != nil {
90 return err
91 }
92
93 multiSpanFiles := 0
94 zinfo := Info{
95 Version: string(ztoc.Version),
96 BuildTool: ztoc.BuildToolIdentifier,
97 Size: entry.Size,
98 SpanSize: gzInfo.SpanSize(),
99 NumSpans: ztoc.MaxSpanID + 1,
100 NumFiles: len(ztoc.FileMetadata),
101 }
102 for _, v := range ztoc.FileMetadata {
103 startSpan := gzInfo.UncompressedOffsetToSpanID(v.UncompressedOffset)
104 endSpan := gzInfo.UncompressedOffsetToSpanID(v.UncompressedOffset + v.UncompressedSize)
105 if startSpan != endSpan {
106 multiSpanFiles++
107 }
108 zinfo.Files = append(zinfo.Files, FileInfo{
109 Filename: v.Name,
110 Offset: int64(v.UncompressedOffset),
111 Size: int64(v.UncompressedSize),
112 Type: v.Type,
113 StartSpan: startSpan,
114 EndSpan: endSpan,
115 })
116 }
117 zinfo.NumMultiSpanFiles = multiSpanFiles
118 j, err := json.MarshalIndent(zinfo, "", " ")
119 if err != nil {
120 return err
121 }
122 fmt.Println(string(j))
123 return nil
124 },
125 }