-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
115 lines (93 loc) · 2.59 KB
/
handler.go
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
111
112
113
114
115
package directory
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
digest "github.com/opencontainers/go-digest"
oci "github.com/opencontainers/image-spec/specs-go/v1"
)
type Handler struct {
ociImageDir string
}
func NewHandler(oid string) *Handler {
return &Handler{
/* handle both oci directory path and oci:///<directory-path> */
ociImageDir: strings.TrimPrefix(oid, "oci:///"),
}
}
func (h *Handler) AddBlob(srcBlobPath string, blobDescriptor oci.Descriptor) error {
layerfd, err := os.Open(srcBlobPath)
if err != nil {
return err
}
defer layerfd.Close()
if _, err := os.Stat(h.blobsDir()); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s is not a valid OCI image: %s directory missing", h.ociImageDir, h.blobsDir())
}
return err
}
if err := blobDescriptor.Digest.Validate(); err != nil {
return err
}
destfd, err := os.Create(h.blobsPath(blobDescriptor.Digest.Encoded()))
if err != nil {
return err
}
defer destfd.Close()
_, err = io.Copy(destfd, layerfd)
return err
}
func (h *Handler) RemoveTopBlob(sha256 string) error {
if _, err := os.Stat(h.blobsDir()); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s is not a valid OCI image: %s directory missing", h.ociImageDir, h.blobsDir())
}
return err
}
if err := os.Remove(h.blobsPath(sha256)); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s does not contain layer: %s", h.ociImageDir, sha256)
}
return err
}
return nil
}
func (h *Handler) ClearMetadata() error {
filesToDelete := []string{h.ociLayoutPath(), h.indexPath()}
i, err := h.loadIndex()
if err != nil {
return fmt.Errorf("couldn't load index.json: %s", err.Error())
}
mDesc := i.Manifests[0]
filesToDelete = append(filesToDelete, h.blobsPathFromDescriptor(mDesc))
m, err := h.loadManifest(mDesc)
if err != nil {
return fmt.Errorf("couldn't load manifest: %s", err.Error())
}
filesToDelete = append(filesToDelete, h.blobsPathFromDescriptor(m.Config))
var errRet error
for _, f := range filesToDelete {
if err := os.RemoveAll(f); err != nil {
errRet = err
}
}
return errRet
}
func (h *Handler) blobsPathFromDescriptor(desc oci.Descriptor) string {
return h.blobsPath(desc.Digest.Encoded())
}
func (h *Handler) blobsPath(filename string) string {
return filepath.Join(h.blobsDir(), filename)
}
func (h *Handler) blobsDir() string {
return filepath.Join(h.ociImageDir, "blobs", string(digest.SHA256))
}
func (h *Handler) indexPath() string {
return filepath.Join(h.ociImageDir, "index.json")
}
func (h *Handler) ociLayoutPath() string {
return filepath.Join(h.ociImageDir, "oci-layout")
}