1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include "canonicalize.h"
27 #include "areadlink.h"
29 #include "hash-triple.h"
35 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
37 /* In this file, we cannot handle file names longer than PATH_MAX.
38 On systems with no file name length limit, use a fallback. */
40 # define PATH_MAX 8192
43 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
44 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
48 # define SLASHES "/\\"
53 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
54 || GNULIB_CANONICALIZE_LGPL)
55 /* Return the canonical absolute name of file NAME. A canonical name
56 does not contain any ".", ".." components nor any repeated file name
57 separators ('/') or symlinks. All components must exist.
58 The result is malloc'd. */
61 canonicalize_file_name (const char *name
)
63 return canonicalize_filename_mode (name
, CAN_EXISTING
);
65 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
67 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
68 If *HT is not initialized, initialize it. */
70 seen_triple (Hash_table
**ht
, char const *filename
, struct stat
const *st
)
74 size_t initial_capacity
= 7;
75 *ht
= hash_initialize (initial_capacity
,
78 triple_compare_ino_str
,
84 if (seen_file (*ht
, filename
, st
))
87 record_file (*ht
, filename
, st
);
91 /* Return the canonical absolute name of file NAME, while treating
92 missing elements according to CAN_MODE. A canonical name
93 does not contain any ".", ".." components nor any repeated file name
94 separators ('/') or, depending on other CAN_MODE flags, symlinks.
95 Whether components must exist or not depends on canonicalize mode.
96 The result is malloc'd. */
99 canonicalize_filename_mode (const char *name
, canonicalize_mode_t can_mode
)
101 char *rname
, *dest
, *extra_buf
= NULL
;
104 char const *rname_limit
;
105 size_t extra_len
= 0;
106 Hash_table
*ht
= NULL
;
108 int can_flags
= can_mode
& ~CAN_MODE_MASK
;
109 bool logical
= can_flags
& CAN_NOLINKS
;
112 can_mode
&= CAN_MODE_MASK
;
114 if (MULTIPLE_BITS_SET (can_mode
))
132 /* This is always zero for Posix hosts, but can be 2 for MS-Windows
133 and MS-DOS X:/foo/bar file names. */
134 prefix_len
= FILE_SYSTEM_PREFIX_LEN (name
);
136 if (!IS_ABSOLUTE_FILE_NAME (name
))
141 size_t rnamelen
= strlen (rname
);
142 size_t rnamesize
= rnamelen
; /* Lower bound on size; good enough. */
143 if (rnamesize
< PATH_MAX
)
145 rnamesize
= PATH_MAX
;
146 rname
= xrealloc (rname
, rnamesize
);
148 dest
= rname
+ rnamelen
;
149 rname_limit
= rname
+ rnamesize
;
151 prefix_len
= FILE_SYSTEM_PREFIX_LEN (rname
);
155 rname
= xmalloc (PATH_MAX
);
156 rname_limit
= rname
+ PATH_MAX
;
160 memcpy (rname
, name
, prefix_len
);
164 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
166 if (prefix_len
== 0 /* implies ISSLASH (name[0]) */
167 && ISSLASH (name
[1]) && !ISSLASH (name
[2]))
170 #if defined _WIN32 && !defined __CYGWIN__
171 /* For UNC file names '\\server\path\to\file', extend the prefix
172 to include the server: '\\server\'. */
175 for (i
= 2; name
[i
] != '\0' && !ISSLASH (name
[i
]); )
177 if (name
[i
] != '\0' /* implies ISSLASH (name[i]) */
178 && i
+ 1 < rname_limit
- rname
)
181 memcpy (dest
, name
+ 2, i
- 2 + 1);
186 /* Either name = '\\server'; this is an invalid file name.
187 Or name = '\\server\...' and server is more than
188 PATH_MAX - 4 bytes long. In either case, stop the UNC
196 start
= name
+ prefix_len
;
199 for ( ; *start
; start
= end
)
201 /* Skip sequence of multiple file name separators. */
202 while (ISSLASH (*start
))
205 /* Find end of component. */
206 for (end
= start
; *end
&& !ISSLASH (*end
); ++end
)
209 if (end
- start
== 0)
211 else if (end
- start
== 1 && start
[0] == '.')
213 else if (end
- start
== 2 && start
[0] == '.' && start
[1] == '.')
215 /* Back up to previous component, ignore if at root already. */
216 if (dest
> rname
+ prefix_len
+ 1)
217 for (--dest
; dest
> rname
&& !ISSLASH (dest
[-1]); --dest
)
219 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rname
+ 1
220 && !prefix_len
&& ISSLASH (*dest
) && !ISSLASH (dest
[1]))
227 if (!ISSLASH (dest
[-1]))
230 if (rname_limit
- dest
<= end
- start
)
232 ptrdiff_t dest_offset
= dest
- rname
;
233 size_t new_size
= rname_limit
- rname
;
235 if (end
- start
+ 1 > PATH_MAX
)
236 new_size
+= end
- start
+ 1;
238 new_size
+= PATH_MAX
;
239 rname
= xrealloc (rname
, new_size
);
240 rname_limit
= rname
+ new_size
;
242 dest
= rname
+ dest_offset
;
245 dest
= memcpy (dest
, start
, end
- start
);
249 if (logical
&& (can_mode
== CAN_MISSING
))
251 /* Avoid the stat in this case as it's inconsequential.
252 i.e. we're neither resolving symlinks or testing
253 component existence. */
256 else if ((logical
? stat (rname
, &st
) : lstat (rname
, &st
)) != 0)
258 /* FIXME: If errno == EOVERFLOW here, the entry exists. */
260 if (can_mode
== CAN_EXISTING
)
262 if (can_mode
== CAN_ALL_BUT_LAST
)
264 if (end
[strspn (end
, SLASHES
)] || saved_errno
!= ENOENT
)
271 if (S_ISLNK (st
.st_mode
))
276 /* Detect loops. We cannot use the cycle-check module here,
277 since it's actually possible to encounter the same symlink
278 more than once in a given traversal. However, encountering
279 the same symlink,NAME pair twice does indicate a loop. */
280 if (seen_triple (&ht
, name
, &st
))
282 if (can_mode
== CAN_MISSING
)
288 buf
= areadlink_with_size (rname
, st
.st_size
);
291 if (can_mode
== CAN_MISSING
&& errno
!= ENOMEM
)
303 ((n
+ len
+ 1) > PATH_MAX
) ? (n
+ len
+ 1) : PATH_MAX
;
304 extra_buf
= xmalloc (extra_len
);
306 else if ((n
+ len
+ 1) > extra_len
)
308 extra_len
= n
+ len
+ 1;
309 extra_buf
= xrealloc (extra_buf
, extra_len
);
312 /* Careful here, end may be a pointer into extra_buf... */
313 memmove (&extra_buf
[n
], end
, len
+ 1);
314 name
= end
= memcpy (extra_buf
, buf
, n
);
316 if (IS_ABSOLUTE_FILE_NAME (buf
))
318 size_t pfxlen
= FILE_SYSTEM_PREFIX_LEN (buf
);
321 memcpy (rname
, buf
, pfxlen
);
322 dest
= rname
+ pfxlen
;
323 *dest
++ = '/'; /* It's an absolute symlink */
324 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
)
326 if (ISSLASH (buf
[1]) && !ISSLASH (buf
[2]) && !pfxlen
)
330 /* Install the new prefix to be in effect hereafter. */
335 /* Back up to previous component, ignore if at root
337 if (dest
> rname
+ prefix_len
+ 1)
338 for (--dest
; dest
> rname
&& !ISSLASH (dest
[-1]); --dest
)
340 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rname
+ 1
341 && ISSLASH (*dest
) && !ISSLASH (dest
[1]) && !prefix_len
)
349 if (!S_ISDIR (st
.st_mode
) && *end
&& (can_mode
!= CAN_MISSING
))
351 saved_errno
= ENOTDIR
;
357 if (dest
> rname
+ prefix_len
+ 1 && ISSLASH (dest
[-1]))
359 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
&& dest
== rname
+ 1 && !prefix_len
360 && ISSLASH (*dest
) && !ISSLASH (dest
[1]))
363 if (rname_limit
!= dest
+ 1)
364 rname
= xrealloc (rname
, dest
- rname
+ 1);