canonicalize-lgpl: fix glibc bug with trailing slash
[pspp] / 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 && FUNC_REALPATH_WORKS)      \
35       || GNULIB_CANONICALIZE_LGPL)
36 /* Return the canonical absolute name of file NAME.  A canonical name
37    does not contain any `.', `..' components nor any repeated file name
38    separators ('/') or symlinks.  All components must exist.
39    The result is malloc'd.  */
40
41 char *
42 canonicalize_file_name (const char *name)
43 {
44   return canonicalize_filename_mode (name, CAN_EXISTING);
45 }
46 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
47
48 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
49    If *HT is not initialized, initialize it.  */
50 static bool
51 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
52 {
53   if (*ht == NULL)
54     {
55       size_t initial_capacity = 7;
56       *ht = hash_initialize (initial_capacity,
57                             NULL,
58                             triple_hash,
59                             triple_compare_ino_str,
60                             triple_free);
61       if (*ht == NULL)
62         xalloc_die ();
63     }
64
65   if (seen_file (*ht, filename, st))
66     return true;
67
68   record_file (*ht, filename, st);
69   return false;
70 }
71
72 /* Return the canonical absolute name of file NAME, while treating
73    missing elements according to CAN_MODE.  A canonical name
74    does not contain any `.', `..' components nor any repeated file name
75    separators ('/') or symlinks.  Whether components must exist
76    or not depends on canonicalize mode.  The result is malloc'd.  */
77
78 char *
79 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
80 {
81   char *rname, *dest, *extra_buf = NULL;
82   char const *start;
83   char const *end;
84   char const *rname_limit;
85   size_t extra_len = 0;
86   Hash_table *ht = NULL;
87   int saved_errno;
88
89   if (name == NULL)
90     {
91       errno = EINVAL;
92       return NULL;
93     }
94
95   if (name[0] == '\0')
96     {
97       errno = ENOENT;
98       return NULL;
99     }
100
101   if (name[0] != '/')
102     {
103       rname = xgetcwd ();
104       if (!rname)
105         return NULL;
106       dest = strchr (rname, '\0');
107       if (dest - rname < PATH_MAX)
108         {
109           char *p = xrealloc (rname, PATH_MAX);
110           dest = p + (dest - rname);
111           rname = p;
112           rname_limit = rname + PATH_MAX;
113         }
114       else
115         {
116           rname_limit = dest;
117         }
118     }
119   else
120     {
121       rname = xmalloc (PATH_MAX);
122       rname_limit = rname + PATH_MAX;
123       rname[0] = '/';
124       dest = rname + 1;
125     }
126
127   for (start = name; *start; start = end)
128     {
129       /* Skip sequence of multiple file name separators.  */
130       while (*start == '/')
131         ++start;
132
133       /* Find end of component.  */
134       for (end = start; *end && *end != '/'; ++end)
135         /* Nothing.  */;
136
137       if (end - start == 0)
138         break;
139       else if (end - start == 1 && start[0] == '.')
140         /* nothing */;
141       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
142         {
143           /* Back up to previous component, ignore if at root already.  */
144           if (dest > rname + 1)
145             while ((--dest)[-1] != '/');
146         }
147       else
148         {
149           struct stat st;
150
151           if (dest[-1] != '/')
152             *dest++ = '/';
153
154           if (dest + (end - start) >= rname_limit)
155             {
156               ptrdiff_t dest_offset = dest - rname;
157               size_t new_size = rname_limit - rname;
158
159               if (end - start + 1 > PATH_MAX)
160                 new_size += end - start + 1;
161               else
162                 new_size += PATH_MAX;
163               rname = xrealloc (rname, new_size);
164               rname_limit = rname + new_size;
165
166               dest = rname + dest_offset;
167             }
168
169           dest = memcpy (dest, start, end - start);
170           dest += end - start;
171           *dest = '\0';
172
173           if (lstat (rname, &st) != 0)
174             {
175               saved_errno = errno;
176               if (can_mode == CAN_EXISTING)
177                 goto error;
178               if (can_mode == CAN_ALL_BUT_LAST && *end)
179                 goto error;
180               st.st_mode = 0;
181             }
182
183           if (S_ISLNK (st.st_mode))
184             {
185               char *buf;
186               size_t n, len;
187
188               /* Detect loops.  We cannot use the cycle-check module here,
189                  since it's actually possible to encounter the same symlink
190                  more than once in a given traversal.  However, encountering
191                  the same symlink,NAME pair twice does indicate a loop.  */
192               if (seen_triple (&ht, name, &st))
193                 {
194                   if (can_mode == CAN_MISSING)
195                     continue;
196                   saved_errno = ELOOP;
197                   goto error;
198                 }
199
200               buf = areadlink_with_size (rname, st.st_size);
201               if (!buf)
202                 {
203                   if (can_mode == CAN_MISSING && errno != ENOMEM)
204                     continue;
205                   saved_errno = errno;
206                   goto error;
207                 }
208
209               n = strlen (buf);
210               len = strlen (end);
211
212               if (!extra_len)
213                 {
214                   extra_len =
215                     ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
216                   extra_buf = xmalloc (extra_len);
217                 }
218               else if ((n + len + 1) > extra_len)
219                 {
220                   extra_len = n + len + 1;
221                   extra_buf = xrealloc (extra_buf, extra_len);
222                 }
223
224               /* Careful here, end may be a pointer into extra_buf... */
225               memmove (&extra_buf[n], end, len + 1);
226               name = end = memcpy (extra_buf, buf, n);
227
228               if (buf[0] == '/')
229                 dest = rname + 1;       /* It's an absolute symlink */
230               else
231                 /* Back up to previous component, ignore if at root already: */
232                 if (dest > rname + 1)
233                   while ((--dest)[-1] != '/');
234
235               free (buf);
236             }
237           else
238             {
239               if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
240                 {
241                   saved_errno = ENOTDIR;
242                   goto error;
243                 }
244             }
245         }
246     }
247   if (dest > rname + 1 && dest[-1] == '/')
248     --dest;
249   *dest = '\0';
250
251   free (extra_buf);
252   if (ht)
253     hash_free (ht);
254   return rname;
255
256 error:
257   free (extra_buf);
258   free (rname);
259   if (ht)
260     hash_free (ht);
261   errno = saved_errno;
262   return NULL;
263 }