1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2009 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 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
35 || GNULIB_CANONICALIZE_LGPL)
36 /* Return the canonical absolute name of file NAME. A canonical name
37 does not contain any `.', `..' components nor any repeated file name
38 separators ('/') or symlinks. All components must exist.
39 The result is malloc'd. */
42 canonicalize_file_name (const char *name)
44 return canonicalize_filename_mode (name, CAN_EXISTING);
46 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
48 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
49 If *HT is not initialized, initialize it. */
51 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
55 size_t initial_capacity = 7;
56 *ht = hash_initialize (initial_capacity,
59 triple_compare_ino_str,
65 if (seen_file (*ht, filename, st))
68 record_file (*ht, filename, st);
72 /* Return the canonical absolute name of file NAME, while treating
73 missing elements according to CAN_MODE. A canonical name
74 does not contain any `.', `..' components nor any repeated file name
75 separators ('/') or symlinks. Whether components must exist
76 or not depends on canonicalize mode. The result is malloc'd. */
79 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
81 char *rname, *dest, *extra_buf = NULL;
84 char const *rname_limit;
86 Hash_table *ht = NULL;
106 dest = strchr (rname, '\0');
107 if (dest - rname < PATH_MAX)
109 char *p = xrealloc (rname, PATH_MAX);
110 dest = p + (dest - rname);
112 rname_limit = rname + PATH_MAX;
121 rname = xmalloc (PATH_MAX);
122 rname_limit = rname + PATH_MAX;
127 for (start = name; *start; start = end)
129 /* Skip sequence of multiple file name separators. */
130 while (*start == '/')
133 /* Find end of component. */
134 for (end = start; *end && *end != '/'; ++end)
137 if (end - start == 0)
139 else if (end - start == 1 && start[0] == '.')
141 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
143 /* Back up to previous component, ignore if at root already. */
144 if (dest > rname + 1)
145 while ((--dest)[-1] != '/');
154 if (dest + (end - start) >= rname_limit)
156 ptrdiff_t dest_offset = dest - rname;
157 size_t new_size = rname_limit - rname;
159 if (end - start + 1 > PATH_MAX)
160 new_size += end - start + 1;
162 new_size += PATH_MAX;
163 rname = xrealloc (rname, new_size);
164 rname_limit = rname + new_size;
166 dest = rname + dest_offset;
169 dest = memcpy (dest, start, end - start);
173 if (lstat (rname, &st) != 0)
176 if (can_mode == CAN_EXISTING)
178 if (can_mode == CAN_ALL_BUT_LAST && *end)
183 if (S_ISLNK (st.st_mode))
188 /* Detect loops. We cannot use the cycle-check module here,
189 since it's actually possible to encounter the same symlink
190 more than once in a given traversal. However, encountering
191 the same symlink,NAME pair twice does indicate a loop. */
192 if (seen_triple (&ht, name, &st))
194 if (can_mode == CAN_MISSING)
200 buf = areadlink_with_size (rname, st.st_size);
203 if (can_mode == CAN_MISSING && errno != ENOMEM)
215 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
216 extra_buf = xmalloc (extra_len);
218 else if ((n + len + 1) > extra_len)
220 extra_len = n + len + 1;
221 extra_buf = xrealloc (extra_buf, extra_len);
224 /* Careful here, end may be a pointer into extra_buf... */
225 memmove (&extra_buf[n], end, len + 1);
226 name = end = memcpy (extra_buf, buf, n);
229 dest = rname + 1; /* It's an absolute symlink */
231 /* Back up to previous component, ignore if at root already: */
232 if (dest > rname + 1)
233 while ((--dest)[-1] != '/');
239 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
241 saved_errno = ENOTDIR;
247 if (dest > rname + 1 && dest[-1] == '/')