canonicalize: avoid resolvepath
[gnulib.git] / lib / canonicalize.c
1 /* Return the canonical absolute name of a given file.
2    Copyright (C) 1996-2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include "canonicalize.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "areadlink.h"
28 #include "file-set.h"
29 #include "hash-triple.h"
30 #include "pathmax.h"
31 #include "xalloc.h"
32 #include "xgetcwd.h"
33
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.  */
39
40 char *
41 canonicalize_file_name (const char *name)
42 {
43   return canonicalize_filename_mode (name, CAN_EXISTING);
44 }
45 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
46
47 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
48    If *HT is not initialized, initialize it.  */
49 static bool
50 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
51 {
52   if (*ht == NULL)
53     {
54       size_t initial_capacity = 7;
55       *ht = hash_initialize (initial_capacity,
56                             NULL,
57                             triple_hash,
58                             triple_compare_ino_str,
59                             triple_free);
60       if (*ht == NULL)
61         xalloc_die ();
62     }
63
64   if (seen_file (*ht, filename, st))
65     return true;
66
67   record_file (*ht, filename, st);
68   return false;
69 }
70
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.  */
76
77 char *
78 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
79 {
80   char *rname, *dest, *extra_buf = NULL;
81   char const *start;
82   char const *end;
83   char const *rname_limit;
84   size_t extra_len = 0;
85   Hash_table *ht = NULL;
86   int saved_errno;
87
88   if (name == NULL)
89     {
90       errno = EINVAL;
91       return NULL;
92     }
93
94   if (name[0] == '\0')
95     {
96       errno = ENOENT;
97       return NULL;
98     }
99
100   if (name[0] != '/')
101     {
102       rname = xgetcwd ();
103       if (!rname)
104         return NULL;
105       dest = strchr (rname, '\0');
106       if (dest - rname < PATH_MAX)
107         {
108           char *p = xrealloc (rname, PATH_MAX);
109           dest = p + (dest - rname);
110           rname = p;
111           rname_limit = rname + PATH_MAX;
112         }
113       else
114         {
115           rname_limit = dest;
116         }
117     }
118   else
119     {
120       rname = xmalloc (PATH_MAX);
121       rname_limit = rname + PATH_MAX;
122       rname[0] = '/';
123       dest = rname + 1;
124     }
125
126   for (start = name; *start; start = end)
127     {
128       /* Skip sequence of multiple file name separators.  */
129       while (*start == '/')
130         ++start;
131
132       /* Find end of component.  */
133       for (end = start; *end && *end != '/'; ++end)
134         /* Nothing.  */;
135
136       if (end - start == 0)
137         break;
138       else if (end - start == 1 && start[0] == '.')
139         /* nothing */;
140       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
141         {
142           /* Back up to previous component, ignore if at root already.  */
143           if (dest > rname + 1)
144             while ((--dest)[-1] != '/');
145         }
146       else
147         {
148           struct stat st;
149
150           if (dest[-1] != '/')
151             *dest++ = '/';
152
153           if (dest + (end - start) >= rname_limit)
154             {
155               ptrdiff_t dest_offset = dest - rname;
156               size_t new_size = rname_limit - rname;
157
158               if (end - start + 1 > PATH_MAX)
159                 new_size += end - start + 1;
160               else
161                 new_size += PATH_MAX;
162               rname = xrealloc (rname, new_size);
163               rname_limit = rname + new_size;
164
165               dest = rname + dest_offset;
166             }
167
168           dest = memcpy (dest, start, end - start);
169           dest += end - start;
170           *dest = '\0';
171
172           if (lstat (rname, &st) != 0)
173             {
174               saved_errno = errno;
175               if (can_mode == CAN_EXISTING)
176                 goto error;
177               if (can_mode == CAN_ALL_BUT_LAST && *end)
178                 goto error;
179               st.st_mode = 0;
180             }
181
182           if (S_ISLNK (st.st_mode))
183             {
184               char *buf;
185               size_t n, len;
186
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))
192                 {
193                   if (can_mode == CAN_MISSING)
194                     continue;
195                   saved_errno = ELOOP;
196                   goto error;
197                 }
198
199               buf = areadlink_with_size (rname, st.st_size);
200               if (!buf)
201                 {
202                   if (can_mode == CAN_MISSING && errno != ENOMEM)
203                     continue;
204                   saved_errno = errno;
205                   goto error;
206                 }
207
208               n = strlen (buf);
209               len = strlen (end);
210
211               if (!extra_len)
212                 {
213                   extra_len =
214                     ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
215                   extra_buf = xmalloc (extra_len);
216                 }
217               else if ((n + len + 1) > extra_len)
218                 {
219                   extra_len = n + len + 1;
220                   extra_buf = xrealloc (extra_buf, extra_len);
221                 }
222
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);
226
227               if (buf[0] == '/')
228                 dest = rname + 1;       /* It's an absolute symlink */
229               else
230                 /* Back up to previous component, ignore if at root already: */
231                 if (dest > rname + 1)
232                   while ((--dest)[-1] != '/');
233
234               free (buf);
235             }
236           else
237             {
238               if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
239                 {
240                   saved_errno = ENOTDIR;
241                   goto error;
242                 }
243             }
244         }
245     }
246   if (dest > rname + 1 && dest[-1] == '/')
247     --dest;
248   *dest = '\0';
249
250   free (extra_buf);
251   if (ht)
252     hash_free (ht);
253   return rname;
254
255 error:
256   free (extra_buf);
257   free (rname);
258   if (ht)
259     hash_free (ht);
260   errno = saved_errno;
261   return NULL;
262 }