-
Notifications
You must be signed in to change notification settings - Fork 59
/
charmarchive.go
345 lines (311 loc) · 8.22 KB
/
charmarchive.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/juju/collections/set"
"github.com/juju/errors"
ziputil "github.com/juju/utils/v3/zip"
)
// CharmArchive type encapsulates access to data and operations
// on a charm archive.
type CharmArchive struct {
zopen zipOpener
Path string // May be empty if CharmArchive wasn't read from a file
*charmBase
}
// Trick to ensure *CharmArchive implements the Charm interface.
var _ Charm = (*CharmArchive)(nil)
// ReadCharmArchive returns a CharmArchive for the charm in path.
func ReadCharmArchive(path string) (*CharmArchive, error) {
a, err := readCharmArchive(newZipOpenerFromPath(path))
if err != nil {
return nil, err
}
a.Path = path
return a, nil
}
// ReadCharmArchiveBytes returns a CharmArchive read from the given data.
// Make sure the archive fits in memory before using this.
func ReadCharmArchiveBytes(data []byte) (archive *CharmArchive, err error) {
zopener := newZipOpenerFromReader(bytes.NewReader(data), int64(len(data)))
return readCharmArchive(zopener)
}
// ReadCharmArchiveFromReader returns a CharmArchive that uses
// r to read the charm. The given size must hold the number
// of available bytes in the file.
//
// Note that the caller is responsible for closing r - methods on
// the returned CharmArchive may fail after that.
func ReadCharmArchiveFromReader(r io.ReaderAt, size int64) (archive *CharmArchive, err error) {
return readCharmArchive(newZipOpenerFromReader(r, size))
}
func readCharmArchive(zopen zipOpener) (archive *CharmArchive, err error) {
b := &CharmArchive{
zopen: zopen,
charmBase: &charmBase{},
}
zipr, err := zopen.openZip()
if err != nil {
return nil, err
}
defer func() { _ = zipr.Close() }()
reader, err := zipOpenFile(zipr, "metadata.yaml")
if err != nil {
return nil, err
}
b.meta, err = ReadMeta(reader)
_ = reader.Close()
if err != nil {
return nil, err
}
// Try to read the optional manifest.yaml, it's required to determine if
// this charm is v1 or not.
reader, err = zipOpenFile(zipr, "manifest.yaml")
if _, ok := err.(*noCharmArchiveFile); ok {
b.manifest = nil
} else if err != nil {
return nil, errors.Annotatef(err, `opening "manifest.yaml" file`)
} else {
b.manifest, err = ReadManifest(reader)
_ = reader.Close()
if err != nil {
return nil, errors.Annotatef(err, `parsing "manifest.yaml" file`)
}
}
reader, err = zipOpenFile(zipr, "config.yaml")
if _, ok := err.(*noCharmArchiveFile); ok {
b.config = NewConfig()
} else if err != nil {
return nil, err
} else {
b.config, err = ReadConfig(reader)
_ = reader.Close()
if err != nil {
return nil, err
}
}
reader, err = zipOpenFile(zipr, "metrics.yaml")
if err == nil {
b.metrics, err = ReadMetrics(reader)
_ = reader.Close()
if err != nil {
return nil, err
}
} else if _, ok := err.(*noCharmArchiveFile); !ok {
return nil, err
}
if b.actions, err = getActions(
b.meta.Name,
func(file string) (io.ReadCloser, error) {
return zipOpenFile(zipr, file)
},
func(err error) bool {
_, ok := err.(*noCharmArchiveFile)
return ok
},
); err != nil {
return nil, err
}
reader, err = zipOpenFile(zipr, "revision")
if err != nil {
if _, ok := err.(*noCharmArchiveFile); !ok {
return nil, err
}
} else {
_, err = fmt.Fscan(reader, &b.revision)
if err != nil {
return nil, errors.New("invalid revision file")
}
}
reader, err = zipOpenFile(zipr, "lxd-profile.yaml")
if _, ok := err.(*noCharmArchiveFile); ok {
b.lxdProfile = NewLXDProfile()
} else if err != nil {
return nil, err
} else {
b.lxdProfile, err = ReadLXDProfile(reader)
_ = reader.Close()
if err != nil {
return nil, err
}
}
reader, err = zipOpenFile(zipr, "version")
if err != nil {
if _, ok := err.(*noCharmArchiveFile); !ok {
return nil, err
}
} else {
b.version, err = ReadVersion(reader)
_ = reader.Close()
if err != nil {
return nil, err
}
}
return b, nil
}
type fileOpener func(string) (io.ReadCloser, error)
func getActions(charmName string, open fileOpener, isNotFound func(error) bool) (actions *Actions, err error) {
reader, err := open("actions.yaml")
if err == nil {
defer reader.Close()
return ReadActionsYaml(charmName, reader)
} else if !isNotFound(err) {
return nil, err
}
return NewActions(), nil
}
func zipOpenFile(zipr *zipReadCloser, path string) (rc io.ReadCloser, err error) {
for _, fh := range zipr.File {
if fh.Name == path {
return fh.Open()
}
}
return nil, &noCharmArchiveFile{path}
}
type noCharmArchiveFile struct {
path string
}
func (err noCharmArchiveFile) Error() string {
return fmt.Sprintf("archive file %q not found", err.path)
}
type zipReadCloser struct {
io.Closer
*zip.Reader
}
// zipOpener holds the information needed to open a zip
// file.
type zipOpener interface {
openZip() (*zipReadCloser, error)
}
// newZipOpenerFromPath returns a zipOpener that can be
// used to read the archive from the given path.
func newZipOpenerFromPath(path string) zipOpener {
return &zipPathOpener{path: path}
}
// newZipOpenerFromReader returns a zipOpener that can be
// used to read the archive from the given ReaderAt
// holding the given number of bytes.
func newZipOpenerFromReader(r io.ReaderAt, size int64) zipOpener {
return &zipReaderOpener{
r: r,
size: size,
}
}
type zipPathOpener struct {
path string
}
func (zo *zipPathOpener) openZip() (*zipReadCloser, error) {
f, err := os.Open(zo.path)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
r, err := zip.NewReader(f, fi.Size())
if err != nil {
f.Close()
return nil, err
}
return &zipReadCloser{Closer: f, Reader: r}, nil
}
type zipReaderOpener struct {
r io.ReaderAt
size int64
}
func (zo *zipReaderOpener) openZip() (*zipReadCloser, error) {
r, err := zip.NewReader(zo.r, zo.size)
if err != nil {
return nil, err
}
return &zipReadCloser{Closer: ioutil.NopCloser(nil), Reader: r}, nil
}
// ArchiveMembers returns a set of the charm's contents.
func (a *CharmArchive) ArchiveMembers() (set.Strings, error) {
zipr, err := a.zopen.openZip()
if err != nil {
return set.NewStrings(), err
}
defer zipr.Close()
paths, err := ziputil.Find(zipr.Reader, "*")
if err != nil {
return set.NewStrings(), err
}
manifest := set.NewStrings()
// Zip archives can omit file's parent directories,
// as these are created automatically during extraction.
// Ensure that parent directories exist in the member list.
for _, path := range paths {
for path != "." && path != "/" {
manifest.Add(path)
path = filepath.Dir(path)
}
}
// We always write out a revision file, even if there isn't one in the
// archive; and we always strip ".", because that's sometimes not present.
manifest.Add("revision")
manifest.Remove(".")
return manifest, nil
}
// ExpandTo expands the charm archive into dir, creating it if necessary.
// If any errors occur during the expansion procedure, the process will
// abort.
func (a *CharmArchive) ExpandTo(dir string) error {
zipr, err := a.zopen.openZip()
if err != nil {
return err
}
defer zipr.Close()
if err := ziputil.ExtractAll(zipr.Reader, dir); err != nil {
return err
}
hooksDir := filepath.Join(dir, "hooks")
fixHook := fixHookFunc(hooksDir, a.meta.Hooks())
if err := filepath.Walk(hooksDir, fixHook); err != nil {
if !os.IsNotExist(err) {
return err
}
}
revFile, err := os.Create(filepath.Join(dir, "revision"))
if err != nil {
return err
}
if _, err := revFile.Write([]byte(strconv.Itoa(a.revision))); err != nil {
return err
}
if err := revFile.Sync(); err != nil {
return err
}
if err := revFile.Close(); err != nil {
return err
}
return nil
}
// fixHookFunc returns a WalkFunc that makes sure hooks are owner-executable.
func fixHookFunc(hooksDir string, hookNames map[string]bool) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
mode := info.Mode()
if path != hooksDir && mode.IsDir() {
return filepath.SkipDir
}
if name := filepath.Base(path); hookNames[name] {
if mode&0100 == 0 {
return os.Chmod(path, mode|0100)
}
}
return nil
}
}