Debian Codegraph @

internal/rsynctest/rsynctest_unix.go

raw symbols SCIP
1 //go:build !windows
2
3 package rsynctest
4
5 import (
6 "net"
7 "os"
8 "path/filepath"
9 "syscall"
10 "testing"
11
12 "github.com/google/renameio/v2"
13 "golang.org/x/sys/unix"
14 )
15
16 func CreateDummyDeviceFiles(t *testing.T, dir string) {
17 if err := os.MkdirAll(dir, 0755); err != nil {
18 t.Fatal(err)
19 }
20
21 char := filepath.Join(dir, "char")
22 // major 1, minor 5, like /dev/zero
23 if err := unix.Mknod(char, 0600|syscall.S_IFCHR, int(unix.Mkdev(1, 5))); err != nil {
24 t.Fatal(err)
25 }
26
27 block := filepath.Join(dir, "block")
28 // major 242, minor 9, like /dev/nvme0
29 if err := unix.Mknod(block, 0600|syscall.S_IFBLK, int(unix.Mkdev(242, 9))); err != nil {
30 t.Fatal(err)
31 }
32
33 fifo := filepath.Join(dir, "fifo")
34 if err := unix.Mkfifo(fifo, 0600); err != nil {
35 t.Fatal(err)
36 }
37
38 sock := filepath.Join(dir, "sock")
39 ln, err := net.Listen("unix", sock)
40 if err != nil {
41 t.Fatal(err)
42 }
43 t.Cleanup(func() { ln.Close() })
44 }
45
46 func VerifyDummyDeviceFiles(t *testing.T, source, dest string) {
47 {
48 sourcest, err := os.Stat(filepath.Join(source, "char"))
49 if err != nil {
50 t.Fatal(err)
51 }
52 destst, err := os.Stat(filepath.Join(dest, "char"))
53 if err != nil {
54 t.Fatal(err)
55 }
56 if destst.Mode().Type()&os.ModeCharDevice == 0 {
57 t.Fatalf("unexpected type: got %v, want character device", destst.Mode())
58 }
59 destsys, ok := destst.Sys().(*syscall.Stat_t)
60 if !ok {
61 t.Fatal("stat does not contain rdev")
62 }
63 sourcesys, ok := sourcest.Sys().(*syscall.Stat_t)
64 if !ok {
65 t.Fatal("stat does not contain rdev")
66 }
67 if got, want := destsys.Rdev, sourcesys.Rdev; got != want {
68 t.Fatalf("unexpected rdev: got %v, want %v", got, want)
69 }
70 }
71
72 {
73 sourcest, err := os.Stat(filepath.Join(source, "block"))
74 if err != nil {
75 t.Fatal(err)
76 }
77 destst, err := os.Stat(filepath.Join(dest, "block"))
78 if err != nil {
79 t.Fatal(err)
80 }
81 if destst.Mode().Type()&os.ModeDevice == 0 ||
82 destst.Mode().Type()&os.ModeCharDevice != 0 {
83 t.Fatalf("unexpected type: got %v, want block device", destst.Mode())
84 }
85 destsys, ok := destst.Sys().(*syscall.Stat_t)
86 if !ok {
87 t.Fatal("stat does not contain rdev")
88 }
89 sourcesys, ok := sourcest.Sys().(*syscall.Stat_t)
90 if !ok {
91 t.Fatal("stat does not contain rdev")
92 }
93 if got, want := destsys.Rdev, sourcesys.Rdev; got != want {
94 t.Fatalf("unexpected rdev: got %v, want %v", got, want)
95 }
96 }
97
98 {
99 st, err := os.Stat(filepath.Join(dest, "fifo"))
100 if err != nil {
101 t.Fatal(err)
102 }
103 if st.Mode().Type()&os.ModeNamedPipe == 0 {
104 t.Fatalf("unexpected type: got %v, want fifo", st.Mode())
105 }
106 }
107
108 {
109 st, err := os.Stat(filepath.Join(dest, "sock"))
110 if err != nil {
111 t.Fatal(err)
112 }
113 if st.Mode().Type()&os.ModeSocket == 0 {
114 t.Fatalf("unexpected type: got %v, want socket", st.Mode())
115 }
116 }
117 }
118
119 // StatUidGid returns the owner uid and gid backing fi.
120 //
121 // This check is encapsulated in StatUidGid so that
122 // on Windows, we can fail loudly (the check should be skipped).
123 func StatUidGid(t *testing.T, fi os.FileInfo) (uid, gid int) {
124 stt, ok := fi.Sys().(*syscall.Stat_t)
125 if !ok {
126 t.Fatalf("FileInfo.Sys() is %T, want *syscall.Stat_t", fi.Sys())
127 }
128 return int(stt.Uid), int(stt.Gid)
129 }
130
131 // ReplaceSymlink atomically replaces newname with a symlink to oldname.
132 //
133 // On Unix, this function works atomically,
134 // on Windows it falls back to non-atomic behavior.
135 func ReplaceSymlink(t *testing.T, oldname, newname string) {
136 t.Helper()
137 if err := renameio.Symlink(oldname, newname); err != nil {
138 t.Fatal(err)
139 }
140 }