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 || GNULIB_CANONICALIZE_LGPL)
35 /* Return the canonical absolute name of file NAME. A canonical name
36 does not contain any `.', `..' components nor any repeated file name
37 separators ('/') or symlinks. All components must exist.
38 The result is malloc'd. */
41 canonicalize_file_name (const char *name)
43 return canonicalize_filename_mode (name, CAN_EXISTING);
45 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
47 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
48 If *HT is not initialized, initialize it. */
50 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
54 size_t initial_capacity = 7;
55 *ht = hash_initialize (initial_capacity,
58 triple_compare_ino_str,
64 if (seen_file (*ht, filename, st))
67 record_file (*ht, filename, st);
71 /* Return the canonical absolute name of file NAME, while treating
72 missing elements according to CAN_MODE. A canonical name
73 does not contain any `.', `..' components nor any repeated file name
74 separators ('/') or symlinks. Whether components must exist
75 or not depends on canonicalize mode. The result is malloc'd. */
78 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
80 char *rname, *dest, *extra_buf = NULL;
83 char const *rname_limit;
85 Hash_table *ht = NULL;
105 dest = strchr (rname, '\0');
106 if (dest - rname < PATH_MAX)
108 char *p = xrealloc (rname, PATH_MAX);
109 dest = p + (dest - rname);
111 rname_limit = rname + PATH_MAX;
120 rname = xmalloc (PATH_MAX);
121 rname_limit = rname + PATH_MAX;
126 for (start = name; *start; start = end)
128 /* Skip sequence of multiple file name separators. */
129 while (*start == '/')
132 /* Find end of component. */
133 for (end = start; *end && *end != '/'; ++end)
136 if (end - start == 0)
138 else if (end - start == 1 && start[0] == '.')
140 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
142 /* Back up to previous component, ignore if at root already. */
143 if (dest > rname + 1)
144 while ((--dest)[-1] != '/');
153 if (dest + (end - start) >= rname_limit)
155 ptrdiff_t dest_offset = dest - rname;
156 size_t new_size = rname_limit - rname;
158 if (end - start + 1 > PATH_MAX)
159 new_size += end - start + 1;
161 new_size += PATH_MAX;
162 rname = xrealloc (rname, new_size);
163 rname_limit = rname + new_size;
165 dest = rname + dest_offset;
168 dest = memcpy (dest, start, end - start);
172 if (lstat (rname, &st) != 0)
175 if (can_mode == CAN_EXISTING)
177 if (can_mode == CAN_ALL_BUT_LAST && *end)
182 if (S_ISLNK (st.st_mode))
187 /* Detect loops. We cannot use the cycle-check module here,
188 since it's actually possible to encounter the same symlink
189 more than once in a given traversal. However, encountering
190 the same symlink,NAME pair twice does indicate a loop. */
191 if (seen_triple (&ht, name, &st))
193 if (can_mode == CAN_MISSING)
199 buf = areadlink_with_size (rname, st.st_size);
202 if (can_mode == CAN_MISSING && errno != ENOMEM)
214 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
215 extra_buf = xmalloc (extra_len);
217 else if ((n + len + 1) > extra_len)
219 extra_len = n + len + 1;
220 extra_buf = xrealloc (extra_buf, extra_len);
223 /* Careful here, end may be a pointer into extra_buf... */
224 memmove (&extra_buf[n], end, len + 1);
225 name = end = memcpy (extra_buf, buf, n);
228 dest = rname + 1; /* It's an absolute symlink */
230 /* Back up to previous component, ignore if at root already: */
231 if (dest > rname + 1)
232 while ((--dest)[-1] != '/');
238 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
240 saved_errno = ENOTDIR;
246 if (dest > rname + 1 && dest[-1] == '/')