1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2011 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 <http://www.gnu.org/licenses/>. */
19 #include "canonicalize.h"
27 #include "areadlink.h"
29 #include "hash-triple.h"
34 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
35 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
38 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
39 || GNULIB_CANONICALIZE_LGPL)
40 /* Return the canonical absolute name of file NAME. A canonical name
41 does not contain any `.', `..' components nor any repeated file name
42 separators ('/') or symlinks. All components must exist.
43 The result is malloc'd. */
46 canonicalize_file_name (const char *name)
48 return canonicalize_filename_mode (name, CAN_EXISTING);
50 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
52 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
53 If *HT is not initialized, initialize it. */
55 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
59 size_t initial_capacity = 7;
60 *ht = hash_initialize (initial_capacity,
63 triple_compare_ino_str,
69 if (seen_file (*ht, filename, st))
72 record_file (*ht, filename, st);
76 /* Return the canonical absolute name of file NAME, while treating
77 missing elements according to CAN_MODE. A canonical name
78 does not contain any `.', `..' components nor any repeated file name
79 separators ('/') or symlinks. Whether components must exist
80 or not depends on canonicalize mode. The result is malloc'd. */
83 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
85 char *rname, *dest, *extra_buf = NULL;
88 char const *rname_limit;
90 Hash_table *ht = NULL;
110 dest = strchr (rname, '\0');
111 if (dest - rname < PATH_MAX)
113 char *p = xrealloc (rname, PATH_MAX);
114 dest = p + (dest - rname);
116 rname_limit = rname + PATH_MAX;
125 rname = xmalloc (PATH_MAX);
126 rname_limit = rname + PATH_MAX;
129 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
133 for (start = name; *start; start = end)
135 /* Skip sequence of multiple file name separators. */
136 while (*start == '/')
139 /* Find end of component. */
140 for (end = start; *end && *end != '/'; ++end)
143 if (end - start == 0)
145 else if (end - start == 1 && start[0] == '.')
147 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
149 /* Back up to previous component, ignore if at root already. */
150 if (dest > rname + 1)
151 while ((--dest)[-1] != '/');
152 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
163 if (dest + (end - start) >= rname_limit)
165 ptrdiff_t dest_offset = dest - rname;
166 size_t new_size = rname_limit - rname;
168 if (end - start + 1 > PATH_MAX)
169 new_size += end - start + 1;
171 new_size += PATH_MAX;
172 rname = xrealloc (rname, new_size);
173 rname_limit = rname + new_size;
175 dest = rname + dest_offset;
178 dest = memcpy (dest, start, end - start);
182 if (lstat (rname, &st) != 0)
185 if (can_mode == CAN_EXISTING)
187 if (can_mode == CAN_ALL_BUT_LAST)
189 if (end[strspn (end, "/")] || saved_errno != ENOENT)
196 if (S_ISLNK (st.st_mode))
201 /* Detect loops. We cannot use the cycle-check module here,
202 since it's actually possible to encounter the same symlink
203 more than once in a given traversal. However, encountering
204 the same symlink,NAME pair twice does indicate a loop. */
205 if (seen_triple (&ht, name, &st))
207 if (can_mode == CAN_MISSING)
213 buf = areadlink_with_size (rname, st.st_size);
216 if (can_mode == CAN_MISSING && errno != ENOMEM)
228 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
229 extra_buf = xmalloc (extra_len);
231 else if ((n + len + 1) > extra_len)
233 extra_len = n + len + 1;
234 extra_buf = xrealloc (extra_buf, extra_len);
237 /* Careful here, end may be a pointer into extra_buf... */
238 memmove (&extra_buf[n], end, len + 1);
239 name = end = memcpy (extra_buf, buf, n);
243 dest = rname + 1; /* It's an absolute symlink */
244 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
249 /* Back up to previous component, ignore if at root
251 if (dest > rname + 1)
252 while ((--dest)[-1] != '/');
253 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
262 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
264 saved_errno = ENOTDIR;
270 if (dest > rname + 1 && dest[-1] == '/')
272 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && *dest == '/')
275 if (rname_limit != dest + 1)
276 rname = xrealloc (rname, dest - rname + 1);