Commit | Line | Data |
---|---|---|
98399780 | 1 | /* Return the canonical absolute name of a given file. |
49e4877c | 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. |
98399780 YQ |
3 | This file is part of the GNU C Library. |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #ifndef _LIBC | |
4a626d0a PA |
19 | /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc |
20 | optimizes away the name == NULL test below. */ | |
21 | # define _GL_ARG_NONNULL(params) | |
22 | ||
98399780 YQ |
23 | # define _GL_USE_STDLIB_ALLOC 1 |
24 | # include <config.h> | |
25 | #endif | |
26 | ||
27 | #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC | |
28 | ||
98399780 YQ |
29 | /* Specification. */ |
30 | #include <stdlib.h> | |
31 | ||
32 | #include <alloca.h> | |
33 | #include <string.h> | |
34 | #include <unistd.h> | |
35 | #include <limits.h> | |
36 | #if HAVE_SYS_PARAM_H || defined _LIBC | |
37 | # include <sys/param.h> | |
38 | #endif | |
39 | #include <sys/stat.h> | |
40 | #include <errno.h> | |
41 | #include <stddef.h> | |
42 | ||
43 | #ifdef _LIBC | |
44 | # include <shlib-compat.h> | |
45 | #else | |
46 | # define SHLIB_COMPAT(lib, introduced, obsoleted) 0 | |
47 | # define versioned_symbol(lib, local, symbol, version) extern int dummy | |
48 | # define compat_symbol(lib, local, symbol, version) | |
49 | # define weak_alias(local, symbol) | |
50 | # define __canonicalize_file_name canonicalize_file_name | |
51 | # define __realpath realpath | |
52 | # include "pathmax.h" | |
53 | # include "malloca.h" | |
54 | # include "dosname.h" | |
55 | # if HAVE_GETCWD | |
56 | # if IN_RELOCWRAPPER | |
57 | /* When building the relocatable program wrapper, use the system's getcwd | |
58 | function, not the gnulib override, otherwise we would get a link error. | |
59 | */ | |
60 | # undef getcwd | |
61 | # endif | |
62 | # ifdef VMS | |
63 | /* We want the directory in Unix syntax, not in VMS syntax. */ | |
64 | # define __getcwd(buf, max) getcwd (buf, max, 0) | |
65 | # else | |
66 | # define __getcwd getcwd | |
67 | # endif | |
68 | # else | |
69 | # define __getcwd(buf, max) getwd (buf) | |
70 | # endif | |
71 | # define __readlink readlink | |
72 | # define __set_errno(e) errno = (e) | |
73 | # ifndef MAXSYMLINKS | |
74 | # ifdef SYMLOOP_MAX | |
75 | # define MAXSYMLINKS SYMLOOP_MAX | |
76 | # else | |
77 | # define MAXSYMLINKS 20 | |
78 | # endif | |
79 | # endif | |
80 | #endif | |
81 | ||
82 | #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT | |
83 | # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0 | |
84 | #endif | |
85 | ||
86 | #if !FUNC_REALPATH_WORKS || defined _LIBC | |
49e4877c PA |
87 | |
88 | static void | |
89 | alloc_failed (void) | |
90 | { | |
91 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
92 | /* Avoid errno problem without using the malloc or realloc modules; see: | |
93 | http://lists.gnu.org/archive/html/bug-gnulib/2016-08/msg00025.html */ | |
94 | errno = ENOMEM; | |
95 | #endif | |
96 | } | |
97 | ||
98399780 YQ |
98 | /* Return the canonical absolute name of file NAME. A canonical name |
99 | does not contain any ".", ".." components nor any repeated path | |
100 | separators ('/') or symlinks. All path components must exist. If | |
101 | RESOLVED is null, the result is malloc'd; otherwise, if the | |
102 | canonical name is PATH_MAX chars or more, returns null with 'errno' | |
103 | set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, | |
104 | returns the name in RESOLVED. If the name cannot be resolved and | |
105 | RESOLVED is non-NULL, it contains the path of the first component | |
106 | that cannot be resolved. If the path can be resolved, RESOLVED | |
107 | holds the same value as the value returned. */ | |
108 | ||
109 | char * | |
110 | __realpath (const char *name, char *resolved) | |
111 | { | |
112 | char *rpath, *dest, *extra_buf = NULL; | |
113 | const char *start, *end, *rpath_limit; | |
114 | long int path_max; | |
115 | int num_links = 0; | |
116 | size_t prefix_len; | |
117 | ||
118 | if (name == NULL) | |
119 | { | |
120 | /* As per Single Unix Specification V2 we must return an error if | |
121 | either parameter is a null pointer. We extend this to allow | |
122 | the RESOLVED parameter to be NULL in case the we are expected to | |
123 | allocate the room for the return value. */ | |
124 | __set_errno (EINVAL); | |
125 | return NULL; | |
126 | } | |
127 | ||
128 | if (name[0] == '\0') | |
129 | { | |
130 | /* As per Single Unix Specification V2 we must return an error if | |
131 | the name argument points to an empty string. */ | |
132 | __set_errno (ENOENT); | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | #ifdef PATH_MAX | |
137 | path_max = PATH_MAX; | |
138 | #else | |
139 | path_max = pathconf (name, _PC_PATH_MAX); | |
140 | if (path_max <= 0) | |
141 | path_max = 8192; | |
142 | #endif | |
143 | ||
144 | if (resolved == NULL) | |
145 | { | |
146 | rpath = malloc (path_max); | |
147 | if (rpath == NULL) | |
148 | { | |
49e4877c | 149 | alloc_failed (); |
98399780 YQ |
150 | return NULL; |
151 | } | |
152 | } | |
153 | else | |
154 | rpath = resolved; | |
155 | rpath_limit = rpath + path_max; | |
156 | ||
157 | /* This is always zero for Posix hosts, but can be 2 for MS-Windows | |
158 | and MS-DOS X:/foo/bar file names. */ | |
159 | prefix_len = FILE_SYSTEM_PREFIX_LEN (name); | |
160 | ||
161 | if (!IS_ABSOLUTE_FILE_NAME (name)) | |
162 | { | |
163 | if (!__getcwd (rpath, path_max)) | |
164 | { | |
165 | rpath[0] = '\0'; | |
166 | goto error; | |
167 | } | |
168 | dest = strchr (rpath, '\0'); | |
169 | start = name; | |
170 | prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath); | |
171 | } | |
172 | else | |
173 | { | |
174 | dest = rpath; | |
175 | if (prefix_len) | |
176 | { | |
177 | memcpy (rpath, name, prefix_len); | |
178 | dest += prefix_len; | |
179 | } | |
180 | *dest++ = '/'; | |
181 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT) | |
182 | { | |
183 | if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) | |
184 | *dest++ = '/'; | |
185 | *dest = '\0'; | |
186 | } | |
187 | start = name + prefix_len; | |
188 | } | |
189 | ||
190 | for (end = start; *start; start = end) | |
191 | { | |
192 | #ifdef _LIBC | |
193 | struct stat64 st; | |
194 | #else | |
195 | struct stat st; | |
196 | #endif | |
197 | int n; | |
198 | ||
199 | /* Skip sequence of multiple path-separators. */ | |
200 | while (ISSLASH (*start)) | |
201 | ++start; | |
202 | ||
203 | /* Find end of path component. */ | |
204 | for (end = start; *end && !ISSLASH (*end); ++end) | |
205 | /* Nothing. */; | |
206 | ||
207 | if (end - start == 0) | |
208 | break; | |
209 | else if (end - start == 1 && start[0] == '.') | |
210 | /* nothing */; | |
211 | else if (end - start == 2 && start[0] == '.' && start[1] == '.') | |
212 | { | |
213 | /* Back up to previous component, ignore if at root already. */ | |
214 | if (dest > rpath + prefix_len + 1) | |
215 | for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) | |
216 | continue; | |
217 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT | |
218 | && dest == rpath + 1 && !prefix_len | |
219 | && ISSLASH (*dest) && !ISSLASH (dest[1])) | |
220 | dest++; | |
221 | } | |
222 | else | |
223 | { | |
224 | size_t new_size; | |
225 | ||
226 | if (!ISSLASH (dest[-1])) | |
227 | *dest++ = '/'; | |
228 | ||
229 | if (dest + (end - start) >= rpath_limit) | |
230 | { | |
231 | ptrdiff_t dest_offset = dest - rpath; | |
232 | char *new_rpath; | |
233 | ||
234 | if (resolved) | |
235 | { | |
236 | __set_errno (ENAMETOOLONG); | |
237 | if (dest > rpath + prefix_len + 1) | |
238 | dest--; | |
239 | *dest = '\0'; | |
240 | goto error; | |
241 | } | |
242 | new_size = rpath_limit - rpath; | |
243 | if (end - start + 1 > path_max) | |
244 | new_size += end - start + 1; | |
245 | else | |
246 | new_size += path_max; | |
247 | new_rpath = (char *) realloc (rpath, new_size); | |
248 | if (new_rpath == NULL) | |
249 | { | |
49e4877c | 250 | alloc_failed (); |
98399780 YQ |
251 | goto error; |
252 | } | |
253 | rpath = new_rpath; | |
254 | rpath_limit = rpath + new_size; | |
255 | ||
256 | dest = rpath + dest_offset; | |
257 | } | |
258 | ||
259 | #ifdef _LIBC | |
260 | dest = __mempcpy (dest, start, end - start); | |
261 | #else | |
262 | memcpy (dest, start, end - start); | |
263 | dest += end - start; | |
264 | #endif | |
265 | *dest = '\0'; | |
266 | ||
267 | #ifdef _LIBC | |
268 | if (__lxstat64 (_STAT_VER, rpath, &st) < 0) | |
269 | #else | |
270 | if (lstat (rpath, &st) < 0) | |
271 | #endif | |
272 | goto error; | |
273 | ||
274 | if (S_ISLNK (st.st_mode)) | |
275 | { | |
276 | char *buf; | |
277 | size_t len; | |
278 | ||
279 | if (++num_links > MAXSYMLINKS) | |
280 | { | |
281 | __set_errno (ELOOP); | |
282 | goto error; | |
283 | } | |
284 | ||
285 | buf = malloca (path_max); | |
286 | if (!buf) | |
287 | { | |
49e4877c | 288 | __set_errno (ENOMEM); |
98399780 YQ |
289 | goto error; |
290 | } | |
291 | ||
292 | n = __readlink (rpath, buf, path_max - 1); | |
293 | if (n < 0) | |
294 | { | |
295 | int saved_errno = errno; | |
296 | freea (buf); | |
49e4877c | 297 | __set_errno (saved_errno); |
98399780 YQ |
298 | goto error; |
299 | } | |
300 | buf[n] = '\0'; | |
301 | ||
302 | if (!extra_buf) | |
303 | { | |
304 | extra_buf = malloca (path_max); | |
305 | if (!extra_buf) | |
306 | { | |
307 | freea (buf); | |
49e4877c | 308 | __set_errno (ENOMEM); |
98399780 YQ |
309 | goto error; |
310 | } | |
311 | } | |
312 | ||
313 | len = strlen (end); | |
314 | if ((long int) (n + len) >= path_max) | |
315 | { | |
316 | freea (buf); | |
317 | __set_errno (ENAMETOOLONG); | |
318 | goto error; | |
319 | } | |
320 | ||
321 | /* Careful here, end may be a pointer into extra_buf... */ | |
322 | memmove (&extra_buf[n], end, len + 1); | |
323 | name = end = memcpy (extra_buf, buf, n); | |
324 | ||
325 | if (IS_ABSOLUTE_FILE_NAME (buf)) | |
326 | { | |
327 | size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); | |
328 | ||
329 | if (pfxlen) | |
330 | memcpy (rpath, buf, pfxlen); | |
331 | dest = rpath + pfxlen; | |
332 | *dest++ = '/'; /* It's an absolute symlink */ | |
333 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT) | |
334 | { | |
335 | if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) | |
336 | *dest++ = '/'; | |
337 | *dest = '\0'; | |
338 | } | |
339 | /* Install the new prefix to be in effect hereafter. */ | |
340 | prefix_len = pfxlen; | |
341 | } | |
342 | else | |
343 | { | |
344 | /* Back up to previous component, ignore if at root | |
345 | already: */ | |
346 | if (dest > rpath + prefix_len + 1) | |
347 | for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) | |
348 | continue; | |
349 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
350 | && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) | |
351 | dest++; | |
352 | } | |
353 | } | |
354 | else if (!S_ISDIR (st.st_mode) && *end != '\0') | |
355 | { | |
356 | __set_errno (ENOTDIR); | |
357 | goto error; | |
358 | } | |
359 | } | |
360 | } | |
361 | if (dest > rpath + prefix_len + 1 && ISSLASH (dest[-1])) | |
362 | --dest; | |
363 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len | |
364 | && ISSLASH (*dest) && !ISSLASH (dest[1])) | |
365 | dest++; | |
366 | *dest = '\0'; | |
367 | ||
368 | if (extra_buf) | |
369 | freea (extra_buf); | |
370 | ||
371 | return rpath; | |
372 | ||
373 | error: | |
374 | { | |
375 | int saved_errno = errno; | |
376 | if (extra_buf) | |
377 | freea (extra_buf); | |
378 | if (resolved == NULL) | |
379 | free (rpath); | |
49e4877c | 380 | __set_errno (saved_errno); |
98399780 YQ |
381 | } |
382 | return NULL; | |
383 | } | |
384 | versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); | |
385 | #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */ | |
386 | ||
387 | ||
388 | #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) | |
389 | char * | |
390 | attribute_compat_text_section | |
391 | __old_realpath (const char *name, char *resolved) | |
392 | { | |
393 | if (resolved == NULL) | |
394 | { | |
395 | __set_errno (EINVAL); | |
396 | return NULL; | |
397 | } | |
398 | ||
399 | return __realpath (name, resolved); | |
400 | } | |
401 | compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0); | |
402 | #endif | |
403 | ||
404 | ||
405 | char * | |
406 | __canonicalize_file_name (const char *name) | |
407 | { | |
408 | return __realpath (name, NULL); | |
409 | } | |
410 | weak_alias (__canonicalize_file_name, canonicalize_file_name) | |
411 | ||
412 | #else | |
413 | ||
414 | /* This declaration is solely to ensure that after preprocessing | |
415 | this file is never empty. */ | |
416 | typedef int dummy; | |
417 | ||
418 | #endif |