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 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"
45 # define __set_errno(Val) errno = (Val)
49 #include "areadlink.h"
51 #if !HAVE_CANONICALIZE_FILE_NAME
52 /* Return the canonical absolute name of file NAME. A canonical name
53 does not contain any `.', `..' components nor any repeated file name
54 separators ('/') or symlinks. All components must exist.
55 The result is malloc'd. */
58 canonicalize_file_name (const char *name)
62 char *resolved, *extra_buf = NULL;
78 /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
79 relative names into absolute ones, so prepend the working
80 directory if the file name is not absolute. */
85 if (!(wd = xgetcwd ()))
88 extra_buf = file_name_concat (wd, name, NULL);
93 resolved_size = strlen (name);
96 resolved_size = 2 * resolved_size + 1;
97 resolved = xmalloc (resolved_size);
98 resolved_len = resolvepath (name, resolved, resolved_size);
105 if (resolved_len < resolved_size)
112 /* NUL-terminate the resulting name. */
113 resolved[resolved_len] = '\0';
119 return canonicalize_filename_mode (name, CAN_EXISTING);
121 # endif /* !HAVE_RESOLVEPATH */
123 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
125 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
126 If *HT is not initialized, initialize it. */
128 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
132 size_t initial_capacity = 7;
133 *ht = hash_initialize (initial_capacity,
136 triple_compare_ino_str,
142 if (seen_file (*ht, filename, st))
145 record_file (*ht, filename, st);
149 /* Return the canonical absolute name of file NAME. A canonical name
150 does not contain any `.', `..' components nor any repeated file name
151 separators ('/') or symlinks. Whether components must exist
152 or not depends on canonicalize mode. The result is malloc'd. */
155 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
157 char *rname, *dest, *extra_buf = NULL;
160 char const *rname_limit;
161 size_t extra_len = 0;
162 Hash_table *ht = NULL;
166 __set_errno (EINVAL);
172 __set_errno (ENOENT);
181 dest = strchr (rname, '\0');
182 if (dest - rname < PATH_MAX)
184 char *p = xrealloc (rname, PATH_MAX);
185 dest = p + (dest - rname);
187 rname_limit = rname + PATH_MAX;
196 rname = xmalloc (PATH_MAX);
197 rname_limit = rname + PATH_MAX;
202 for (start = end = name; *start; start = end)
204 /* Skip sequence of multiple file name separators. */
205 while (*start == '/')
208 /* Find end of component. */
209 for (end = start; *end && *end != '/'; ++end)
212 if (end - start == 0)
214 else if (end - start == 1 && start[0] == '.')
216 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
218 /* Back up to previous component, ignore if at root already. */
219 if (dest > rname + 1)
220 while ((--dest)[-1] != '/');
229 if (dest + (end - start) >= rname_limit)
231 ptrdiff_t dest_offset = dest - rname;
232 size_t new_size = rname_limit - rname;
234 if (end - start + 1 > PATH_MAX)
235 new_size += end - start + 1;
237 new_size += PATH_MAX;
238 rname = xrealloc (rname, new_size);
239 rname_limit = rname + new_size;
241 dest = rname + dest_offset;
244 dest = memcpy (dest, start, end - start);
248 if (lstat (rname, &st) != 0)
250 if (can_mode == CAN_EXISTING)
252 if (can_mode == CAN_ALL_BUT_LAST && *end)
257 if (S_ISLNK (st.st_mode))
262 /* Detect loops. We cannot use the cycle-check module here,
263 since it's actually possible to encounter the same symlink
264 more than once in a given traversal. However, encountering
265 the same symlink,NAME pair twice does indicate a loop. */
266 if (seen_triple (&ht, name, &st))
269 if (can_mode == CAN_MISSING)
275 buf = areadlink_with_size (rname, st.st_size);
278 if (can_mode == CAN_MISSING && errno != ENOMEM)
290 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
291 extra_buf = xmalloc (extra_len);
293 else if ((n + len + 1) > extra_len)
295 extra_len = n + len + 1;
296 extra_buf = xrealloc (extra_buf, extra_len);
299 /* Careful here, end may be a pointer into extra_buf... */
300 memmove (&extra_buf[n], end, len + 1);
301 name = end = memcpy (extra_buf, buf, n);
304 dest = rname + 1; /* It's an absolute symlink */
306 /* Back up to previous component, ignore if at root already: */
307 if (dest > rname + 1)
308 while ((--dest)[-1] != '/');
314 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
322 if (dest > rname + 1 && dest[-1] == '/')