Merge lp:~mvo/snappy/snappy-hashes-yaml-for-devices into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Michael Vogt on 2015-05-06
Status: Merged
Approved by: John Lenton on 2015-05-06
Approved revision: 444
Merged at revision: 443
Proposed branch: lp:~mvo/snappy/snappy-hashes-yaml-for-devices
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 236 lines (+123/-23)
4 files modified
snappy/build.go (+46/-20)
snappy/build_test.go (+17/-0)
snappy/hashes.go (+12/-3)
snappy/hashes_test.go (+48/-0)
To merge this branch: bzr merge lp:~mvo/snappy/snappy-hashes-yaml-for-devices
Reviewer Review Type Date Requested Status
John Lenton 2015-05-06 Approve on 2015-05-06
Review via email: mp+258372@code.launchpad.net

Commit Message

Add support for char/block devices in hashes.yaml

Description of the Change

This branch adds support for char/block devices for the hashes.yaml. This is prep work for an eventual move of the OS part as a snap.

To post a comment you must log in.
John Lenton (chipaca) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy/build.go'
2--- snappy/build.go 2015-05-05 23:22:01 +0000
3+++ snappy/build.go 2015-05-06 12:14:38 +0000
4@@ -27,6 +27,7 @@
5 "path/filepath"
6 "regexp"
7 "strings"
8+ "syscall"
9 "text/template"
10
11 "launchpad.net/snappy/clickdeb"
12@@ -216,6 +217,47 @@
13 return strings.Fields(string(output))[0], nil
14 }
15
16+func hashForFile(buildDir, path string, info os.FileInfo) (h *fileHash, err error) {
17+ sha512sum := ""
18+ // pointer so that omitempty works (we don't want size for
19+ // directories or symlinks)
20+ var size *int64
21+ if info.Mode().IsRegular() {
22+ sha512sum, err = helpers.Sha512sum(path)
23+ if err != nil {
24+ return nil, err
25+ }
26+ fsize := info.Size()
27+ size = &fsize
28+ }
29+
30+ // major/minor handling
31+ device := ""
32+ if (info.Mode()&os.ModeDevice) != 0 || (info.Mode()&os.ModeCharDevice) != 0 {
33+ unixStat, ok := info.Sys().(*syscall.Stat_t)
34+ if ok {
35+ // see /usr/include/linux/kdev_t.h
36+ major := int64(unixStat.Rdev >> 8)
37+ minor := int64(unixStat.Rdev & 0xff)
38+ device = fmt.Sprintf("%v,%v", major, minor)
39+ }
40+ }
41+
42+ if buildDir != "" {
43+ path = path[len(buildDir)+1:]
44+ }
45+
46+ return &fileHash{
47+ Name: path,
48+ Size: size,
49+ Sha512: sha512sum,
50+ Device: device,
51+ // FIXME: not portable, this output is different on
52+ // windows, macos
53+ Mode: newYamlFileMode(info.Mode()),
54+ }, nil
55+}
56+
57 func writeHashes(buildDir, dataTar string) error {
58
59 debianDir := filepath.Join(buildDir, "DEBIAN")
60@@ -236,27 +278,11 @@
61 return nil
62 }
63
64- sha512sum := ""
65- // pointer so that omitempty works (we don't want size for
66- // directories or symlinks)
67- var size *int64
68- if info.Mode().IsRegular() {
69- sha512sum, err = helpers.Sha512sum(path)
70- if err != nil {
71- return err
72- }
73- fsize := info.Size()
74- size = &fsize
75+ hash, err := hashForFile(buildDir, path, info)
76+ if err != nil {
77+ return err
78 }
79-
80- hashes.Files = append(hashes.Files, fileHash{
81- Name: path[len(buildDir)+1:],
82- Size: size,
83- Sha512: sha512sum,
84- // FIXME: not portable, this output is different on
85- // windows, macos
86- Mode: newYamlFileMode(info.Mode()),
87- })
88+ hashes.Files = append(hashes.Files, hash)
89
90 return nil
91 })
92
93=== modified file 'snappy/build_test.go'
94--- snappy/build_test.go 2015-05-02 22:52:02 +0000
95+++ snappy/build_test.go 2015-05-06 12:14:38 +0000
96@@ -24,6 +24,8 @@
97 "path/filepath"
98 "strings"
99
100+ "launchpad.net/snappy/helpers"
101+
102 . "launchpad.net/gocheck"
103 )
104
105@@ -409,3 +411,18 @@
106 c.Check(debArchitecture(&packageYaml{Architectures: []string{"foo", "bar"}}), Equals, "multi")
107 c.Check(debArchitecture(&packageYaml{Architectures: nil}), Equals, "unknown")
108 }
109+
110+func (s *SnapTestSuite) TestHashForFileForDevice(c *C) {
111+ // skip test
112+ if !helpers.FileExists("/dev/kmsg") {
113+ c.Skip("no /dev/kmsg")
114+ }
115+
116+ stat, err := os.Stat("/dev/kmsg")
117+ c.Assert(err, IsNil)
118+ h, err := hashForFile("", "/dev/kmsg", stat)
119+ c.Assert(err, IsNil)
120+ c.Assert(h.Name, Equals, "/dev/kmsg")
121+ c.Assert(h.Device, Equals, "1,11")
122+ c.Assert(h.Sha512, Equals, "")
123+}
124
125=== modified file 'snappy/hashes.go'
126--- snappy/hashes.go 2015-03-31 14:12:52 +0000
127+++ snappy/hashes.go 2015-05-06 12:14:38 +0000
128@@ -38,6 +38,10 @@
129 buf[0] = 'd'
130 case (v.mode & os.ModeSymlink) != 0:
131 buf[0] = 'l'
132+ case (v.mode & os.ModeDevice) != 0:
133+ buf[0] = 'b'
134+ case (v.mode & os.ModeCharDevice) != 0:
135+ buf[0] = 'c'
136 case (v.mode & os.ModeType) == 0:
137 buf[0] = 'f'
138 default:
139@@ -67,11 +71,15 @@
140 switch modeAsStr[0] {
141 case 'd':
142 m |= os.ModeDir
143+ case 'l':
144+ m |= os.ModeSymlink
145+ case 'c':
146+ m |= os.ModeCharDevice
147+ case 'b':
148+ m |= os.ModeDevice
149 case 'f':
150 // default
151 m |= 0
152- case 'l':
153- m |= os.ModeSymlink
154 default:
155 return fmt.Errorf("Unknown file mode %s", modeAsStr)
156 }
157@@ -93,6 +101,7 @@
158 type fileHash struct {
159 Name string `yaml:"name"`
160 Size *int64 `yaml:"size,omitempty"`
161+ Device string `yaml:"device,omitempty"`
162 Sha512 string `yaml:"sha512,omitempty"`
163 Mode *yamlFileMode `yaml:"mode"`
164 // FIXME: not used yet, our tar implementation does not
165@@ -106,5 +115,5 @@
166 ArchiveSha512 string `yaml:"archive-sha512"`
167
168 // the hashes for the files in the archive
169- Files []fileHash
170+ Files []*fileHash
171 }
172
173=== modified file 'snappy/hashes_test.go'
174--- snappy/hashes_test.go 2015-03-31 14:12:52 +0000
175+++ snappy/hashes_test.go 2015-05-06 12:14:38 +0000
176@@ -47,6 +47,30 @@
177 c.Assert(h, DeepEquals, fileHashStruct)
178 }
179
180+func (s *SnapTestSuite) TestHashesYamlUnmarshalChar(c *C) {
181+ var h fileHash
182+ err := yaml.Unmarshal([]byte(`name: device
183+mode: crw-r--r--`), &h)
184+ c.Assert(err, IsNil)
185+
186+ c.Assert(h, DeepEquals, fileHash{
187+ Name: "device",
188+ Mode: newYamlFileMode(0644 | os.ModeCharDevice),
189+ })
190+}
191+
192+func (s *SnapTestSuite) TestHashesYamlUnmarshalBlock(c *C) {
193+ var h fileHash
194+ err := yaml.Unmarshal([]byte(`name: device
195+mode: brw-r--r--`), &h)
196+ c.Assert(err, IsNil)
197+
198+ c.Assert(h, DeepEquals, fileHash{
199+ Name: "device",
200+ Mode: newYamlFileMode(0644 | os.ModeDevice),
201+ })
202+}
203+
204 func (s *SnapTestSuite) TestHashesYamlMarshal(c *C) {
205 y, err := yaml.Marshal(&fileHashStruct)
206 c.Assert(err, IsNil)
207@@ -54,6 +78,30 @@
208 c.Assert(string(y), Equals, fileHashYaml)
209 }
210
211+func (s *SnapTestSuite) TestHashesYamlMarshalBlockDevice(c *C) {
212+ y, err := yaml.Marshal(fileHash{
213+ Name: "device",
214+ Mode: newYamlFileMode(0644 | os.ModeDevice),
215+ })
216+ c.Assert(err, IsNil)
217+
218+ c.Assert(string(y), Equals, `name: device
219+mode: brw-r--r--
220+`)
221+}
222+
223+func (s *SnapTestSuite) TestHashesYamlMarshalCharDevice(c *C) {
224+ y, err := yaml.Marshal(fileHash{
225+ Name: "device",
226+ Mode: newYamlFileMode(0644 | os.ModeCharDevice),
227+ })
228+ c.Assert(err, IsNil)
229+
230+ c.Assert(string(y), Equals, `name: device
231+mode: crw-r--r--
232+`)
233+}
234+
235 func (s *SnapTestSuite) TestBuildCreateDebianHashesSimple(c *C) {
236 tempdir := c.MkDir()
237

Subscribers

People subscribed via source and target branches