1 /* Return the canonical absolute name of a given file.
2 Copyright (C) 1996-2007 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 2, or (at your option)
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; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "canonicalize.h"
27 # include <sys/param.h>
38 #include "filenamecat.h"
39 #include "hash-triple.h"
47 # define __set_errno(Val) errno = (Val)
51 #include "areadlink.h"
53 #if !HAVE_CANONICALIZE_FILE_NAME
54 /* Return the canonical absolute name of file NAME. A canonical name
55 does not contain any `.', `..' components nor any repeated file name
56 separators ('/') or symlinks. All components must exist.
57 The result is malloc'd. */
60 canonicalize_file_name (const char *name)
64 char *resolved, *extra_buf = NULL;
80 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
81 relative names into absolute ones, so prepend the working
82 directory if the file name is not absolute. */
87 if (!(wd = xgetcwd ()))
90 extra_buf = file_name_concat (wd, name, NULL);
95 resolved_size = strlen (name);
98 resolved_size = 2 * resolved_size + 1;
99 resolved = xmalloc (resolved_size);
100 resolved_len = resolvepath (name, resolved, resolved_size);
101 if (resolved_len < 0)
107 if (resolved_len < resolved_size)
114 /* NUL-terminate the resulting name. */
115 resolved[resolved_len] = '\0';
121 return canonicalize_filename_mode (name, CAN_EXISTING);
123 # endif /* !HAVE_RESOLVEPATH */
125 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
127 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
128 If *HT is not initialized, initialize it. */
130 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
134 size_t initial_capacity = 7;
135 *ht = hash_initialize (initial_capacity,
144 if (seen_file (*ht, filename, st))
147 record_file (*ht, filename, st);
151 /* Return the canonical absolute name of file NAME. A canonical name
152 does not contain any `.', `..' components nor any repeated file name
153 separators ('/') or symlinks. Whether components must exist
154 or not depends on canonicalize mode. The result is malloc'd. */
157 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
159 char *rname, *dest, *extra_buf = NULL;
162 char const *rname_limit;
163 size_t extra_len = 0;
164 Hash_table *ht = NULL;
168 __set_errno (EINVAL);
174 __set_errno (ENOENT);
183 dest = strchr (rname, '\0');
184 if (dest - rname < PATH_MAX)
186 char *p = xrealloc (rname, PATH_MAX);
187 dest = p + (dest - rname);
189 rname_limit = rname + PATH_MAX;
198 rname = xmalloc (PATH_MAX);
199 rname_limit = rname + PATH_MAX;
204 for (start = end = name; *start; start = end)
206 /* Skip sequence of multiple file name separators. */
207 while (*start == '/')
210 /* Find end of component. */
211 for (end = start; *end && *end != '/'; ++end)
214 if (end - start == 0)
216 else if (end - start == 1 && start[0] == '.')
218 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
220 /* Back up to previous component, ignore if at root already. */
221 if (dest > rname + 1)
222 while ((--dest)[-1] != '/');
231 if (dest + (end - start) >= rname_limit)
233 ptrdiff_t dest_offset = dest - rname;
234 size_t new_size = rname_limit - rname;
236 if (end - start + 1 > PATH_MAX)
237 new_size += end - start + 1;
239 new_size += PATH_MAX;
240 rname = xrealloc (rname, new_size);
241 rname_limit = rname + new_size;
243 dest = rname + dest_offset;
246 dest = memcpy (dest, start, end - start);
250 if (lstat (rname, &st) != 0)
252 if (can_mode == CAN_EXISTING)
254 if (can_mode == CAN_ALL_BUT_LAST && *end)
259 if (S_ISLNK (st.st_mode))
264 /* Detect loops. We cannot use the cycle-check module here,
265 since it's actually possible to encounter the same symlink
266 more than once in a given traversal. However, encountering
267 the same symlink,NAME pair twice does indicate a loop. */
268 if (seen_triple (&ht, name, &st))
271 if (can_mode == CAN_MISSING)
277 buf = areadlink_with_size (rname, st.st_size);
280 if (can_mode == CAN_MISSING && errno != ENOMEM)
292 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
293 extra_buf = xmalloc (extra_len);
295 else if ((n + len + 1) > extra_len)
297 extra_len = n + len + 1;
298 extra_buf = xrealloc (extra_buf, extra_len);
301 /* Careful here, end may be a pointer into extra_buf... */
302 memmove (&extra_buf[n], end, len + 1);
303 name = end = memcpy (extra_buf, buf, n);
306 dest = rname + 1; /* It's an absolute symlink */
308 /* Back up to previous component, ignore if at root already: */
309 if (dest > rname + 1)
310 while ((--dest)[-1] != '/');
316 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
324 if (dest > rname + 1 && dest[-1] == '/')