Ubuntu Pastebin

Paste from egon at Tue, 12 Sep 2017 15:42:24 +0000

Download as text
 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
diff --git a/cmd/snap-seccomp/main.go b/cmd/snap-seccomp/main.go
index 2ef8f85aa..f23588e03 100644
--- a/cmd/snap-seccomp/main.go
+++ b/cmd/snap-seccomp/main.go
@@ -790,12 +790,6 @@ func compile(content []byte, out string) error {
 	}
 
 	// write atomically
-	dir, err := os.Open(filepath.Dir(out))
-	if err != nil {
-		return err
-	}
-	defer dir.Close()
-
 	fout, err := os.Create(out + ".tmp")
 	if err != nil {
 		return err
@@ -804,13 +798,31 @@ func compile(content []byte, out string) error {
 	if err := secFilter.ExportBPF(fout); err != nil {
 		return err
 	}
-	if err := fout.Sync(); err != nil {
-		return err
+
+	var dir *os.File
+	if !osutil.SnapdUnsafeIO {
+		dir, err = os.Open(filepath.Dir(out))
+		if err != nil {
+			return err
+		}
+		defer dir.Close()
+
+		if err := fout.Sync(); err != nil {
+			return err
+		}
 	}
+
 	if err := os.Rename(out+".tmp", out); err != nil {
 		return err
 	}
-	return dir.Sync()
+
+	if !osutil.SnapdUnsafeIO {
+		if err := dir.Sync(); err != nil {
+			return err
+		}
+	}
+
+	return nil
 }
 
 func showSeccompLibraryVersion() error {
diff --git a/osutil/io.go b/osutil/io.go
index 050e10178..298a1149e 100644
--- a/osutil/io.go
+++ b/osutil/io.go
@@ -38,10 +38,11 @@ const (
 	AtomicWriteFollow AtomicWriteFlags = 1 << iota
 )
 
-// Allow disabling sync for testing. This brings massive improvements on
-// certain filesystems (like btrfs) and very much noticeable improvements in
-// all unit tests in genreal.
-var snapdUnsafeIO bool = len(os.Args) > 0 && strings.HasSuffix(os.Args[0], ".test") && GetenvBool("SNAPD_UNSAFE_IO")
+// SnapdUnsafeIO is set when snapd is told to disable sync for
+// testing.  This brings massive improvements on certain filesystems
+// (like btrfs) and very much noticeable improvements in all unit
+// tests in general.
+var SnapdUnsafeIO bool = len(os.Args) > 0 && strings.HasSuffix(os.Args[0], ".test") && GetenvBool("SNAPD_UNSAFE_IO")
 
 // An AtomicWriter is an io.WriteCloser that has a Commit() method that does
 // whatever needs to be done so the modification is "atomic": an AtomicWriter
@@ -158,7 +159,7 @@ func (aw *atomicFile) Commit() error {
 	}
 
 	var dir *os.File
-	if !snapdUnsafeIO {
+	if !SnapdUnsafeIO {
 		// XXX: if go switches to use aio_fsync, we need to open the dir for writing
 		d, err := os.Open(filepath.Dir(aw.target))
 		if err != nil {
@@ -181,7 +182,7 @@ func (aw *atomicFile) Commit() error {
 	}
 	aw.renamed = true // it is now too late to Cancel()
 
-	if !snapdUnsafeIO {
+	if !SnapdUnsafeIO {
 		return dir.Sync()
 	}
 
Download as text