dccb4724c0bd9abd7c82e3552d2deb26a15daefc
[gnulib.git] / lib / canonicalize.c
1 /* Return the canonical absolute name of a given file.
2    Copyright (C) 1996-2006 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 2, or (at your option)
7    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; see the file COPYING.
16    If not, write to the Free Software Foundation,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 #include "canonicalize.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #if HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
29
30 #include <sys/stat.h>
31
32 #include <unistd.h>
33
34 #include <errno.h>
35 #include <stddef.h>
36
37 #include "cycle-check.h"
38 #include "filenamecat.h"
39 #include "stat-macros.h"
40 #include "xalloc.h"
41 #include "xgetcwd.h"
42
43 #ifndef __set_errno
44 # define __set_errno(Val) errno = (Val)
45 #endif
46
47 #include "pathmax.h"
48 #include "xreadlink.h"
49
50 #if !HAVE_CANONICALIZE_FILE_NAME
51 /* Return the canonical absolute name of file NAME.  A canonical name
52    does not contain any `.', `..' components nor any repeated file name
53    separators ('/') or symlinks.  All components must exist.
54    The result is malloc'd.  */
55
56 char *
57 canonicalize_file_name (const char *name)
58 {
59 # if HAVE_RESOLVEPATH
60
61   char *resolved, *extra_buf = NULL;
62   size_t resolved_size;
63   ssize_t resolved_len;
64
65   if (name == NULL)
66     {
67       __set_errno (EINVAL);
68       return NULL;
69     }
70
71   if (name[0] == '\0')
72     {
73       __set_errno (ENOENT);
74       return NULL;
75     }
76
77   /* All known hosts with resolvepath (e.g. Solaris 7) don't turn
78      relative names into absolute ones, so prepend the working
79      directory if the file name is not absolute.  */
80   if (name[0] != '/')
81     {
82       char *wd;
83
84       if (!(wd = xgetcwd ()))
85         return NULL;
86
87       extra_buf = file_name_concat (wd, name, NULL);
88       name = extra_buf;
89       free (wd);
90     }
91
92   resolved_size = strlen (name);
93   while (1)
94     {
95       resolved_size = 2 * resolved_size + 1;
96       resolved = xmalloc (resolved_size);
97       resolved_len = resolvepath (name, resolved, resolved_size);
98       if (resolved_len < 0)
99         {
100           free (resolved);
101           free (extra_buf);
102           return NULL;
103         }
104       if (resolved_len < resolved_size)
105         break;
106       free (resolved);
107     }
108
109   free (extra_buf);
110
111   /* NUL-terminate the resulting name.  */
112   resolved[resolved_len] = '\0';
113
114   return resolved;
115
116 # else
117
118   return canonicalize_filename_mode (name, CAN_EXISTING);
119
120 # endif /* !HAVE_RESOLVEPATH */
121 }
122 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
123
124 /* Return the canonical absolute name of file NAME.  A canonical name
125    does not contain any `.', `..' components nor any repeated file name
126    separators ('/') or symlinks.  Whether components must exist
127    or not depends on canonicalize mode.  The result is malloc'd.  */
128
129 char *
130 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
131 {
132   char *rname, *dest, *extra_buf = NULL;
133   char const *start;
134   char const *end;
135   char const *rname_limit;
136   size_t extra_len = 0;
137   struct cycle_check_state cycle_state;
138
139   if (name == NULL)
140     {
141       __set_errno (EINVAL);
142       return NULL;
143     }
144
145   if (name[0] == '\0')
146     {
147       __set_errno (ENOENT);
148       return NULL;
149     }
150
151   if (name[0] != '/')
152     {
153       rname = xgetcwd ();
154       if (!rname)
155         return NULL;
156       dest = strchr (rname, '\0');
157       if (dest - rname < PATH_MAX)
158         {
159           char *p = xrealloc (rname, PATH_MAX);
160           dest = p + (dest - rname);
161           rname = p;
162           rname_limit = rname + PATH_MAX;
163         }
164       else
165         {
166           rname_limit = dest;
167         }
168     }
169   else
170     {
171       rname = xmalloc (PATH_MAX);
172       rname_limit = rname + PATH_MAX;
173       rname[0] = '/';
174       dest = rname + 1;
175     }
176
177   cycle_check_init (&cycle_state);
178   for (start = end = name; *start; start = end)
179     {
180       /* Skip sequence of multiple file name separators.  */
181       while (*start == '/')
182         ++start;
183
184       /* Find end of component.  */
185       for (end = start; *end && *end != '/'; ++end)
186         /* Nothing.  */;
187
188       if (end - start == 0)
189         break;
190       else if (end - start == 1 && start[0] == '.')
191         /* nothing */;
192       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
193         {
194           /* Back up to previous component, ignore if at root already.  */
195           if (dest > rname + 1)
196             while ((--dest)[-1] != '/');
197         }
198       else
199         {
200           struct stat st;
201
202           if (dest[-1] != '/')
203             *dest++ = '/';
204
205           if (dest + (end - start) >= rname_limit)
206             {
207               ptrdiff_t dest_offset = dest - rname;
208               size_t new_size = rname_limit - rname;
209
210               if (end - start + 1 > PATH_MAX)
211                 new_size += end - start + 1;
212               else
213                 new_size += PATH_MAX;
214               rname = xrealloc (rname, new_size);
215               rname_limit = rname + new_size;
216
217               dest = rname + dest_offset;
218             }
219
220           dest = memcpy (dest, start, end - start);
221           dest += end - start;
222           *dest = '\0';
223
224           if (lstat (rname, &st) != 0)
225             {
226               if (can_mode == CAN_EXISTING)
227                 goto error;
228               if (can_mode == CAN_ALL_BUT_LAST && *end)
229                 goto error;
230               st.st_mode = 0;
231             }
232
233           if (S_ISLNK (st.st_mode))
234             {
235               char *buf;
236               size_t n, len;
237
238               if (cycle_check (&cycle_state, &st))
239                 {
240                   __set_errno (ELOOP);
241                   if (can_mode == CAN_MISSING)
242                     continue;
243                   else
244                     goto error;
245                 }
246
247               buf = xreadlink (rname, st.st_size);
248               if (!buf)
249                 {
250                   if (can_mode == CAN_MISSING)
251                     continue;
252                   else
253                     goto error;
254                 }
255
256               n = strlen (buf);
257               len = strlen (end);
258
259               if (!extra_len)
260                 {
261                   extra_len =
262                     ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
263                   extra_buf = xmalloc (extra_len);
264                 }
265               else if ((n + len + 1) > extra_len)
266                 {
267                   extra_len = n + len + 1;
268                   extra_buf = xrealloc (extra_buf, extra_len);
269                 }
270
271               /* Careful here, end may be a pointer into extra_buf... */
272               memmove (&extra_buf[n], end, len + 1);
273               name = end = memcpy (extra_buf, buf, n);
274
275               if (buf[0] == '/')
276                 dest = rname + 1;       /* It's an absolute symlink */
277               else
278                 /* Back up to previous component, ignore if at root already: */
279                 if (dest > rname + 1)
280                   while ((--dest)[-1] != '/');
281
282               free (buf);
283             }
284           else
285             {
286               if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
287                 {
288                   errno = ENOTDIR;
289                   goto error;
290                 }
291             }
292         }
293     }
294   if (dest > rname + 1 && dest[-1] == '/')
295     --dest;
296   *dest = '\0';
297
298   free (extra_buf);
299   return rname;
300
301 error:
302   free (extra_buf);
303   free (rname);
304   return NULL;
305 }