forked from mgoltzsche/ctnr
/
imagerepo.go
153 lines (139 loc) · 4.26 KB
/
imagerepo.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package store
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/mgoltzsche/cntnr/pkg/atomic"
exterrors "github.com/mgoltzsche/cntnr/pkg/errors"
lock "github.com/mgoltzsche/cntnr/pkg/lock"
ispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
type ImageRepo struct {
extBlobDir string
indexFile string
index ispecs.Index
lock lock.Locker
}
func OpenImageRepo(dir, externalBlobDir string, create bool) (r *ImageRepo, err error) {
dir = filepath.Clean(dir)
r = &ImageRepo{extBlobDir: externalBlobDir}
defer exterrors.Wrapd(&err, "open image repo")
if externalBlobDir != "" && !filepath.IsAbs(externalBlobDir) {
return nil, errors.Errorf("externalBlobDir %q is not an absolute path", externalBlobDir)
}
// Lock repo directory
// TODO: Use tmpfs
r.lock, err = lock.LockFile(dir + ".lock")
if err != nil {
return
}
if err = r.lock.Lock(); err != nil {
return
}
defer func() {
if err != nil {
e := r.lock.Unlock()
err = exterrors.Append(err, errors.Wrap(e, "unlock image repo"))
}
}()
// Create image directory if not exists
if _, e := os.Stat(dir); os.IsNotExist(e) {
if create {
if err = os.MkdirAll(dir, 0775); err != nil {
return r, errors.New(err.Error())
}
} else {
return r, errors.Errorf("repo dir %s does not exist", dir)
}
}
// Create/link blob dir if not exists
blobDir := filepath.Join(dir, "blobs")
if externalBlobDir == "" {
if err = os.MkdirAll(blobDir, 0755); err != nil {
return r, errors.New(err.Error())
}
} else if _, e := os.Lstat(blobDir); os.IsNotExist(e) {
if _, e = os.Stat(externalBlobDir); os.IsNotExist(e) {
return r, errors.Errorf("external blob dir %s does not exist", externalBlobDir)
}
if err = os.Symlink(externalBlobDir, blobDir); err != nil {
return r, errors.New("link external blob dir: " + err.Error())
}
}
// Create/check oci-layout
layoutFile := filepath.Join(dir, "oci-layout")
if _, e := os.Stat(layoutFile); os.IsNotExist(e) {
// Create new oci-layout file
layout := ispecs.ImageLayout{}
layout.Version = ispecs.ImageLayoutVersion
if _, err = atomic.WriteJson(layoutFile, &layout); err != nil {
return
}
} else {
// Check existing layout's version
layout := ispecs.ImageLayout{}
b, err := ioutil.ReadFile(layoutFile)
if err != nil {
return nil, errors.New("read oci-layout: " + err.Error())
}
if err = json.Unmarshal(b, &layout); err != nil {
return nil, errors.New("unmarshal oci-layout: " + err.Error())
}
if layout.Version != ispecs.ImageLayoutVersion {
return nil, errors.Errorf("unsupported oci-layout version %q", layout.Version)
}
}
// Create/load index.json
r.indexFile = filepath.Join(dir, "index.json")
if _, e := os.Stat(r.indexFile); os.IsNotExist(e) {
r.index.SchemaVersion = 2
} else {
if err = imageIndex(dir, &r.index); err != nil {
return
}
if r.index.SchemaVersion != 2 {
return nil, errors.Errorf("unsupported index schema version %d in %s", r.index.SchemaVersion, r.indexFile)
}
}
return
}
func (r *ImageRepo) Close() (err error) {
// Unlock image repo dir
defer func() {
err = exterrors.Append(err, r.lock.Unlock())
err = errors.Wrap(err, "close image repo")
}()
if len(r.index.Manifests) == 0 {
// Delete whole image repo directory if manifest list is empty
if err = os.RemoveAll(filepath.Dir(r.indexFile)); err != nil {
err = errors.New(err.Error())
}
} else {
// Write modified index.json
_, err = atomic.WriteJson(r.indexFile, &r.index)
}
return
}
func (r *ImageRepo) AddRef(descriptor ispecs.Descriptor) {
filtered := make([]ispecs.Descriptor, 0, len(r.index.Manifests)+1)
filtered = append(filtered, descriptor)
newRef := descriptor.Annotations[ispecs.AnnotationRefName]
for _, m := range r.index.Manifests {
ref := m.Annotations[ispecs.AnnotationRefName]
if !(ref == newRef && m.Platform.Architecture == descriptor.Platform.Architecture && m.Platform.OS == descriptor.Platform.OS) {
filtered = append(filtered, m)
}
}
r.index.Manifests = filtered
}
func (r *ImageRepo) DelRef(ref string) {
filtered := make([]ispecs.Descriptor, 0, len(r.index.Manifests))
for _, m := range r.index.Manifests {
if ref != m.Annotations[ispecs.AnnotationRefName] {
filtered = append(filtered, m)
}
}
r.index.Manifests = filtered
}