annotate lib/linkat.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Create a hard link relative to open directories.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* written by Eric Blake */
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <unistd.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include <errno.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include <fcntl.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include <limits.h>
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
26 #include <stdlib.h>
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
27 #include <string.h>
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 #include <sys/stat.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 #include "areadlink.h"
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 #include "dirname.h"
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 #include "filenamecat.h"
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 #include "openat-priv.h"
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 #if HAVE_SYS_PARAM_H
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 # include <sys/param.h>
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 #endif
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38 #ifndef MAXSYMLINKS
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 # ifdef SYMLOOP_MAX
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 # define MAXSYMLINKS SYMLOOP_MAX
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 # else
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 # define MAXSYMLINKS 20
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 # endif
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 #endif
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45
17787
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
46 #if !HAVE_LINKAT || LINKAT_SYMLINK_NOTSUP
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
47
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48 /* Create a link. If FILE1 is a symlink, either create a hardlink to
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 that symlink, or fake it by creating an identical symlink. */
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
50 # if LINK_FOLLOWS_SYMLINKS == 0
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
51 # define link_immediate link
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
52 # else
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 static int
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 link_immediate (char const *file1, char const *file2)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 char *target = areadlink (file1);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57 if (target)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 /* A symlink cannot be modified in-place. Therefore, creating
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 an identical symlink behaves like a hard link to a symlink,
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 except for incorrect st_ino and st_nlink. However, we must
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 be careful of EXDEV. */
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 struct stat st1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 struct stat st2;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 char *dir = mdir_name (file2);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 if (!dir)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 errno = ENOMEM;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
70 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 if (lstat (file1, &st1) == 0 && stat (dir, &st2) == 0)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 if (st1.st_dev == st2.st_dev)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 int result = symlink (target, file2);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 int saved_errno = errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 free (dir);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 errno = saved_errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
81 return result;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 free (dir);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 errno = EXDEV;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
86 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
87 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
88 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
89 free (dir);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
90 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
91 if (errno == ENOMEM)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
92 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
93 return link (file1, file2);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
94 }
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
95 # endif /* LINK_FOLLOWS_SYMLINKS == 0 */
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
96
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
97 /* Create a link. If FILE1 is a symlink, create a hardlink to the
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
98 canonicalized file. */
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
99 # if 0 < LINK_FOLLOWS_SYMLINKS
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
100 # define link_follow link
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
101 # else
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
102 static int
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
103 link_follow (char const *file1, char const *file2)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
104 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
105 char *name = (char *) file1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
106 char *target;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
107 int result;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
108 int i = MAXSYMLINKS;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
109
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
110 /* Using realpath or canonicalize_file_name is too heavy-handed: we
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
111 don't need an absolute name, and we don't need to resolve
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
112 intermediate symlinks, just the basename of each iteration. */
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
113 while (i-- && (target = areadlink (name)))
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
114 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
115 if (IS_ABSOLUTE_FILE_NAME (target))
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
116 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
117 if (name != file1)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
118 free (name);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
119 name = target;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
120 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
121 else
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
122 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
123 char *dir = mdir_name (name);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
124 if (name != file1)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
125 free (name);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
126 if (!dir)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
127 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
128 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
129 errno = ENOMEM;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
130 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
131 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
132 name = mfile_name_concat (dir, target, NULL);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
133 free (dir);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
134 free (target);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
135 if (!name)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
136 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
137 errno = ENOMEM;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
138 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
139 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
140 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
141 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
142 if (i < 0)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
143 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
144 target = NULL;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
145 errno = ELOOP;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
146 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
147 if (!target && errno != EINVAL)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
148 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
149 if (name != file1)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
150 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
151 int saved_errno = errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
152 free (name);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
153 errno = saved_errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
154 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
155 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
156 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
157 result = link (name, file2);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
158 if (name != file1)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
159 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
160 int saved_errno = errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
161 free (name);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
162 errno = saved_errno;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
163 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
164 return result;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
165 }
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
166 # endif /* 0 < LINK_FOLLOWS_SYMLINKS */
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
167
14050
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
168 /* On Solaris, link() doesn't follow symlinks by default, but does so as soon
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
169 as a library or executable takes part in the program that has been compiled
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
170 with "c99" or "cc -xc99=all" or "cc ... /usr/lib/values-xpg4.o ...". */
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
171 # if LINK_FOLLOWS_SYMLINKS == -1
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
172
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
173 /* Reduce the penalty of link_immediate and link_follow by incorporating the
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
174 knowledge that link()'s behaviour depends on the __xpg4 variable. */
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
175 extern int __xpg4;
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
176
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
177 static int
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
178 solaris_optimized_link_immediate (char const *file1, char const *file2)
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
179 {
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
180 if (__xpg4 == 0)
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
181 return link (file1, file2);
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
182 return link_immediate (file1, file2);
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
183 }
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
184
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
185 static int
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
186 solaris_optimized_link_follow (char const *file1, char const *file2)
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
187 {
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
188 if (__xpg4 != 0)
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
189 return link (file1, file2);
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
190 return link_follow (file1, file2);
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
191 }
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
192
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
193 # define link_immediate solaris_optimized_link_immediate
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
194 # define link_follow solaris_optimized_link_follow
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
195
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
196 # endif
6566818713fe linkat: Make implementation robust against system behaviour variations.
Bruno Haible <bruno@clisp.org>
parents: 13514
diff changeset
197
17787
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
198 #endif /* !HAVE_LINKAT || LINKAT_SYMLINK_NOTSUP */
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
199
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
200 #if !HAVE_LINKAT
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
201
12068
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
202 /* Create a link to FILE1, in the directory open on descriptor FD1, to FILE2,
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
203 in the directory open on descriptor FD2. If FILE1 is a symlink, FLAG
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
204 controls whether to dereference FILE1 first. If possible, do it without
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
205 changing the working directory. Otherwise, resort to using
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
206 save_cwd/fchdir, then rename/restore_cwd. If either the save_cwd or
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
207 the restore_cwd fails, then give a diagnostic and exit nonzero. */
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
208
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
209 int
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
210 linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
211 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
212 if (flag & ~AT_SYMLINK_FOLLOW)
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
213 {
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
214 errno = EINVAL;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
215 return -1;
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
216 }
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
217 return at_func2 (fd1, file1, fd2, file2,
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
218 flag ? link_follow : link_immediate);
14cddb59cb05 linkat: new module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
219 }
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
220
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
221 #else /* HAVE_LINKAT */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
222
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
223 # undef linkat
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
224
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
225 /* Create a link. If FILE1 is a symlink, create a hardlink to the
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
226 canonicalized file. */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
227
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
228 static int
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
229 linkat_follow (int fd1, char const *file1, int fd2, char const *file2)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
230 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
231 char *name = (char *) file1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
232 char *target;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
233 int result;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
234 int i = MAXSYMLINKS;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
235
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
236 /* There is no realpathat. */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
237 while (i-- && (target = areadlinkat (fd1, name)))
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
238 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
239 if (IS_ABSOLUTE_FILE_NAME (target))
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
240 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
241 if (name != file1)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
242 free (name);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
243 name = target;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
244 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
245 else
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
246 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
247 char *dir = mdir_name (name);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
248 if (name != file1)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
249 free (name);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
250 if (!dir)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
251 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
252 free (target);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
253 errno = ENOMEM;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
254 return -1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
255 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
256 name = mfile_name_concat (dir, target, NULL);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
257 free (dir);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
258 free (target);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
259 if (!name)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
260 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
261 errno = ENOMEM;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
262 return -1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
263 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
264 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
265 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
266 if (i < 0)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
267 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
268 target = NULL;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
269 errno = ELOOP;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
270 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
271 if (!target && errno != EINVAL)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
272 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
273 if (name != file1)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
274 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
275 int saved_errno = errno;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
276 free (name);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
277 errno = saved_errno;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
278 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
279 return -1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
280 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
281 result = linkat (fd1, name, fd2, file2, 0);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
282 if (name != file1)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
283 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
284 int saved_errno = errno;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
285 free (name);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
286 errno = saved_errno;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
287 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
288 return result;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
289 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
290
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
291
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
292 /* Like linkat, but guarantee that AT_SYMLINK_FOLLOW works even on
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
293 older Linux kernels. */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
294
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
295 int
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
296 rpl_linkat (int fd1, char const *file1, int fd2, char const *file2, int flag)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
297 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
298 if (flag & ~AT_SYMLINK_FOLLOW)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
299 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
300 errno = EINVAL;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
301 return -1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
302 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
303
14379
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
304 # if LINKAT_TRAILING_SLASH_BUG
13514
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
305 /* Reject trailing slashes on non-directories. */
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
306 {
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
307 size_t len1 = strlen (file1);
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
308 size_t len2 = strlen (file2);
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
309 if ((len1 && file1[len1 - 1] == '/')
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
310 || (len2 && file2[len2 - 1] == '/'))
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
311 {
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
312 /* Let linkat() decide whether hard-linking directories is legal.
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
313 If fstatat() fails, then linkat() should fail for the same reason;
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
314 if fstatat() succeeds, require a directory. */
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
315 struct stat st;
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
316 if (fstatat (fd1, file1, &st, flag ? 0 : AT_SYMLINK_NOFOLLOW))
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
317 return -1;
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
318 if (!S_ISDIR (st.st_mode))
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
319 {
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
320 errno = ENOTDIR;
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
321 return -1;
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
322 }
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
323 }
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
324 }
14379
2330aac2ae54 maint: adjust cpp indentation to reflect nesting depth
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
325 # endif
13514
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
326
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
327 if (!flag)
17787
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
328 {
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
329 int result = linkat (fd1, file1, fd2, file2, flag);
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
330 # if LINKAT_SYMLINK_NOTSUP
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
331 /* OS X 10.10 has linkat() but it doesn't support
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
332 hardlinks to symlinks. Fallback to our emulation
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
333 in that case. */
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
334 if (result == -1 && (errno == ENOTSUP || errno == EOPNOTSUPP))
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
335 return at_func2 (fd1, file1, fd2, file2, link_immediate);
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
336 # endif
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
337 return result;
461b92d0b2eb linkat: wrap to handle symlinks on OS X 10.10
Pádraig Brady <P@draigBrady.com>
parents: 17576
diff changeset
338 }
13514
6154fce586b6 linkat: Work around AIX 7.1 bug.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
339
12120
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
340 /* Cache the information on whether the system call really works. */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
341 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
342 static int have_follow_really; /* 0 = unknown, 1 = yes, -1 = no */
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
343 if (0 <= have_follow_really)
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
344 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
345 int result = linkat (fd1, file1, fd2, file2, flag);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
346 if (!(result == -1 && errno == EINVAL))
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
347 {
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
348 have_follow_really = 1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
349 return result;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
350 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
351 have_follow_really = -1;
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
352 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
353 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
354 return linkat_follow (fd1, file1, fd2, file2);
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
355 }
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
356
0bc26baea4bf linkat: support Linux 2.6.17
Eric Blake <ebb9@byu.net>
parents: 12068
diff changeset
357 #endif /* HAVE_LINKAT */