Browse code

Support FreeBSD on pkg/system/utimes_*.go

Implement system.LUtimesNano and system.UtimesNano. The latter might be
removed in future because it's basically same as os.Chtimes. That's why
the test is mainly focusing LUtimesNano.

Docker-DCO-1.1-Signed-off-by: Kato Kazuyoshi <kato.kazuyoshi@gmail.com> (github: kzys)

Kato Kazuyoshi authored on 2014/03/21 00:52:00
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,24 @@
0
+package system
1
+
2
+import (
3
+	"syscall"
4
+	"unsafe"
5
+)
6
+
7
+func LUtimesNano(path string, ts []syscall.Timespec) error {
8
+	var _path *byte
9
+	_path, err := syscall.BytePtrFromString(path)
10
+	if err != nil {
11
+		return err
12
+	}
13
+
14
+	if _, _, err := syscall.Syscall(syscall.SYS_LUTIMES, uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(&ts[0])), 0); err != 0 && err != syscall.ENOSYS {
15
+		return err
16
+	}
17
+
18
+	return nil
19
+}
20
+
21
+func UtimesNano(path string, ts []syscall.Timespec) error {
22
+	return syscall.UtimesNano(path, ts)
23
+}
0 24
new file mode 100644
... ...
@@ -0,0 +1,64 @@
0
+package system
1
+
2
+import (
3
+	"io/ioutil"
4
+	"os"
5
+	"path/filepath"
6
+	"syscall"
7
+	"testing"
8
+)
9
+
10
+func prepareFiles(t *testing.T) (string, string, string) {
11
+	dir, err := ioutil.TempDir("", "docker-system-test")
12
+	if err != nil {
13
+		t.Fatal(err)
14
+	}
15
+
16
+	file := filepath.Join(dir, "exist")
17
+	if err := ioutil.WriteFile(file, []byte("hello"), 0644); err != nil {
18
+		t.Fatal(err)
19
+	}
20
+
21
+	invalid := filepath.Join(dir, "doesnt-exist")
22
+
23
+	symlink := filepath.Join(dir, "symlink")
24
+	if err := os.Symlink(file, symlink); err != nil {
25
+		t.Fatal(err)
26
+	}
27
+
28
+	return file, invalid, symlink
29
+}
30
+
31
+func TestLUtimesNano(t *testing.T) {
32
+	file, invalid, symlink := prepareFiles(t)
33
+
34
+	before, err := os.Stat(file)
35
+	if err != nil {
36
+		t.Fatal(err)
37
+	}
38
+
39
+	ts := []syscall.Timespec{{0, 0}, {0, 0}}
40
+	if err := LUtimesNano(symlink, ts); err != nil {
41
+		t.Fatal(err)
42
+	}
43
+
44
+	symlinkInfo, err := os.Lstat(symlink)
45
+	if err != nil {
46
+		t.Fatal(err)
47
+	}
48
+	if before.ModTime().Unix() == symlinkInfo.ModTime().Unix() {
49
+		t.Fatal("The modification time of the symlink should be different")
50
+	}
51
+
52
+	fileInfo, err := os.Stat(file)
53
+	if err != nil {
54
+		t.Fatal(err)
55
+	}
56
+	if before.ModTime().Unix() != fileInfo.ModTime().Unix() {
57
+		t.Fatal("The modification time of the file should be same")
58
+	}
59
+
60
+	if err := LUtimesNano(invalid, ts); err == nil {
61
+		t.Fatal("Doesn't return an error on a non-existing file")
62
+	}
63
+}
... ...
@@ -1,4 +1,4 @@
1
-// +build !linux
1
+// +build !linux,!freebsd
2 2
 
3 3
 package system
4 4