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>
35 #include "areadlink.h"
37 #include "filenamecat.h"
38 #include "hash-triple.h"
43 #if !(HAVE_CANONICALIZE_FILE_NAME || GNULIB_CANONICALIZE_LGPL)
44 /* Return the canonical absolute name of file NAME. A canonical name
45 does not contain any `.', `..' components nor any repeated file name
46 separators ('/') or symlinks. All components must exist.
47 The result is malloc'd. */
50 canonicalize_file_name (const char *name)
54 char *resolved, *extra_buf = NULL;
70 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
71 relative names into absolute ones, so prepend the working
72 directory if the file name is not absolute. */
77 if (!(wd = xgetcwd ()))
80 extra_buf = file_name_concat (wd, name, NULL);
85 resolved_size = strlen (name);
88 resolved_size = 2 * resolved_size + 1;
89 resolved = xmalloc (resolved_size);
90 resolved_len = resolvepath (name, resolved, resolved_size);
97 if (resolved_len < resolved_size)
104 /* NUL-terminate the resulting name. */
105 resolved[resolved_len] = '\0';
111 return canonicalize_filename_mode (name, CAN_EXISTING);
113 # endif /* !HAVE_RESOLVEPATH */
115 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
117 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
118 If *HT is not initialized, initialize it. */
120 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
124 size_t initial_capacity = 7;
125 *ht = hash_initialize (initial_capacity,
128 triple_compare_ino_str,
134 if (seen_file (*ht, filename, st))
137 record_file (*ht, filename, st);
141 /* Return the canonical absolute name of file NAME. A canonical name
142 does not contain any `.', `..' components nor any repeated file name
143 separators ('/') or symlinks. Whether components must exist
144 or not depends on canonicalize mode. The result is malloc'd. */
147 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
149 char *rname, *dest, *extra_buf = NULL;
152 char const *rname_limit;
153 size_t extra_len = 0;
154 Hash_table *ht = NULL;
173 dest = strchr (rname, '\0');
174 if (dest - rname < PATH_MAX)
176 char *p = xrealloc (rname, PATH_MAX);
177 dest = p + (dest - rname);
179 rname_limit = rname + PATH_MAX;
188 rname = xmalloc (PATH_MAX);
189 rname_limit = rname + PATH_MAX;
194 for (start = name; *start; start = end)
196 /* Skip sequence of multiple file name separators. */
197 while (*start == '/')
200 /* Find end of component. */
201 for (end = start; *end && *end != '/'; ++end)
204 if (end - start == 0)
206 else if (end - start == 1 && start[0] == '.')
208 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
210 /* Back up to previous component, ignore if at root already. */
211 if (dest > rname + 1)
212 while ((--dest)[-1] != '/');
221 if (dest + (end - start) >= rname_limit)
223 ptrdiff_t dest_offset = dest - rname;
224 size_t new_size = rname_limit - rname;
226 if (end - start + 1 > PATH_MAX)
227 new_size += end - start + 1;
229 new_size += PATH_MAX;
230 rname = xrealloc (rname, new_size);
231 rname_limit = rname + new_size;
233 dest = rname + dest_offset;
236 dest = memcpy (dest, start, end - start);
240 if (lstat (rname, &st) != 0)
242 if (can_mode == CAN_EXISTING)
244 if (can_mode == CAN_ALL_BUT_LAST && *end)
249 if (S_ISLNK (st.st_mode))
254 /* Detect loops. We cannot use the cycle-check module here,
255 since it's actually possible to encounter the same symlink
256 more than once in a given traversal. However, encountering
257 the same symlink,NAME pair twice does indicate a loop. */
258 if (seen_triple (&ht, name, &st))
260 if (can_mode == CAN_MISSING)
266 buf = areadlink_with_size (rname, st.st_size);
269 if (can_mode == CAN_MISSING && errno != ENOMEM)
280 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
281 extra_buf = xmalloc (extra_len);
283 else if ((n + len + 1) > extra_len)
285 extra_len = n + len + 1;
286 extra_buf = xrealloc (extra_buf, extra_len);
289 /* Careful here, end may be a pointer into extra_buf... */
290 memmove (&extra_buf[n], end, len + 1);
291 name = end = memcpy (extra_buf, buf, n);
294 dest = rname + 1; /* It's an absolute symlink */
296 /* Back up to previous component, ignore if at root already: */
297 if (dest > rname + 1)
298 while ((--dest)[-1] != '/');
304 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
312 if (dest > rname + 1 && dest[-1] == '/')