Commit | Line | Data |
---|---|---|
98399780 | 1 | /* Return the canonical absolute name of a given file. |
5e8754f9 | 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 | |
5e8754f9 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
98399780 YQ |
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 | |
5e8754f9 SDJ |
62 | # ifdef VMS |
63 | /* We want the directory in Unix syntax, not in VMS syntax. */ | |
98399780 YQ |
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 | ||
4c62b19f PA |
86 | /* Define this independently so that stdint.h is not a prerequisite. */ |
87 | #ifndef SIZE_MAX | |
88 | # define SIZE_MAX ((size_t) -1) | |
89 | #endif | |
90 | ||
98399780 | 91 | #if !FUNC_REALPATH_WORKS || defined _LIBC |
49e4877c PA |
92 | |
93 | static void | |
94 | alloc_failed (void) | |
95 | { | |
5e8754f9 | 96 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
49e4877c | 97 | /* Avoid errno problem without using the malloc or realloc modules; see: |
5e8754f9 | 98 | http://lists.gnu.org/archive/html/bug-gnulib/2016-08/msg00025.html */ |
49e4877c PA |
99 | errno = ENOMEM; |
100 | #endif | |
101 | } | |
102 | ||
98399780 YQ |
103 | /* Return the canonical absolute name of file NAME. A canonical name |
104 | does not contain any ".", ".." components nor any repeated path | |
105 | separators ('/') or symlinks. All path components must exist. If | |
106 | RESOLVED is null, the result is malloc'd; otherwise, if the | |
107 | canonical name is PATH_MAX chars or more, returns null with 'errno' | |
108 | set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, | |
109 | returns the name in RESOLVED. If the name cannot be resolved and | |
110 | RESOLVED is non-NULL, it contains the path of the first component | |
111 | that cannot be resolved. If the path can be resolved, RESOLVED | |
112 | holds the same value as the value returned. */ | |
113 | ||
114 | char * | |
115 | __realpath (const char *name, char *resolved) | |
116 | { | |
117 | char *rpath, *dest, *extra_buf = NULL; | |
118 | const char *start, *end, *rpath_limit; | |
119 | long int path_max; | |
120 | int num_links = 0; | |
121 | size_t prefix_len; | |
122 | ||
123 | if (name == NULL) | |
124 | { | |
125 | /* As per Single Unix Specification V2 we must return an error if | |
126 | either parameter is a null pointer. We extend this to allow | |
127 | the RESOLVED parameter to be NULL in case the we are expected to | |
128 | allocate the room for the return value. */ | |
129 | __set_errno (EINVAL); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | if (name[0] == '\0') | |
134 | { | |
135 | /* As per Single Unix Specification V2 we must return an error if | |
136 | the name argument points to an empty string. */ | |
137 | __set_errno (ENOENT); | |
138 | return NULL; | |
139 | } | |
140 | ||
141 | #ifdef PATH_MAX | |
142 | path_max = PATH_MAX; | |
143 | #else | |
144 | path_max = pathconf (name, _PC_PATH_MAX); | |
145 | if (path_max <= 0) | |
146 | path_max = 8192; | |
147 | #endif | |
148 | ||
149 | if (resolved == NULL) | |
150 | { | |
151 | rpath = malloc (path_max); | |
152 | if (rpath == NULL) | |
153 | { | |
49e4877c | 154 | alloc_failed (); |
98399780 YQ |
155 | return NULL; |
156 | } | |
157 | } | |
158 | else | |
159 | rpath = resolved; | |
160 | rpath_limit = rpath + path_max; | |
161 | ||
162 | /* This is always zero for Posix hosts, but can be 2 for MS-Windows | |
163 | and MS-DOS X:/foo/bar file names. */ | |
164 | prefix_len = FILE_SYSTEM_PREFIX_LEN (name); | |
165 | ||
166 | if (!IS_ABSOLUTE_FILE_NAME (name)) | |
167 | { | |
168 | if (!__getcwd (rpath, path_max)) | |
169 | { | |
170 | rpath[0] = '\0'; | |
171 | goto error; | |
172 | } | |
173 | dest = strchr (rpath, '\0'); | |
174 | start = name; | |
175 | prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath); | |
176 | } | |
177 | else | |
178 | { | |
179 | dest = rpath; | |
180 | if (prefix_len) | |
181 | { | |
182 | memcpy (rpath, name, prefix_len); | |
183 | dest += prefix_len; | |
184 | } | |
185 | *dest++ = '/'; | |
186 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT) | |
187 | { | |
188 | if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) | |
189 | *dest++ = '/'; | |
190 | *dest = '\0'; | |
191 | } | |
192 | start = name + prefix_len; | |
193 | } | |
194 | ||
195 | for (end = start; *start; start = end) | |
196 | { | |
197 | #ifdef _LIBC | |
198 | struct stat64 st; | |
199 | #else | |
200 | struct stat st; | |
201 | #endif | |
98399780 YQ |
202 | |
203 | /* Skip sequence of multiple path-separators. */ | |
204 | while (ISSLASH (*start)) | |
205 | ++start; | |
206 | ||
207 | /* Find end of path component. */ | |
208 | for (end = start; *end && !ISSLASH (*end); ++end) | |
209 | /* Nothing. */; | |
210 | ||
211 | if (end - start == 0) | |
212 | break; | |
213 | else if (end - start == 1 && start[0] == '.') | |
214 | /* nothing */; | |
215 | else if (end - start == 2 && start[0] == '.' && start[1] == '.') | |
216 | { | |
217 | /* Back up to previous component, ignore if at root already. */ | |
218 | if (dest > rpath + prefix_len + 1) | |
219 | for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) | |
220 | continue; | |
221 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT | |
222 | && dest == rpath + 1 && !prefix_len | |
223 | && ISSLASH (*dest) && !ISSLASH (dest[1])) | |
224 | dest++; | |
225 | } | |
226 | else | |
227 | { | |
228 | size_t new_size; | |
229 | ||
230 | if (!ISSLASH (dest[-1])) | |
231 | *dest++ = '/'; | |
232 | ||
233 | if (dest + (end - start) >= rpath_limit) | |
234 | { | |
235 | ptrdiff_t dest_offset = dest - rpath; | |
236 | char *new_rpath; | |
237 | ||
238 | if (resolved) | |
239 | { | |
240 | __set_errno (ENAMETOOLONG); | |
241 | if (dest > rpath + prefix_len + 1) | |
242 | dest--; | |
243 | *dest = '\0'; | |
244 | goto error; | |
245 | } | |
246 | new_size = rpath_limit - rpath; | |
247 | if (end - start + 1 > path_max) | |
248 | new_size += end - start + 1; | |
249 | else | |
250 | new_size += path_max; | |
251 | new_rpath = (char *) realloc (rpath, new_size); | |
252 | if (new_rpath == NULL) | |
253 | { | |
49e4877c | 254 | alloc_failed (); |
98399780 YQ |
255 | goto error; |
256 | } | |
257 | rpath = new_rpath; | |
258 | rpath_limit = rpath + new_size; | |
259 | ||
260 | dest = rpath + dest_offset; | |
261 | } | |
262 | ||
263 | #ifdef _LIBC | |
264 | dest = __mempcpy (dest, start, end - start); | |
265 | #else | |
266 | memcpy (dest, start, end - start); | |
267 | dest += end - start; | |
268 | #endif | |
269 | *dest = '\0'; | |
270 | ||
271 | #ifdef _LIBC | |
272 | if (__lxstat64 (_STAT_VER, rpath, &st) < 0) | |
273 | #else | |
274 | if (lstat (rpath, &st) < 0) | |
275 | #endif | |
276 | goto error; | |
277 | ||
278 | if (S_ISLNK (st.st_mode)) | |
279 | { | |
280 | char *buf; | |
281 | size_t len; | |
4c62b19f | 282 | ssize_t n; |
98399780 YQ |
283 | |
284 | if (++num_links > MAXSYMLINKS) | |
285 | { | |
286 | __set_errno (ELOOP); | |
287 | goto error; | |
288 | } | |
289 | ||
290 | buf = malloca (path_max); | |
291 | if (!buf) | |
292 | { | |
49e4877c | 293 | __set_errno (ENOMEM); |
98399780 YQ |
294 | goto error; |
295 | } | |
296 | ||
297 | n = __readlink (rpath, buf, path_max - 1); | |
298 | if (n < 0) | |
299 | { | |
300 | int saved_errno = errno; | |
301 | freea (buf); | |
49e4877c | 302 | __set_errno (saved_errno); |
98399780 YQ |
303 | goto error; |
304 | } | |
305 | buf[n] = '\0'; | |
306 | ||
307 | if (!extra_buf) | |
308 | { | |
309 | extra_buf = malloca (path_max); | |
310 | if (!extra_buf) | |
311 | { | |
312 | freea (buf); | |
49e4877c | 313 | __set_errno (ENOMEM); |
98399780 YQ |
314 | goto error; |
315 | } | |
316 | } | |
317 | ||
318 | len = strlen (end); | |
4c62b19f PA |
319 | /* Check that n + len + 1 doesn't overflow and is <= path_max. */ |
320 | if (n >= SIZE_MAX - len || n + len >= path_max) | |
98399780 YQ |
321 | { |
322 | freea (buf); | |
323 | __set_errno (ENAMETOOLONG); | |
324 | goto error; | |
325 | } | |
326 | ||
327 | /* Careful here, end may be a pointer into extra_buf... */ | |
328 | memmove (&extra_buf[n], end, len + 1); | |
329 | name = end = memcpy (extra_buf, buf, n); | |
330 | ||
331 | if (IS_ABSOLUTE_FILE_NAME (buf)) | |
332 | { | |
333 | size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); | |
334 | ||
335 | if (pfxlen) | |
336 | memcpy (rpath, buf, pfxlen); | |
337 | dest = rpath + pfxlen; | |
338 | *dest++ = '/'; /* It's an absolute symlink */ | |
339 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT) | |
340 | { | |
341 | if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) | |
342 | *dest++ = '/'; | |
343 | *dest = '\0'; | |
344 | } | |
345 | /* Install the new prefix to be in effect hereafter. */ | |
346 | prefix_len = pfxlen; | |
347 | } | |
348 | else | |
349 | { | |
350 | /* Back up to previous component, ignore if at root | |
351 | already: */ | |
352 | if (dest > rpath + prefix_len + 1) | |
353 | for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) | |
354 | continue; | |
355 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 | |
356 | && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) | |
357 | dest++; | |
358 | } | |
359 | } | |
360 | else if (!S_ISDIR (st.st_mode) && *end != '\0') | |
361 | { | |
362 | __set_errno (ENOTDIR); | |
363 | goto error; | |
364 | } | |
365 | } | |
366 | } | |
367 | if (dest > rpath + prefix_len + 1 && ISSLASH (dest[-1])) | |
368 | --dest; | |
369 | if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len | |
370 | && ISSLASH (*dest) && !ISSLASH (dest[1])) | |
371 | dest++; | |
372 | *dest = '\0'; | |
373 | ||
374 | if (extra_buf) | |
375 | freea (extra_buf); | |
376 | ||
377 | return rpath; | |
378 | ||
379 | error: | |
380 | { | |
381 | int saved_errno = errno; | |
382 | if (extra_buf) | |
383 | freea (extra_buf); | |
384 | if (resolved == NULL) | |
385 | free (rpath); | |
49e4877c | 386 | __set_errno (saved_errno); |
98399780 YQ |
387 | } |
388 | return NULL; | |
389 | } | |
390 | versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); | |
391 | #endif /* !FUNC_REALPATH_WORKS || defined _LIBC */ | |
392 | ||
393 | ||
394 | #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) | |
395 | char * | |
396 | attribute_compat_text_section | |
397 | __old_realpath (const char *name, char *resolved) | |
398 | { | |
399 | if (resolved == NULL) | |
400 | { | |
401 | __set_errno (EINVAL); | |
402 | return NULL; | |
403 | } | |
404 | ||
405 | return __realpath (name, resolved); | |
406 | } | |
407 | compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0); | |
408 | #endif | |
409 | ||
410 | ||
411 | char * | |
412 | __canonicalize_file_name (const char *name) | |
413 | { | |
414 | return __realpath (name, NULL); | |
415 | } | |
416 | weak_alias (__canonicalize_file_name, canonicalize_file_name) | |
417 | ||
418 | #else | |
419 | ||
420 | /* This declaration is solely to ensure that after preprocessing | |
421 | this file is never empty. */ | |
422 | typedef int dummy; | |
423 | ||
424 | #endif |