maint: adjust cpp indentation to reflect nesting depth
[gnulib.git] / lib / linkat.c
1 /* Create a hard link relative to open directories.
2    Copyright (C) 2009-2011 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 3 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 /* written by Eric Blake */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #include "areadlink.h"
32 #include "dirname.h"
33 #include "filenamecat.h"
34 #include "openat-priv.h"
35
36 #if HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifndef MAXSYMLINKS
40 # ifdef SYMLOOP_MAX
41 #  define MAXSYMLINKS SYMLOOP_MAX
42 # else
43 #  define MAXSYMLINKS 20
44 # endif
45 #endif
46
47 #if !HAVE_LINKAT
48
49 /* Create a link.  If FILE1 is a symlink, either create a hardlink to
50    that symlink, or fake it by creating an identical symlink.  */
51 # if LINK_FOLLOWS_SYMLINKS == 0
52 #  define link_immediate link
53 # else
54 static int
55 link_immediate (char const *file1, char const *file2)
56 {
57   char *target = areadlink (file1);
58   if (target)
59     {
60       /* A symlink cannot be modified in-place.  Therefore, creating
61          an identical symlink behaves like a hard link to a symlink,
62          except for incorrect st_ino and st_nlink.  However, we must
63          be careful of EXDEV.  */
64       struct stat st1;
65       struct stat st2;
66       char *dir = mdir_name (file2);
67       if (!dir)
68         {
69           free (target);
70           errno = ENOMEM;
71           return -1;
72         }
73       if (lstat (file1, &st1) == 0 && stat (dir, &st2) == 0)
74         {
75           if (st1.st_dev == st2.st_dev)
76             {
77               int result = symlink (target, file2);
78               int saved_errno = errno;
79               free (target);
80               free (dir);
81               errno = saved_errno;
82               return result;
83             }
84           free (target);
85           free (dir);
86           errno = EXDEV;
87           return -1;
88         }
89       free (target);
90       free (dir);
91     }
92   if (errno == ENOMEM)
93     return -1;
94   return link (file1, file2);
95 }
96 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
97
98 /* Create a link.  If FILE1 is a symlink, create a hardlink to the
99    canonicalized file.  */
100 # if 0 < LINK_FOLLOWS_SYMLINKS
101 #  define link_follow link
102 # else
103 static int
104 link_follow (char const *file1, char const *file2)
105 {
106   char *name = (char *) file1;
107   char *target;
108   int result;
109   int i = MAXSYMLINKS;
110
111   /* Using realpath or canonicalize_file_name is too heavy-handed: we
112      don't need an absolute name, and we don't need to resolve
113      intermediate symlinks, just the basename of each iteration.  */
114   while (i-- && (target = areadlink (name)))
115     {
116       if (IS_ABSOLUTE_FILE_NAME (target))
117         {
118           if (name != file1)
119             free (name);
120           name = target;
121         }
122       else
123         {
124           char *dir = mdir_name (name);
125           if (name != file1)
126             free (name);
127           if (!dir)
128             {
129               free (target);
130               errno = ENOMEM;
131               return -1;
132             }
133           name = mfile_name_concat (dir, target, NULL);
134           free (dir);
135           free (target);
136           if (!name)
137             {
138               errno = ENOMEM;
139               return -1;
140             }
141         }
142     }
143   if (i < 0)
144     {
145       target = NULL;
146       errno = ELOOP;
147     }
148   if (!target && errno != EINVAL)
149     {
150       if (name != file1)
151         {
152           int saved_errno = errno;
153           free (name);
154           errno = saved_errno;
155         }
156       return -1;
157     }
158   result = link (name, file2);
159   if (name != file1)
160     {
161       int saved_errno = errno;
162       free (name);
163       errno = saved_errno;
164     }
165   return result;
166 }
167 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
168
169 /* On Solaris, link() doesn't follow symlinks by default, but does so as soon
170    as a library or executable takes part in the program that has been compiled
171    with "c99" or "cc -xc99=all" or "cc ... /usr/lib/values-xpg4.o ...".  */
172 # if LINK_FOLLOWS_SYMLINKS == -1
173
174 /* Reduce the penalty of link_immediate and link_follow by incorporating the
175    knowledge that link()'s behaviour depends on the __xpg4 variable.  */
176 extern int __xpg4;
177
178 static int
179 solaris_optimized_link_immediate (char const *file1, char const *file2)
180 {
181   if (__xpg4 == 0)
182     return link (file1, file2);
183   return link_immediate (file1, file2);
184 }
185
186 static int
187 solaris_optimized_link_follow (char const *file1, char const *file2)
188 {
189   if (__xpg4 != 0)
190     return link (file1, file2);
191   return link_follow (file1, file2);
192 }
193
194 #  define link_immediate solaris_optimized_link_immediate
195 #  define link_follow solaris_optimized_link_follow
196
197 # endif
198
199 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
200    in the directory open on descriptor FD2.  If FILE1 is a symlink, FLAG
201    controls whether to dereference FILE1 first.  If possible, do it without
202    changing the working directory.  Otherwise, resort to using
203    save_cwd/fchdir, then rename/restore_cwd.  If either the save_cwd or
204    the restore_cwd fails, then give a diagnostic and exit nonzero.  */
205
206 int
207 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
208 {
209   if (flag & ~AT_SYMLINK_FOLLOW)
210     {
211       errno = EINVAL;
212       return -1;
213     }
214   return at_func2 (fd1, file1, fd2, file2,
215                    flag ? link_follow : link_immediate);
216 }
217
218 #else /* HAVE_LINKAT */
219
220 # undef linkat
221
222 /* Create a link.  If FILE1 is a symlink, create a hardlink to the
223    canonicalized file.  */
224
225 static int
226 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
227 {
228   char *name = (char *) file1;
229   char *target;
230   int result;
231   int i = MAXSYMLINKS;
232
233   /* There is no realpathat.  */
234   while (i-- && (target = areadlinkat (fd1, name)))
235     {
236       if (IS_ABSOLUTE_FILE_NAME (target))
237         {
238           if (name != file1)
239             free (name);
240           name = target;
241         }
242       else
243         {
244           char *dir = mdir_name (name);
245           if (name != file1)
246             free (name);
247           if (!dir)
248             {
249               free (target);
250               errno = ENOMEM;
251               return -1;
252             }
253           name = mfile_name_concat (dir, target, NULL);
254           free (dir);
255           free (target);
256           if (!name)
257             {
258               errno = ENOMEM;
259               return -1;
260             }
261         }
262     }
263   if (i < 0)
264     {
265       target = NULL;
266       errno = ELOOP;
267     }
268   if (!target && errno != EINVAL)
269     {
270       if (name != file1)
271         {
272           int saved_errno = errno;
273           free (name);
274           errno = saved_errno;
275         }
276       return -1;
277     }
278   result = linkat (fd1, name, fd2, file2, 0);
279   if (name != file1)
280     {
281       int saved_errno = errno;
282       free (name);
283       errno = saved_errno;
284     }
285   return result;
286 }
287
288
289 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
290    older Linux kernels.  */
291
292 int
293 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
294 {
295   if (flag & ~AT_SYMLINK_FOLLOW)
296     {
297       errno = EINVAL;
298       return -1;
299     }
300
301 # if LINKAT_TRAILING_SLASH_BUG
302   /* Reject trailing slashes on non-directories.  */
303   {
304     size_t len1 = strlen (file1);
305     size_t len2 = strlen (file2);
306     if ((len1 && file1[len1 - 1] == '/')
307         || (len2 && file2[len2 - 1] == '/'))
308       {
309         /* Let linkat() decide whether hard-linking directories is legal.
310            If fstatat() fails, then linkat() should fail for the same reason;
311            if fstatat() succeeds, require a directory.  */
312         struct stat st;
313         if (fstatat (fd1, file1, &st, flag ? 0 : AT_SYMLINK_NOFOLLOW))
314           return -1;
315         if (!S_ISDIR (st.st_mode))
316           {
317             errno = ENOTDIR;
318             return -1;
319           }
320       }
321   }
322 # endif
323
324   if (!flag)
325     return linkat (fd1, file1, fd2, file2, flag);
326
327   /* Cache the information on whether the system call really works.  */
328   {
329     static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
330     if (0 <= have_follow_really)
331     {
332       int result = linkat (fd1, file1, fd2, file2, flag);
333       if (!(result == -1 && errno == EINVAL))
334         {
335           have_follow_really = 1;
336           return result;
337         }
338       have_follow_really = -1;
339     }
340   }
341   return linkat_follow (fd1, file1, fd2, file2);
342 }
343
344 #endif /* HAVE_LINKAT */