2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux || freebsd
|
2015-10-01 13:45:32 -04:00
|
|
|
// +build linux freebsd
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2014-03-20 11:52:00 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-31 04:03:31 -04:00
|
|
|
// prepareFiles creates files for testing in the temp directory
|
2014-11-20 12:33:15 -05:00
|
|
|
func prepareFiles(t *testing.T) (string, string, string, string) {
|
2021-08-24 06:10:50 -04:00
|
|
|
dir, err := os.MkdirTemp("", "docker-system-test")
|
2014-03-20 11:52:00 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file := filepath.Join(dir, "exist")
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(file, []byte("hello"), 0644); err != nil {
|
2014-03-20 11:52:00 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-11-20 12:33:15 -05:00
|
|
|
return file, invalid, symlink, dir
|
2014-03-20 11:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLUtimesNano(t *testing.T) {
|
2014-11-20 12:33:15 -05:00
|
|
|
file, invalid, symlink, dir := prepareFiles(t)
|
|
|
|
defer os.RemoveAll(dir)
|
2014-03-20 11:52:00 -04:00
|
|
|
|
|
|
|
before, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:06:50 -05:00
|
|
|
ts := []syscall.Timespec{{Sec: 0, Nsec: 0}, {Sec: 0, Nsec: 0}}
|
2014-03-20 11:52:00 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|