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

Proposed by John Lenton
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 329
Merged at revision: 340
Proposed branch: lp:~chipaca/snappy/touch
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~chipaca/snappy/remove-framework-aborts-on-dependent-snap
Diff against target: 140 lines (+131/-0)
2 files modified
helpers/touch.go (+57/-0)
helpers/touch_test.go (+74/-0)
To merge this branch: bzr merge lp:~chipaca/snappy/touch
Reviewer Review Type Date Requested Status
Sergio Schvezov Approve
Michael Vogt (community) Needs Information
Review via email: mp+255650@code.launchpad.net

Commit message

Added helpers.Touch, for `touch -c -h` functionality.

To post a comment you must log in.
Revision history for this message
Michael Vogt (mvo) wrote :

The code is nice but whats the use-case :) ?

review: Needs Information
Revision history for this message
John Lenton (chipaca) wrote :

Moved to WIP so I can refactor to follow jodh's approach wrt isatty.

Revision history for this message
John Lenton (chipaca) wrote :

Use case is to touch the security symlinks before calling aa-clickwhatsit to regenerate & reload the security thingiemabobs.

Revision history for this message
John Lenton (chipaca) wrote :

Changed my mind, keeping as is.

lp:~chipaca/snappy/touch updated
327. By John Lenton

Check for dependents on framework remove. by chipaca approved by mvo

328. By John Lenton

merged trunk

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

Its very nice, one minor comment on the naming.

Revision history for this message
John Lenton (chipaca) wrote :

I know how bad I am at naming things, so all naming suggestions are accepted :)

Unless I think they're worse, of course :-p

lp:~chipaca/snappy/touch updated
329. By John Lenton

renamed helpers.Touch to helpers.UpdateTimestamp

Revision history for this message
John Lenton (chipaca) wrote :

Done.

Revision history for this message
Sergio Schvezov (sergiusens) wrote :

one minor thing

review: Needs Fixing
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

sorry, wrong mind set when looking at this.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'helpers/touch.go'
2--- helpers/touch.go 1970-01-01 00:00:00 +0000
3+++ helpers/touch.go 2015-04-13 17:01:37 +0000
4@@ -0,0 +1,57 @@
5+/*
6+ * Copyright (C) 2014-2015 Canonical Ltd
7+ *
8+ * This program is free software: you can redistribute it and/or modify
9+ * it under the terms of the GNU General Public License version 3 as
10+ * published by the Free Software Foundation.
11+ *
12+ * This program is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU General Public License
18+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19+ *
20+ */
21+
22+package helpers
23+
24+/*
25+#include <fcntl.h> // for AT_*
26+#include <sys/stat.h>
27+#include <stdlib.h> // for free()
28+
29+int touch(const char *pathname) {
30+ // the first 0 is the dirfd, which is ignored if pathname is absolute
31+ // the second 0 is a null pointer => use current time
32+ return utimensat(0, pathname, 0, AT_SYMLINK_NOFOLLOW);
33+}
34+*/
35+import "C"
36+
37+import (
38+ "errors"
39+ "path/filepath"
40+ "unsafe"
41+)
42+
43+var ErrNotAbsPath = errors.New("not an absolute path")
44+
45+// UpdateTimestamp updates the timestamp of the file at pathname. It does not
46+// create it if it does not exist. It does not dereference it if it is a
47+// symlink. It's like `touch -c -h pathname`.
48+//
49+// pathname must be absolute.
50+func UpdateTimestamp(pathname string) error {
51+ if !filepath.IsAbs(pathname) {
52+ return ErrNotAbsPath
53+ }
54+
55+ p := C.CString(pathname)
56+ defer C.free(unsafe.Pointer(p))
57+
58+ _, e := C.touch(p)
59+
60+ return e
61+}
62
63=== added file 'helpers/touch_test.go'
64--- helpers/touch_test.go 1970-01-01 00:00:00 +0000
65+++ helpers/touch_test.go 2015-04-13 17:01:37 +0000
66@@ -0,0 +1,74 @@
67+/*
68+ * Copyright (C) 2014-2015 Canonical Ltd
69+ *
70+ * This program is free software: you can redistribute it and/or modify
71+ * it under the terms of the GNU General Public License version 3 as
72+ * published by the Free Software Foundation.
73+ *
74+ * This program is distributed in the hope that it will be useful,
75+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
76+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77+ * GNU General Public License for more details.
78+ *
79+ * You should have received a copy of the GNU General Public License
80+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
81+ *
82+ */
83+
84+package helpers
85+
86+import (
87+ "os"
88+ "path/filepath"
89+ "time"
90+
91+ . "launchpad.net/gocheck"
92+)
93+
94+func (ts *HTestSuite) TestUpdateTimestamp(c *C) {
95+ // empirical
96+ minRes := 25 * time.Millisecond
97+
98+ d := c.MkDir()
99+ foo := filepath.Join(d, "foo")
100+ bar := filepath.Join(d, "bar")
101+ c.Assert(os.Symlink(foo, bar), IsNil)
102+ time.Sleep(minRes)
103+ c.Assert(os.Mkdir(foo, 0755), IsNil)
104+ // so now foo (the dir) has a timestamp after bar's (the symlink)
105+ // let's check that:
106+ fifoo, err := os.Lstat(foo)
107+ c.Assert(err, IsNil)
108+ fibar, err := os.Lstat(bar)
109+ c.Assert(err, IsNil)
110+ c.Assert(fifoo.ModTime().After(fibar.ModTime()), Equals, true)
111+ // and bar is a symlink to foo
112+ fifoox, err := os.Stat(bar)
113+ c.Assert(err, IsNil)
114+ c.Assert(fifoo.ModTime(), Equals, fifoox.ModTime())
115+ // ok.
116+ time.Sleep(minRes)
117+
118+ // this should update bar's timestamp to be newer than foo's
119+ // (even though bar is a symlink to foo)
120+ c.Assert(UpdateTimestamp(bar), IsNil)
121+
122+ fifoo, err = os.Lstat(foo)
123+ c.Assert(err, IsNil)
124+ fibar, err = os.Lstat(bar)
125+ c.Assert(err, IsNil)
126+ c.Assert(fifoo.ModTime().Before(fibar.ModTime()), Equals, true)
127+}
128+
129+func (ts *HTestSuite) TestUpdateTimestampDoesNotCreate(c *C) {
130+ d := c.MkDir()
131+ foo := filepath.Join(d, "foo")
132+
133+ c.Check(UpdateTimestamp(foo), NotNil)
134+ _, err := os.Stat(foo)
135+ c.Check(os.IsNotExist(err), Equals, true)
136+}
137+
138+func (ts *HTestSuite) TestUpdateTimestampBailsOnRelative(c *C) {
139+ c.Check(UpdateTimestamp("./foo"), Equals, ErrNotAbsPath)
140+}

Subscribers

People subscribed via source and target branches