1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Return the canonical absolute name of a given file.
4 Copyright (C) 1996-2010 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "canonicalize.h"
29 #include "areadlink.h"
31 #include "hash-triple.h"
36 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
37 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
40 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
41 || GNULIB_CANONICALIZE_LGPL)
42 /* Return the canonical absolute name of file NAME. A canonical name
43 does not contain any `.', `..' components nor any repeated file name
44 separators ('/') or symlinks. All components must exist.
45 The result is malloc'd. */
48 canonicalize_file_name (const char *name)
50 return canonicalize_filename_mode (name, CAN_EXISTING);
52 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
54 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
55 If *HT is not initialized, initialize it. */
57 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
61 size_t initial_capacity = 7;
62 *ht = hash_initialize (initial_capacity,
65 triple_compare_ino_str,
71 if (seen_file (*ht, filename, st))
74 record_file (*ht, filename, st);
78 /* Return the canonical absolute name of file NAME, while treating
79 missing elements according to CAN_MODE. A canonical name
80 does not contain any `.', `..' components nor any repeated file name
81 separators ('/') or symlinks. Whether components must exist
82 or not depends on canonicalize mode. The result is malloc'd. */
85 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
87 char *rname, *dest, *extra_buf = NULL;
90 char const *rname_limit;
92 Hash_table *ht = NULL;
112 dest = strchr (rname, '\0');
113 if (dest - rname < PATH_MAX)
115 char *p = xrealloc (rname, PATH_MAX);
116 dest = p + (dest - rname);
118 rname_limit = rname + PATH_MAX;
127 rname = xmalloc (PATH_MAX);
128 rname_limit = rname + PATH_MAX;
131 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
135 for (start = name; *start; start = end)
137 /* Skip sequence of multiple file name separators. */
138 while (*start == '/')
141 /* Find end of component. */
142 for (end = start; *end && *end != '/'; ++end)
145 if (end - start == 0)
147 else if (end - start == 1 && start[0] == '.')
149 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
151 /* Back up to previous component, ignore if at root already. */
152 if (dest > rname + 1)
153 while ((--dest)[-1] != '/');
154 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
165 if (dest + (end - start) >= rname_limit)
167 ptrdiff_t dest_offset = dest - rname;
168 size_t new_size = rname_limit - rname;
170 if (end - start + 1 > PATH_MAX)
171 new_size += end - start + 1;
173 new_size += PATH_MAX;
174 rname = xrealloc (rname, new_size);
175 rname_limit = rname + new_size;
177 dest = rname + dest_offset;
180 dest = memcpy (dest, start, end - start);
184 if (lstat (rname, &st) != 0)
187 if (can_mode == CAN_EXISTING)
189 if (can_mode == CAN_ALL_BUT_LAST)
191 if (end[strspn (end, "/")] || saved_errno != ENOENT)
198 if (S_ISLNK (st.st_mode))
203 /* Detect loops. We cannot use the cycle-check module here,
204 since it's actually possible to encounter the same symlink
205 more than once in a given traversal. However, encountering
206 the same symlink,NAME pair twice does indicate a loop. */
207 if (seen_triple (&ht, name, &st))
209 if (can_mode == CAN_MISSING)
215 buf = areadlink_with_size (rname, st.st_size);
218 if (can_mode == CAN_MISSING && errno != ENOMEM)
230 ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
231 extra_buf = xmalloc (extra_len);
233 else if ((n + len + 1) > extra_len)
235 extra_len = n + len + 1;
236 extra_buf = xrealloc (extra_buf, extra_len);
239 /* Careful here, end may be a pointer into extra_buf... */
240 memmove (&extra_buf[n], end, len + 1);
241 name = end = memcpy (extra_buf, buf, n);
245 dest = rname + 1; /* It's an absolute symlink */
246 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
251 /* Back up to previous component, ignore if at root
253 if (dest > rname + 1)
254 while ((--dest)[-1] != '/');
255 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
264 if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
266 saved_errno = ENOTDIR;
272 if (dest > rname + 1 && dest[-1] == '/')
274 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && *dest == '/')
277 if (rname_limit != dest + 1)
278 rname = xrealloc (rname, dest - rname + 1);