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"
25 # include <sys/param.h>
36 #include "filenamecat.h"
37 #include "hash-triple.h"
42 # define __set_errno(Val) errno = (Val)
46 #include "areadlink.h"
48 #if !(HAVE_CANONICALIZE_FILE_NAME || GNULIB_CANONICALIZE_LGPL)
49 /* Return the canonical absolute name of file NAME. A canonical name
50 does not contain any `.', `..' components nor any repeated file name
51 separators ('/') or symlinks. All components must exist.
52 The result is malloc'd. */
55 canonicalize_file_name (const char *name)
59 char *resolved, *extra_buf = NULL;
75 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
76 relative names into absolute ones, so prepend the working
77 directory if the file name is not absolute. */
82 if (!(wd = xgetcwd ()))
85 extra_buf = file_name_concat (wd, name, NULL);
90 resolved_size = strlen (name);
93 resolved_size = 2 * resolved_size + 1;
94 resolved = xmalloc (resolved_size);
95 resolved_len = resolvepath (name, resolved, resolved_size);
102 if (resolved_len < resolved_size)
109 /* NUL-terminate the resulting name. */
110 resolved[resolved_len] = '\0';
116 return canonicalize_filename_mode (name, CAN_EXISTING);
118 # endif /* !HAVE_RESOLVEPATH */
120 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
122 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
123 If *HT is not initialized, initialize it. */
125 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
129 size_t initial_capacity = 7;
130 *ht = hash_initialize (initial_capacity,
133 triple_compare_ino_str,
139 if (seen_file (*ht, filename, st))
142 record_file (*ht, filename, st);
146 /* Return the canonical absolute name of file NAME. A canonical name
147 does not contain any `.', `..' components nor any repeated file name
148 separators ('/') or symlinks. Whether components must exist
149 or not depends on canonicalize mode. The result is malloc'd. */
152 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
154 char *rname, *dest, *extra_buf = NULL;
157 char const *rname_limit;
158 size_t extra_len = 0;
159 Hash_table *ht = NULL;
163 __set_errno (EINVAL);
169 __set_errno (ENOENT);
178 dest = strchr (rname, '\0');
179 if (dest - rname < PATH_MAX)
181 char *p = xrealloc (rname, PATH_MAX);
182 dest = p + (dest - rname);
184 rname_limit = rname + PATH_MAX;
193 rname = xmalloc (PATH_MAX);
194 rname_limit = rname + PATH_MAX;
199 for (start = name; *start; start = end)
201 /* Skip sequence of multiple file name separators. */
202 while (*start == '/')
205 /* Find end of component. */
206 for (end = start; *end && *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 + 1)
217 while ((--dest)[-1] != '/');
226 if (dest + (end - start) >= rname_limit)
228 ptrdiff_t dest_offset = dest - rname;
229 size_t new_size = rname_limit - rname;
231 if (end - start + 1 > PATH_MAX)
232 new_size += end - start + 1;
234 new_size += PATH_MAX;
235 rname = xrealloc (rname, new_size);
236 rname_limit = rname + new_size;
238 dest = rname + dest_offset;
241 dest = memcpy (dest, start, end - start);
245 if (lstat (rname, &st) != 0)
247 if (can_mode == CAN_EXISTING)
249 if (can_mode == CAN_ALL_BUT_LAST && *end)
254 if (S_ISLNK (st.st_mode))
259 /* Detect loops. We cannot use the cycle-check module here,
260 since it's actually possible to encounter the same symlink
261 more than once in a given traversal. However, encountering
262 the same symlink,NAME pair twice does indicate a loop. */
263 if (seen_triple (&ht, name, &st))
266 if (can_mode == CAN_MISSING)
272 buf = areadlink_with_size (rname, st.st_size);
275 if (can_mode == CAN_MISSING && errno != ENOMEM)
287 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
288 extra_buf = xmalloc (extra_len);
290 else if ((n + len + 1) > extra_len)
292 extra_len = n + len + 1;
293 extra_buf = xrealloc (extra_buf, extra_len);
296 /* Careful here, end may be a pointer into extra_buf... */
297 memmove (&extra_buf[n], end, len + 1);
298 name = end = memcpy (extra_buf, buf, n);
301 dest = rname + 1; /* It's an absolute symlink */
303 /* Back up to previous component, ignore if at root already: */
304 if (dest > rname + 1)
305 while ((--dest)[-1] != '/');
311 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
319 if (dest > rname + 1 && dest[-1] == '/')