Merge lp:~sergiusens/snappy/trunkBackport470 into lp:~snappy-dev/snappy/15.04-deprecated

Proposed by Sergio Schvezov on 2015-06-02
Status: Merged
Approved by: Michael Vogt on 2015-06-03
Approved revision: 444
Merged at revision: 446
Proposed branch: lp:~sergiusens/snappy/trunkBackport470
Merge into: lp:~snappy-dev/snappy/15.04-deprecated
Diff against target: 134 lines (+59/-3)
2 files modified
partition/bootloader_uboot.go (+20/-3)
partition/bootloader_uboot_test.go (+39/-0)
To merge this branch: bzr merge lp:~sergiusens/snappy/trunkBackport470
Reviewer Review Type Date Requested Status
Michael Vogt 2015-06-02 Approve on 2015-06-03
Review via email: mp+260884@code.launchpad.net

This proposal supersedes a proposal from 2015-06-02.

Commit Message

Backport revno 470 from trunk: Avoid writing to snappy-system.txt if not required

Description of the Change

bzr merge -c 470 lp:snappy

To post a comment you must log in.
Sergio Schvezov (sergiusens) wrote :

All good, what could possibly go wrong

Michael Vogt (mvo) wrote :

Looks good, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'partition/bootloader_uboot.go'
2--- partition/bootloader_uboot.go 2015-04-30 11:28:39 +0000
3+++ partition/bootloader_uboot.go 2015-06-02 20:27:20 +0000
4@@ -51,6 +51,7 @@
5 bootloaderUbootConfigFile = bootloaderUbootConfigFileReal
6 bootloaderUbootStampFile = bootloaderUbootStampFileReal
7 bootloaderUbootEnvFile = bootloaderUbootEnvFileReal
8+ atomicFileUpdate = atomicFileUpdateImpl
9 )
10
11 const bootloaderNameUboot bootloaderName = "u-boot"
12@@ -64,6 +65,7 @@
13 }
14
15 // Stores a Name and a Value to be added as a name=value pair in a file.
16+// TODO convert to map
17 type configFileChange struct {
18 Name string
19 Value string
20@@ -320,7 +322,7 @@
21
22 // Write lines to file atomically. File does not have to preexist.
23 // FIXME: put into utils package
24-func atomicFileUpdate(file string, lines []string) (err error) {
25+func atomicFileUpdateImpl(file string, lines []string) (err error) {
26 tmpFile := fmt.Sprintf("%s.NEW", file)
27
28 // XXX: if go switches to use aio_fsync, we need to open the dir for writing
29@@ -349,6 +351,7 @@
30 // appended to the file.
31 //
32 // FIXME: put into utils package
33+// FIXME: improve logic
34 func modifyNameValueFile(file string, changes []configFileChange) (err error) {
35 var updated []configFileChange
36
37@@ -358,12 +361,20 @@
38 }
39
40 var new []string
41+ // we won't write to a file if we don't need to.
42+ updateNeeded := false
43
44 for _, line := range lines {
45 for _, change := range changes {
46 if strings.HasPrefix(line, fmt.Sprintf("%s=", change.Name)) {
47- line = fmt.Sprintf("%s=%s", change.Name, change.Value)
48+ value := strings.SplitN(line, "=", 2)[1]
49+ // updated is used later to see if you had the originally requested
50+ // value.
51 updated = append(updated, change)
52+ if value != change.Value {
53+ line = fmt.Sprintf("%s=%s", change.Name, change.Value)
54+ updateNeeded = true
55+ }
56 }
57 }
58 new = append(new, line)
59@@ -381,6 +392,8 @@
60 }
61
62 if !got {
63+ updateNeeded = true
64+
65 // name/value pair did not exist in original
66 // file, so append
67 lines = append(lines, fmt.Sprintf("%s=%s",
68@@ -388,7 +401,11 @@
69 }
70 }
71
72- return atomicFileUpdate(file, lines)
73+ if updateNeeded {
74+ return atomicFileUpdate(file, lines)
75+ }
76+
77+ return nil
78 }
79
80 func (u *uboot) AdditionalBindMounts() []string {
81
82=== modified file 'partition/bootloader_uboot_test.go'
83--- partition/bootloader_uboot_test.go 2015-04-30 11:28:39 +0000
84+++ partition/bootloader_uboot_test.go 2015-06-02 20:27:20 +0000
85@@ -27,6 +27,8 @@
86 "launchpad.net/snappy/helpers"
87 )
88
89+// TODO move to uboot specific test suite.
90+
91 const fakeUbootEnvData = `
92 # This is a snappy variables and boot logic file and is entirely generated and
93 # managed by Snappy. Modifications may break boot
94@@ -289,3 +291,40 @@
95 c.Assert(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true)
96 c.Assert(strings.Contains(string(bytes), "snappy_ab=a"), Equals, true)
97 }
98+
99+func (s *PartitionTestSuite) TestNoWriteNotNeeded(c *C) {
100+ s.makeFakeUbootEnv(c)
101+
102+ atomiCall := false
103+ atomicFileUpdate = func(a string, b []string) error { atomiCall = true; return atomicFileUpdateImpl(a, b) }
104+
105+ partition := New()
106+ u := newUboot(partition)
107+ c.Assert(u, NotNil)
108+
109+ c.Check(u.MarkCurrentBootSuccessful(), IsNil)
110+ c.Assert(atomiCall, Equals, false)
111+}
112+
113+func (s *PartitionTestSuite) TestWriteDueToMissingValues(c *C) {
114+ s.makeFakeUbootEnv(c)
115+
116+ // this file needs specific data
117+ c.Assert(ioutil.WriteFile(bootloaderUbootEnvFile, []byte(""), 0644), IsNil)
118+
119+ atomiCall := false
120+ atomicFileUpdate = func(a string, b []string) error { atomiCall = true; return atomicFileUpdateImpl(a, b) }
121+
122+ partition := New()
123+ u := newUboot(partition)
124+ c.Assert(u, NotNil)
125+
126+ c.Check(u.MarkCurrentBootSuccessful(), IsNil)
127+ c.Assert(atomiCall, Equals, true)
128+
129+ bytes, err := ioutil.ReadFile(bootloaderUbootEnvFile)
130+ c.Assert(err, IsNil)
131+ c.Check(strings.Contains(string(bytes), "snappy_mode=try"), Equals, false)
132+ c.Check(strings.Contains(string(bytes), "snappy_mode=regular"), Equals, true)
133+ c.Check(strings.Contains(string(bytes), "snappy_ab=a"), Equals, true)
134+}

Subscribers

People subscribed via source and target branches