This source file includes following definitions.
- test_link
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 static int
25 test_link (int (*func) (char const *, char const *), bool print)
26 {
27 int fd;
28 int ret;
29
30
31 fd = open (BASE "a", O_CREAT | O_EXCL | O_WRONLY, 0600);
32 ASSERT (0 <= fd);
33 ASSERT (write (fd, "hello", 5) == 5);
34 ASSERT (close (fd) == 0);
35
36
37
38
39
40
41 ret = func (BASE "a", BASE "b");
42 if (!ret)
43 {
44 struct stat st;
45 ASSERT (stat (BASE "b", &st) == 0);
46 if (st.st_ino && st.st_nlink != 2)
47 {
48 ASSERT (unlink (BASE "b") == 0);
49 errno = EPERM;
50 ret = -1;
51 }
52 }
53 if (ret == -1)
54 {
55
56
57 switch (errno)
58 {
59 case EPERM:
60 case EOPNOTSUPP:
61 if (print)
62 fputs ("skipping test: "
63 "hard links not supported on this file system\n",
64 stderr);
65 ASSERT (unlink (BASE "a") == 0);
66 return 77;
67 default:
68 perror ("link");
69 return 1;
70 }
71 }
72 ASSERT (ret == 0);
73
74
75
76 fd = open (BASE "b", O_APPEND | O_WRONLY);
77 ASSERT (0 <= fd);
78 ASSERT (write (fd, "world", 5) == 5);
79 ASSERT (close (fd) == 0);
80 {
81 char buf[11] = { 0 };
82 fd = open (BASE "a", O_RDONLY);
83 ASSERT (0 <= fd);
84 ASSERT (read (fd, buf, 10) == 10);
85 ASSERT (strcmp (buf, "helloworld") == 0);
86 ASSERT (close (fd) == 0);
87 ASSERT (unlink (BASE "b") == 0);
88 fd = open (BASE "a", O_RDONLY);
89 ASSERT (0 <= fd);
90 ASSERT (read (fd, buf, 10) == 10);
91 ASSERT (strcmp (buf, "helloworld") == 0);
92 ASSERT (close (fd) == 0);
93 }
94
95
96 ASSERT (mkdir (BASE "d", 0700) == 0);
97 errno = 0;
98 ASSERT (func (BASE "a", ".") == -1);
99 ASSERT (errno == EEXIST || errno == EINVAL);
100 errno = 0;
101 ASSERT (func (BASE "a", BASE "a") == -1);
102 ASSERT (errno == EEXIST);
103 ASSERT (func (BASE "a", BASE "b") == 0);
104 errno = 0;
105 ASSERT (func (BASE "a", BASE "b") == -1);
106 ASSERT (errno == EEXIST);
107 errno = 0;
108 ASSERT (func (BASE "a", BASE "d") == -1);
109 ASSERT (errno == EEXIST);
110 errno = 0;
111 ASSERT (func (BASE "c", BASE "e") == -1);
112 ASSERT (errno == ENOENT);
113 errno = 0;
114 ASSERT (func (BASE "a", BASE "c/.") == -1);
115 ASSERT (errno == ENOENT);
116 errno = 0;
117 ASSERT (func (BASE "a/", BASE "c") == -1);
118 ASSERT (errno == ENOTDIR || errno == EINVAL);
119 errno = 0;
120 ASSERT (func (BASE "a", BASE "c/") == -1);
121 ASSERT (errno == ENOTDIR || errno == ENOENT || errno == EINVAL);
122
123
124
125
126
127 {
128 int result;
129 errno = 0;
130 result = func (BASE "d", BASE "c");
131 if (result == 0)
132 {
133
134 ASSERT (unlink (BASE "c") == 0);
135 }
136 else
137 {
138
139 ASSERT (errno == EPERM || errno == EACCES || errno == EISDIR);
140 errno = 0;
141 ASSERT (func (BASE "d/.", BASE "c") == -1);
142 ASSERT (errno == EPERM || errno == EACCES || errno == EISDIR
143 || errno == EINVAL);
144 errno = 0;
145 ASSERT (func (BASE "d/.//", BASE "c") == -1);
146 ASSERT (errno == EPERM || errno == EACCES || errno == EISDIR
147 || errno == EINVAL);
148 }
149 }
150 ASSERT (unlink (BASE "a") == 0);
151 errno = 0;
152 ASSERT (unlink (BASE "c") == -1);
153 ASSERT (errno == ENOENT);
154 ASSERT (rmdir (BASE "d") == 0);
155
156
157 if (symlink (BASE "a", BASE "link") != 0)
158 {
159 ASSERT (unlink (BASE "b") == 0);
160 if (print)
161 fputs ("skipping test: symlinks not supported on this file system\n",
162 stderr);
163 return 77;
164 }
165 errno = 0;
166 ASSERT (func (BASE "b", BASE "link/") == -1);
167 ASSERT (errno == ENOTDIR || errno == ENOENT || errno == EEXIST
168 || errno == EINVAL);
169 errno = 0;
170 ASSERT (func (BASE "b", BASE "link") == -1);
171 ASSERT (errno == EEXIST);
172 ASSERT (rename (BASE "b", BASE "a") == 0);
173 errno = 0;
174 ASSERT (func (BASE "link/", BASE "b") == -1);
175 ASSERT (errno == ENOTDIR || errno == EEXIST || errno == EINVAL);
176
177
178 ASSERT (unlink (BASE "a") == 0);
179 ASSERT (unlink (BASE "link") == 0);
180
181 return 0;
182 }