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

Proposed by John Lenton
Status: Merged
Approved by: Sergio Schvezov
Approved revision: no longer in the source branch.
Merged at revision: 546
Proposed branch: lp:~chipaca/snappy/overwrite
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 52 lines (+23/-1)
2 files modified
helpers/cp.go (+8/-1)
helpers/cp_test.go (+15/-0)
To merge this branch: bzr merge lp:~chipaca/snappy/overwrite
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Review via email: mp+263584@code.launchpad.net

Commit message

Add CopyFileOverwrite flag and behaviour to helpers.CopyFile.

To post a comment you must log in.
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

\o/

review: Approve
lp:~chipaca/snappy/overwrite updated
546. By John Lenton

Add CopyFileOverwrite flag and behaviour to helpers.CopyFile. by chipaca approved by sergiusens

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'helpers/cp.go'
2--- helpers/cp.go 2015-05-15 15:38:17 +0000
3+++ helpers/cp.go 2015-07-01 20:52:02 +0000
4@@ -33,6 +33,8 @@
5 CopyFlagDefault CopyFlag = 0
6 // CopyFlagSync does a sync after copying the files
7 CopyFlagSync CopyFlag = 1 << iota
8+ // CopyFlagOverwrite overwrites the target if it exists
9+ CopyFlagOverwrite
10 )
11
12 var (
13@@ -70,7 +72,12 @@
14 return fmt.Errorf("unable to stat %s: %v", src, err)
15 }
16
17- fout, err := openfile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, fi.Mode())
18+ outflags := os.O_WRONLY | os.O_CREATE
19+ if flags&CopyFlagOverwrite == 0 {
20+ outflags |= os.O_EXCL
21+ }
22+
23+ fout, err := openfile(dst, outflags, fi.Mode())
24 if err != nil {
25 return fmt.Errorf("unable to create %s: %v", dst, err)
26 }
27
28=== modified file 'helpers/cp_test.go'
29--- helpers/cp_test.go 2015-06-02 20:46:07 +0000
30+++ helpers/cp_test.go 2015-07-01 20:52:02 +0000
31@@ -90,6 +90,21 @@
32 c.Check(bs, DeepEquals, s.data)
33 }
34
35+func (s *cpSuite) TestCpNoOverwrite(c *C) {
36+ _, err := os.Create(s.f2)
37+ c.Assert(err, IsNil)
38+ c.Check(CopyFile(s.f1, s.f2, CopyFlagDefault), NotNil)
39+}
40+
41+func (s *cpSuite) TestCpOverwrite(c *C) {
42+ _, err := os.Create(s.f2)
43+ c.Assert(err, IsNil)
44+ c.Check(CopyFile(s.f1, s.f2, CopyFlagOverwrite), IsNil)
45+ bs, err := ioutil.ReadFile(s.f2)
46+ c.Check(err, IsNil)
47+ c.Check(bs, DeepEquals, s.data)
48+}
49+
50 func (s *cpSuite) TestCpSync(c *C) {
51 s.mock()
52 c.Check(CopyFile(s.f1, s.f2, CopyFlagDefault), IsNil)

Subscribers

People subscribed via source and target branches