forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharmarchive.go
315 lines (280 loc) · 7.45 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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/juju/utils/set"
ziputil "github.com/juju/utils/zip"
)
// The 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
meta *Meta
config *Config
metrics *Metrics
actions *Actions
revision int
}
// 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,
}
zipr, err := zopen.openZip()
if err != nil {
return nil, err
}
defer 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
}
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
}
reader, err = zipOpenFile(zipr, "actions.yaml")
if _, ok := err.(*noCharmArchiveFile); ok {
b.actions = NewActions()
} else if err != nil {
return nil, err
} else {
b.actions, err = ReadActionsYaml(reader)
reader.Close()
if err != nil {
return nil, err
}
}
reader, err = zipOpenFile(zipr, "revision")
if err != nil {
if _, ok := err.(*noCharmArchiveFile); !ok {
return nil, err
}
b.revision = b.meta.OldRevision
} else {
_, err = fmt.Fscan(reader, &b.revision)
if err != nil {
return nil, errors.New("invalid revision file")
}
}
return b, 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)
}
// Revision returns the revision number for the charm
// expanded in dir.
func (a *CharmArchive) Revision() int {
return a.revision
}
// SetRevision changes the charm revision number. This affects the
// revision reported by Revision and the revision of the charm
// directory created by ExpandTo.
func (a *CharmArchive) SetRevision(revision int) {
a.revision = revision
}
// Meta returns the Meta representing the metadata.yaml file from archive.
func (a *CharmArchive) Meta() *Meta {
return a.meta
}
// Config returns the Config representing the config.yaml file
// for the charm archive.
func (a *CharmArchive) Config() *Config {
return a.config
}
// Metrics returns the Metrics representing the metrics.yaml file
// for the charm archive.
func (a *CharmArchive) Metrics() *Metrics {
return a.metrics
}
// Actions returns the Actions map for the actions.yaml file for the charm
// archive.
func (a *CharmArchive) Actions() *Actions {
return a.actions
}
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
}
// Manifest returns a set of the charm's contents.
func (a *CharmArchive) Manifest() (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(paths...)
// 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
}
_, err = revFile.Write([]byte(strconv.Itoa(a.revision)))
revFile.Close()
return err
}
// 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
}
}