github.com/uppal0016/docker_new@v0.0.0-20240123060250-1c98be13ac2c/layer/ro_layer.go (about) 1 package layer 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/docker/distribution/digest" 8 ) 9 10 type roLayer struct { 11 chainID ChainID 12 diffID DiffID 13 parent *roLayer 14 cacheID string 15 size int64 16 layerStore *layerStore 17 18 referenceCount int 19 references map[Layer]struct{} 20 } 21 22 func (rl *roLayer) TarStream() (io.ReadCloser, error) { 23 r, err := rl.layerStore.store.TarSplitReader(rl.chainID) 24 if err != nil { 25 return nil, err 26 } 27 28 pr, pw := io.Pipe() 29 go func() { 30 err := rl.layerStore.assembleTarTo(rl.cacheID, r, nil, pw) 31 if err != nil { 32 pw.CloseWithError(err) 33 } else { 34 pw.Close() 35 } 36 }() 37 rc, err := newVerifiedReadCloser(pr, digest.Digest(rl.diffID)) 38 if err != nil { 39 return nil, err 40 } 41 return rc, nil 42 } 43 44 func (rl *roLayer) ChainID() ChainID { 45 return rl.chainID 46 } 47 48 func (rl *roLayer) DiffID() DiffID { 49 return rl.diffID 50 } 51 52 func (rl *roLayer) Parent() Layer { 53 if rl.parent == nil { 54 return nil 55 } 56 return rl.parent 57 } 58 59 func (rl *roLayer) Size() (size int64, err error) { 60 if rl.parent != nil { 61 size, err = rl.parent.Size() 62 if err != nil { 63 return 64 } 65 } 66 67 return size + rl.size, nil 68 } 69 70 func (rl *roLayer) DiffSize() (size int64, err error) { 71 return rl.size, nil 72 } 73 74 func (rl *roLayer) Metadata() (map[string]string, error) { 75 return rl.layerStore.driver.GetMetadata(rl.cacheID) 76 } 77 78 type referencedCacheLayer struct { 79 *roLayer 80 } 81 82 func (rl *roLayer) getReference() Layer { 83 ref := &referencedCacheLayer{ 84 roLayer: rl, 85 } 86 rl.references[ref] = struct{}{} 87 88 return ref 89 } 90 91 func (rl *roLayer) hasReference(ref Layer) bool { 92 _, ok := rl.references[ref] 93 return ok 94 } 95 96 func (rl *roLayer) hasReferences() bool { 97 return len(rl.references) > 0 98 } 99 100 func (rl *roLayer) deleteReference(ref Layer) { 101 delete(rl.references, ref) 102 } 103 104 func (rl *roLayer) depth() int { 105 if rl.parent == nil { 106 return 1 107 } 108 return rl.parent.depth() + 1 109 } 110 111 func storeLayer(tx MetadataTransaction, layer *roLayer) error { 112 if err := tx.SetDiffID(layer.diffID); err != nil { 113 return err 114 } 115 if err := tx.SetSize(layer.size); err != nil { 116 return err 117 } 118 if err := tx.SetCacheID(layer.cacheID); err != nil { 119 return err 120 } 121 if layer.parent != nil { 122 if err := tx.SetParent(layer.parent.chainID); err != nil { 123 return err 124 } 125 } 126 127 return nil 128 } 129 130 func newVerifiedReadCloser(rc io.ReadCloser, dgst digest.Digest) (io.ReadCloser, error) { 131 verifier, err := digest.NewDigestVerifier(dgst) 132 if err != nil { 133 return nil, err 134 } 135 return &verifiedReadCloser{ 136 rc: rc, 137 dgst: dgst, 138 verifier: verifier, 139 }, nil 140 } 141 142 type verifiedReadCloser struct { 143 rc io.ReadCloser 144 dgst digest.Digest 145 verifier digest.Verifier 146 } 147 148 func (vrc *verifiedReadCloser) Read(p []byte) (n int, err error) { 149 n, err = vrc.rc.Read(p) 150 if n > 0 { 151 if n, err := vrc.verifier.Write(p[:n]); err != nil { 152 return n, err 153 } 154 } 155 if err == io.EOF { 156 if !vrc.verifier.Verified() { 157 err = fmt.Errorf("could not verify layer data for: %s. This may be because internal files in the layer store were modified. Re-pulling or rebuilding this image may resolve the issue", vrc.dgst) 158 } 159 } 160 return 161 } 162 func (vrc *verifiedReadCloser) Close() error { 163 return vrc.rc.Close() 164 }