Merge branch 'upstream'
[gnulib.git] / tests / test-link.c
1 /* Test of link() function.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include <unistd.h>
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 #define BASE "test-link.t"
42
43 int
44 main (int argc, char **argv)
45 {
46   int fd;
47   int ret;
48
49   /* Remove any garbage left from previous partial runs.  */
50   ASSERT (system ("rm -rf " BASE "*") == 0);
51
52   /* Create first file.  */
53   fd = open (BASE "a", O_CREAT | O_EXCL | O_WRONLY, 0600);
54   ASSERT (0 <= fd);
55   ASSERT (write (fd, "hello", 5) == 5);
56   ASSERT (close (fd) == 0);
57
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");
64   if (!ret)
65   {
66     struct stat st;
67     ASSERT (stat (BASE "b", &st) == 0);
68     if (st.st_ino && st.st_nlink != 2)
69       {
70         ASSERT (unlink (BASE "b") == 0);
71         errno = EPERM;
72         ret = -1;
73       }
74   }
75   if (ret == -1)
76     {
77       /* If the device does not support hard links, errno is
78          EPERM on Linux, EOPNOTSUPP on FreeBSD.  */
79       switch (errno)
80         {
81         case EPERM:
82         case EOPNOTSUPP:
83           fputs ("skipping test: "
84                  "hard links are not supported on this file system\n", stderr);
85           ASSERT (unlink (BASE "a") == 0);
86           return 77;
87         default:
88           perror ("link");
89           return 1;
90         }
91     }
92   ASSERT (ret == 0);
93
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);
97   ASSERT (0 <= fd);
98   ASSERT (write (fd, "world", 5) == 5);
99   ASSERT (close (fd) == 0);
100   {
101     char buf[11] = { 0 };
102     fd = open (BASE "a", O_RDONLY);
103     ASSERT (0 <= fd);
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);
109     ASSERT (0 <= fd);
110     ASSERT (read (fd, buf, 10) == 10);
111     ASSERT (strcmp (buf, "helloworld") == 0);
112     ASSERT (close (fd) == 0);
113   }
114
115   /* Test for various error conditions.  Assumes hard links to
116      directories are not permitted.  */
117   ASSERT (mkdir (BASE "d", 0700) == 0);
118   errno = 0;
119   ASSERT (link (BASE "a", ".") == -1);
120   ASSERT (errno == EEXIST || errno == EINVAL);
121   errno = 0;
122   ASSERT (link (BASE "a", BASE "a") == -1);
123   ASSERT (errno == EEXIST);
124   ASSERT (link (BASE "a", BASE "b") == 0);
125   errno = 0;
126   ASSERT (link (BASE "a", BASE "b") == -1);
127   ASSERT (errno == EEXIST);
128   errno = 0;
129   ASSERT (link (BASE "a", BASE "d") == -1);
130   ASSERT (errno == EEXIST);
131   errno = 0;
132   ASSERT (link (BASE "c", BASE "e") == -1);
133   ASSERT (errno == ENOENT);
134   errno = 0;
135   ASSERT (link (BASE "a", BASE "c/.") == -1);
136   ASSERT (errno == ENOENT);
137   errno = 0;
138   ASSERT (link (BASE "a/", BASE "c") == -1);
139   ASSERT (errno == ENOTDIR);
140   errno = 0;
141   ASSERT (link (BASE "a", BASE "c/") == -1);
142   ASSERT (errno == ENOTDIR || errno == ENOENT);
143   errno = 0;
144   ASSERT (link (BASE "d", BASE "c") == -1);
145   ASSERT (errno == EPERM || errno == EACCES);
146
147   /* Clean up.  */
148   ASSERT (unlink (BASE "a") == 0);
149   ASSERT (unlink (BASE "b") == 0);
150   errno = 0;
151   ASSERT (unlink (BASE "c") == -1);
152   ASSERT (errno == ENOENT);
153   ASSERT (rmdir (BASE "d") == 0);
154
155   return 0;
156 }