/
image_dir.go
200 lines (170 loc) · 3.69 KB
/
image_dir.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
package etcd
import (
"archive/tar"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/cybozu-go/log"
"github.com/cybozu-go/sabakan"
)
/*
Directory structure:
/var/lib/sabakan/
- OS (coreos) /
- ID1/
- kernel
- initrd.gz
- ID2/
- kernel
- initrd.gz
- ...
While extracting the image archive, a temporary directory is created
under `/var/lib/sabakan/OS/`. The temporary directory will be renamed
to `IDxx` once extraction successfully completed.
*/
// ImageDir is a struct to manage an image directory.
type ImageDir struct {
// Dir is an absolute path to point image directory of an OS.
Dir string
}
// Exists returns true if image files referenced by "id"
// is stored in the directory.
func (d ImageDir) Exists(id string) bool {
p := filepath.Join(d.Dir, id)
_, err := os.Stat(p)
return err == nil
}
func writeToFile(p string, r io.Reader) error {
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
err = f.Chmod(0644)
if err != nil {
return err
}
_, err = io.Copy(f, r)
if err != nil {
return err
}
return f.Sync()
}
// Extract reads tar archive from "r" to extract files shown in "members".
//
// Extracted files are finally stored under "id" directory.
// If the tar archive contains a file not in "members", or if the tar
// archive lacks a file in "members", this function returns sabakan.ErrBadRequest.
func (d ImageDir) Extract(r io.Reader, id string, members []string) error {
defer func() {
io.Copy(ioutil.Discard, r)
}()
err := os.MkdirAll(d.Dir, 0755)
if err != nil {
return err
}
tmpdir, err := ioutil.TempDir(d.Dir, "_tmp")
if err != nil {
return err
}
defer func() {
if tmpdir == "" {
return
}
os.RemoveAll(tmpdir)
}()
memberMap := make(map[string]bool)
for _, m := range members {
memberMap[m] = true
}
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if !memberMap[hdr.Name] {
return sabakan.ErrBadRequest
}
delete(memberMap, hdr.Name)
err = writeToFile(filepath.Join(tmpdir, hdr.Name), tr)
if err != nil {
return err
}
}
if len(memberMap) > 0 {
return sabakan.ErrBadRequest
}
err = os.Rename(tmpdir, filepath.Join(d.Dir, id))
if err != nil {
return err
}
tmpdir = ""
return nil
}
func copyFile(w io.Writer, p string) error {
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(w, f)
return err
}
// Download writes files in a directory specified by "id" as a tar archive.
func (d ImageDir) Download(w io.Writer, id string) error {
files, err := ioutil.ReadDir(filepath.Join(d.Dir, id))
if err != nil {
return err
}
tw := tar.NewWriter(w)
for _, fi := range files {
if !fi.Mode().IsRegular() {
log.Warn("non-regular file in image dir", map[string]interface{}{
"dir": filepath.Join(d.Dir, id),
"name": fi.Name(),
})
continue
}
hdr := &tar.Header{
Name: fi.Name(),
Size: fi.Size(),
Mode: 0644,
}
err = tw.WriteHeader(hdr)
if err != nil {
return err
}
err = copyFile(tw, filepath.Join(d.Dir, id, fi.Name()))
if err != nil {
return err
}
}
return tw.Close()
}
// ServeFile opens filename in "id" directory then calls "f" with the opened file.
func (d ImageDir) ServeFile(id, filename string, f func(content io.ReadSeeker)) error {
p := filepath.Join(d.Dir, id, filename)
g, err := os.Open(p)
if err != nil {
return err
}
defer g.Close()
f(g)
return nil
}
// GC removes images listed in "ids".
func (d ImageDir) GC(ids []string) error {
for _, id := range ids {
p := filepath.Join(d.Dir, id)
err := os.RemoveAll(p)
if err != nil {
return err
}
}
return nil
}