From 6b292bf03472b55f5d5c080d7de0291bd4a994b0 Mon Sep 17 00:00:00 2001 From: Kato Kazuyoshi Date: Fri, 21 Mar 2014 00:52:00 +0900 Subject: [PATCH] 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 (github: kzys) --- system/utimes_freebsd.go | 24 ++++++++++++++ system/utimes_test.go | 64 ++++++++++++++++++++++++++++++++++++ system/utimes_unsupported.go | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 system/utimes_freebsd.go create mode 100644 system/utimes_test.go diff --git a/system/utimes_freebsd.go b/system/utimes_freebsd.go new file mode 100644 index 0000000..ceaa044 --- /dev/null +++ b/system/utimes_freebsd.go @@ -0,0 +1,24 @@ +package system + +import ( + "syscall" + "unsafe" +) + +func LUtimesNano(path string, ts []syscall.Timespec) error { + var _path *byte + _path, err := syscall.BytePtrFromString(path) + if err != nil { + return err + } + + if _, _, err := syscall.Syscall(syscall.SYS_LUTIMES, uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(&ts[0])), 0); err != 0 && err != syscall.ENOSYS { + return err + } + + return nil +} + +func UtimesNano(path string, ts []syscall.Timespec) error { + return syscall.UtimesNano(path, ts) +} diff --git a/system/utimes_test.go b/system/utimes_test.go new file mode 100644 index 0000000..38e4020 --- /dev/null +++ b/system/utimes_test.go @@ -0,0 +1,64 @@ +package system + +import ( + "io/ioutil" + "os" + "path/filepath" + "syscall" + "testing" +) + +func prepareFiles(t *testing.T) (string, string, string) { + dir, err := ioutil.TempDir("", "docker-system-test") + if err != nil { + t.Fatal(err) + } + + file := filepath.Join(dir, "exist") + if err := ioutil.WriteFile(file, []byte("hello"), 0644); err != nil { + t.Fatal(err) + } + + invalid := filepath.Join(dir, "doesnt-exist") + + symlink := filepath.Join(dir, "symlink") + if err := os.Symlink(file, symlink); err != nil { + t.Fatal(err) + } + + return file, invalid, symlink +} + +func TestLUtimesNano(t *testing.T) { + file, invalid, symlink := prepareFiles(t) + + before, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } + + ts := []syscall.Timespec{{0, 0}, {0, 0}} + if err := LUtimesNano(symlink, ts); err != nil { + t.Fatal(err) + } + + symlinkInfo, err := os.Lstat(symlink) + if err != nil { + t.Fatal(err) + } + if before.ModTime().Unix() == symlinkInfo.ModTime().Unix() { + t.Fatal("The modification time of the symlink should be different") + } + + fileInfo, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } + if before.ModTime().Unix() != fileInfo.ModTime().Unix() { + t.Fatal("The modification time of the file should be same") + } + + if err := LUtimesNano(invalid, ts); err == nil { + t.Fatal("Doesn't return an error on a non-existing file") + } +} diff --git a/system/utimes_unsupported.go b/system/utimes_unsupported.go index d247ba2..9a8cf9c 100644 --- a/system/utimes_unsupported.go +++ b/system/utimes_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux +// +build !linux,!freebsd package system