1 /* Test of link() function.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #define ASSERT(expr) \
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 #define BASE "test-link.t"
44 main (int argc, char **argv)
49 /* Remove any garbage left from previous partial runs. */
50 ASSERT (system ("rm -rf " BASE "*") == 0);
52 /* Create first file. */
53 fd = open (BASE "a", O_CREAT | O_EXCL | O_WRONLY, 0600);
55 ASSERT (write (fd, "hello", 5) == 5);
56 ASSERT (close (fd) == 0);
58 /* Not all file systems support link. Mingw doesn't have reliable
59 st_nlink on hard links, but our implementation does fail with
60 EPERM on poor file systems, and we can detect the inferior stat()
61 via st_ino. Cygwin 1.5.x copies rather than links files on those
62 file systems, but there, st_nlink and st_ino are reliable. */
63 ret = link (BASE "a", BASE "b");
67 ASSERT (stat (BASE "b", &st) == 0);
68 if (st.st_ino && st.st_nlink != 2)
70 ASSERT (unlink (BASE "b") == 0);
77 /* If the device does not support hard links, errno is
78 EPERM on Linux, EOPNOTSUPP on FreeBSD. */
83 fputs ("skipping test: "
84 "hard links are not supported on this file system\n", stderr);
85 ASSERT (unlink (BASE "a") == 0);
94 /* Now, for some behavior tests. Modify the contents of 'b', and
95 ensure that 'a' can see it, both while 'b' exists and after. */
96 fd = open (BASE "b", O_APPEND | O_WRONLY);
98 ASSERT (write (fd, "world", 5) == 5);
99 ASSERT (close (fd) == 0);
101 char buf[11] = { 0 };
102 fd = open (BASE "a", O_RDONLY);
104 ASSERT (read (fd, buf, 10) == 10);
105 ASSERT (strcmp (buf, "helloworld") == 0);
106 ASSERT (close (fd) == 0);
107 ASSERT (unlink (BASE "b") == 0);
108 fd = open (BASE "a", O_RDONLY);
110 ASSERT (read (fd, buf, 10) == 10);
111 ASSERT (strcmp (buf, "helloworld") == 0);
112 ASSERT (close (fd) == 0);
115 /* Test for various error conditions. Assumes hard links to
116 directories are not permitted. */
117 ASSERT (mkdir (BASE "d", 0700) == 0);
119 ASSERT (link (BASE "a", ".") == -1);
120 ASSERT (errno == EEXIST || errno == EINVAL);
122 ASSERT (link (BASE "a", BASE "a") == -1);
123 ASSERT (errno == EEXIST);
124 ASSERT (link (BASE "a", BASE "b") == 0);
126 ASSERT (link (BASE "a", BASE "b") == -1);
127 ASSERT (errno == EEXIST);
129 ASSERT (link (BASE "a", BASE "d") == -1);
130 ASSERT (errno == EEXIST);
132 ASSERT (link (BASE "c", BASE "e") == -1);
133 ASSERT (errno == ENOENT);
135 ASSERT (link (BASE "a", BASE "c/.") == -1);
136 ASSERT (errno == ENOENT);
138 ASSERT (link (BASE "a/", BASE "c") == -1);
139 ASSERT (errno == ENOTDIR);
141 ASSERT (link (BASE "a", BASE "c/") == -1);
142 ASSERT (errno == ENOTDIR);
144 ASSERT (link (BASE "d", BASE "c") == -1);
145 ASSERT (errno == EPERM || errno == EACCES);
148 ASSERT (unlink (BASE "a") == 0);
149 ASSERT (unlink (BASE "b") == 0);
151 ASSERT (unlink (BASE "c") == -1);
152 ASSERT (errno == ENOENT);
153 ASSERT (rmdir (BASE "d") == 0);