Merge lp:~chipaca/snappy/fsync into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by John Lenton
Status: Merged
Approved by: Michael Vogt
Approved revision: 432
Merged at revision: 431
Proposed branch: lp:~chipaca/snappy/fsync
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 119 lines (+57/-11)
2 files modified
helpers/helpers.go (+37/-8)
partition/bootloader_uboot.go (+20/-3)
To merge this branch: bzr merge lp:~chipaca/snappy/fsync
Reviewer Review Type Date Requested Status
Michael Vogt (community) Approve
Review via email: mp+257726@code.launchpad.net

Commit message

do fsync after "atomic" file writes

Description of the change

This might make mounting boot "sync" unnecessary.

To post a comment you must log in.
lp:~chipaca/snappy/fsync updated
432. By John Lenton

oops

Revision history for this message
Michael Vogt (mvo) wrote :

Thanks for doing this work, its really great! One comment/question inline.

Revision history for this message
Michael Vogt (mvo) wrote :

One more comment from Steve.

Revision history for this message
John Lenton (chipaca) :
Revision history for this message
Michael Vogt (mvo) :
Revision history for this message
Michael Vogt (mvo) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'helpers/helpers.go'
2--- helpers/helpers.go 2015-04-23 11:19:42 +0000
3+++ helpers/helpers.go 2015-04-29 09:10:20 +0000
4@@ -23,7 +23,6 @@
5 "encoding/hex"
6 "fmt"
7 "io"
8- "io/ioutil"
9 "math/rand"
10 "os"
11 "os/exec"
12@@ -247,15 +246,45 @@
13
14 // AtomicWriteFile updates the filename atomically and works otherwise
15 // exactly like io/ioutil.WriteFile()
16-func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error {
17+func AtomicWriteFile(filename string, data []byte, perm os.FileMode) (err error) {
18 tmp := filename + ".new"
19
20- if err := ioutil.WriteFile(tmp, data, perm); err != nil {
21- os.Remove(tmp)
22- return err
23- }
24-
25- return os.Rename(tmp, filename)
26+ // XXX: if go switches to use aio_fsync, we need to open the dir for writing
27+ dir, err := os.Open(filepath.Dir(filename))
28+ if err != nil {
29+ return err
30+ }
31+ defer dir.Close()
32+
33+ fd, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
34+ if err != nil {
35+ return err
36+ }
37+ defer func() {
38+ e := fd.Close()
39+ if err == nil {
40+ err = e
41+ }
42+ if err != nil {
43+ os.Remove(tmp)
44+ }
45+ }()
46+
47+ // according to the docs, Write returns a non-nil error when n !=
48+ // len(b), so don't worry about short writes.
49+ if _, err := fd.Write(data); err != nil {
50+ return err
51+ }
52+
53+ if err := fd.Sync(); err != nil {
54+ return err
55+ }
56+
57+ if err := os.Rename(tmp, filename); err != nil {
58+ return err
59+ }
60+
61+ return dir.Sync()
62 }
63
64 // CurrentHomeDir returns the homedir of the current user. It looks at
65
66=== modified file 'partition/bootloader_uboot.go'
67--- partition/bootloader_uboot.go 2015-03-26 09:05:27 +0000
68+++ partition/bootloader_uboot.go 2015-04-29 09:10:20 +0000
69@@ -175,7 +175,12 @@
70 return err
71 }
72
73- defer file.Close()
74+ defer func() {
75+ e := file.Close()
76+ if err == nil {
77+ err = e
78+ }
79+ }()
80
81 writer := bufio.NewWriter(file)
82
83@@ -184,7 +189,12 @@
84 return err
85 }
86 }
87- return writer.Flush()
88+
89+ if err := writer.Flush(); err != nil {
90+ return err
91+ }
92+
93+ return file.Sync()
94 }
95
96 func (u *uboot) MarkCurrentBootSuccessful() (err error) {
97@@ -310,6 +320,13 @@
98 func atomicFileUpdate(file string, lines []string) (err error) {
99 tmpFile := fmt.Sprintf("%s.NEW", file)
100
101+ // XXX: if go switches to use aio_fsync, we need to open the dir for writing
102+ dir, err := os.Open(filepath.Dir(file))
103+ if err != nil {
104+ return err
105+ }
106+ defer dir.Close()
107+
108 if err := writeLines(lines, tmpFile); err != nil {
109 return err
110 }
111@@ -319,7 +336,7 @@
112 return err
113 }
114
115- return nil
116+ return dir.Sync()
117 }
118
119 // Rewrite the specified file, applying the specified set of changes.

Subscribers

People subscribed via source and target branches