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 /* In this file, we cannot handle file names longer than PATH_MAX.
35 On systems with no file name length limit, use a fallback. */
37 # define PATH_MAX 8192
40 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
41 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
44 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
45 || GNULIB_CANONICALIZE_LGPL)
46 /* Return the canonical absolute name of file NAME. A canonical name
47 does not contain any `.', `..' components nor any repeated file name
48 separators ('/') or symlinks. All components must exist.
49 The result is malloc'd. */
52 canonicalize_file_name (const char *name)
54 return canonicalize_filename_mode (name, CAN_EXISTING);
56 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
58 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
59 If *HT is not initialized, initialize it. */
61 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
65 size_t initial_capacity = 7;
66 *ht = hash_initialize (initial_capacity,
69 triple_compare_ino_str,
75 if (seen_file (*ht, filename, st))
78 record_file (*ht, filename, st);
82 /* Return the canonical absolute name of file NAME, while treating
83 missing elements according to CAN_MODE. A canonical name
84 does not contain any `.', `..' components nor any repeated file name
85 separators ('/') or symlinks. Whether components must exist
86 or not depends on canonicalize mode. The result is malloc'd. */
89 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
91 char *rname, *dest, *extra_buf = NULL;
94 char const *rname_limit;
96 Hash_table *ht = NULL;
116 dest = strchr (rname, '\0');
117 if (dest - rname < PATH_MAX)
119 char *p = xrealloc (rname, PATH_MAX);
120 dest = p + (dest - rname);
122 rname_limit = rname + PATH_MAX;
131 rname = xmalloc (PATH_MAX);
132 rname_limit = rname + PATH_MAX;
135 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
139 for (start = name; *start; start = end)
141 /* Skip sequence of multiple file name separators. */
142 while (*start == '/')
145 /* Find end of component. */
146 for (end = start; *end && *end != '/'; ++end)
149 if (end - start == 0)
151 else if (end - start == 1 && start[0] == '.')
153 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
155 /* Back up to previous component, ignore if at root already. */
156 if (dest > rname + 1)
157 while ((--dest)[-1] != '/');
158 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
169 if (dest + (end - start) >= rname_limit)
171 ptrdiff_t dest_offset = dest - rname;
172 size_t new_size = rname_limit - rname;
174 if (end - start + 1 > PATH_MAX)
175 new_size += end - start + 1;
177 new_size += PATH_MAX;
178 rname = xrealloc (rname, new_size);
179 rname_limit = rname + new_size;
181 dest = rname + dest_offset;
184 dest = memcpy (dest, start, end - start);
188 if (lstat (rname, &st) != 0)
191 if (can_mode == CAN_EXISTING)
193 if (can_mode == CAN_ALL_BUT_LAST)
195 if (end[strspn (end, "/")] || saved_errno != ENOENT)
202 if (S_ISLNK (st.st_mode))
207 /* Detect loops. We cannot use the cycle-check module here,
208 since it's actually possible to encounter the same symlink
209 more than once in a given traversal. However, encountering
210 the same symlink,NAME pair twice does indicate a loop. */
211 if (seen_triple (&ht, name, &st))
213 if (can_mode == CAN_MISSING)
219 buf = areadlink_with_size (rname, st.st_size);
222 if (can_mode == CAN_MISSING && errno != ENOMEM)
234 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
235 extra_buf = xmalloc (extra_len);
237 else if ((n + len + 1) > extra_len)
239 extra_len = n + len + 1;
240 extra_buf = xrealloc (extra_buf, extra_len);
243 /* Careful here, end may be a pointer into extra_buf... */
244 memmove (&extra_buf[n], end, len + 1);
245 name = end = memcpy (extra_buf, buf, n);
249 dest = rname + 1; /* It's an absolute symlink */
250 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
255 /* Back up to previous component, ignore if at root
257 if (dest > rname + 1)
258 while ((--dest)[-1] != '/');
259 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
268 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
270 saved_errno = ENOTDIR;
276 if (dest > rname + 1 && dest[-1] == '/')
278 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && *dest == '/')
281 if (rname_limit != dest + 1)
282 rname = xrealloc (rname, dest - rname + 1);