]> git.cworth.org Git - tar/blob - gnu/canonicalize.c
Imported Upstream version 1.23
[tar] / gnu / canonicalize.c
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.
5
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.
10
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.
15
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/>.  */
18
19 #include <config.h>
20
21 #include "canonicalize.h"
22
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 #include "areadlink.h"
30 #include "file-set.h"
31 #include "hash-triple.h"
32 #include "pathmax.h"
33 #include "xalloc.h"
34 #include "xgetcwd.h"
35
36 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
37 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
38 #endif
39
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.  */
46
47 char *
48 canonicalize_file_name (const char *name)
49 {
50   return canonicalize_filename_mode (name, CAN_EXISTING);
51 }
52 #endif /* !HAVE_CANONICALIZE_FILE_NAME */
53
54 /* Return true if we've already seen the triple, <FILENAME, dev, ino>.
55    If *HT is not initialized, initialize it.  */
56 static bool
57 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
58 {
59   if (*ht == NULL)
60     {
61       size_t initial_capacity = 7;
62       *ht = hash_initialize (initial_capacity,
63                             NULL,
64                             triple_hash,
65                             triple_compare_ino_str,
66                             triple_free);
67       if (*ht == NULL)
68         xalloc_die ();
69     }
70
71   if (seen_file (*ht, filename, st))
72     return true;
73
74   record_file (*ht, filename, st);
75   return false;
76 }
77
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.  */
83
84 char *
85 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
86 {
87   char *rname, *dest, *extra_buf = NULL;
88   char const *start;
89   char const *end;
90   char const *rname_limit;
91   size_t extra_len = 0;
92   Hash_table *ht = NULL;
93   int saved_errno;
94
95   if (name == NULL)
96     {
97       errno = EINVAL;
98       return NULL;
99     }
100
101   if (name[0] == '\0')
102     {
103       errno = ENOENT;
104       return NULL;
105     }
106
107   if (name[0] != '/')
108     {
109       rname = xgetcwd ();
110       if (!rname)
111         return NULL;
112       dest = strchr (rname, '\0');
113       if (dest - rname < PATH_MAX)
114         {
115           char *p = xrealloc (rname, PATH_MAX);
116           dest = p + (dest - rname);
117           rname = p;
118           rname_limit = rname + PATH_MAX;
119         }
120       else
121         {
122           rname_limit = dest;
123         }
124     }
125   else
126     {
127       rname = xmalloc (PATH_MAX);
128       rname_limit = rname + PATH_MAX;
129       rname[0] = '/';
130       dest = rname + 1;
131       if (DOUBLE_SLASH_IS_DISTINCT_ROOT && name[1] == '/')
132         *dest++ = '/';
133     }
134
135   for (start = name; *start; start = end)
136     {
137       /* Skip sequence of multiple file name separators.  */
138       while (*start == '/')
139         ++start;
140
141       /* Find end of component.  */
142       for (end = start; *end && *end != '/'; ++end)
143         /* Nothing.  */;
144
145       if (end - start == 0)
146         break;
147       else if (end - start == 1 && start[0] == '.')
148         /* nothing */;
149       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
150         {
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
155               && *dest == '/')
156             dest++;
157         }
158       else
159         {
160           struct stat st;
161
162           if (dest[-1] != '/')
163             *dest++ = '/';
164
165           if (dest + (end - start) >= rname_limit)
166             {
167               ptrdiff_t dest_offset = dest - rname;
168               size_t new_size = rname_limit - rname;
169
170               if (end - start + 1 > PATH_MAX)
171                 new_size += end - start + 1;
172               else
173                 new_size += PATH_MAX;
174               rname = xrealloc (rname, new_size);
175               rname_limit = rname + new_size;
176
177               dest = rname + dest_offset;
178             }
179
180           dest = memcpy (dest, start, end - start);
181           dest += end - start;
182           *dest = '\0';
183
184           if (lstat (rname, &st) != 0)
185             {
186               saved_errno = errno;
187               if (can_mode == CAN_EXISTING)
188                 goto error;
189               if (can_mode == CAN_ALL_BUT_LAST)
190                 {
191                   if (end[strspn (end, "/")] || saved_errno != ENOENT)
192                     goto error;
193                   continue;
194                 }
195               st.st_mode = 0;
196             }
197
198           if (S_ISLNK (st.st_mode))
199             {
200               char *buf;
201               size_t n, len;
202
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))
208                 {
209                   if (can_mode == CAN_MISSING)
210                     continue;
211                   saved_errno = ELOOP;
212                   goto error;
213                 }
214
215               buf = areadlink_with_size (rname, st.st_size);
216               if (!buf)
217                 {
218                   if (can_mode == CAN_MISSING && errno != ENOMEM)
219                     continue;
220                   saved_errno = errno;
221                   goto error;
222                 }
223
224               n = strlen (buf);
225               len = strlen (end);
226
227               if (!extra_len)
228                 {
229                   extra_len =
230                     ((n + len + 1) > PATH_MAX) ? (n + len + 1) : PATH_MAX;
231                   extra_buf = xmalloc (extra_len);
232                 }
233               else if ((n + len + 1) > extra_len)
234                 {
235                   extra_len = n + len + 1;
236                   extra_buf = xrealloc (extra_buf, extra_len);
237                 }
238
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);
242
243               if (buf[0] == '/')
244                 {
245                   dest = rname + 1;     /* It's an absolute symlink */
246                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && buf[1] == '/')
247                     *dest++ = '/';
248                 }
249               else
250                 {
251                   /* Back up to previous component, ignore if at root
252                      already: */
253                   if (dest > rname + 1)
254                     while ((--dest)[-1] != '/');
255                   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
256                       && *dest == '/')
257                     dest++;
258                 }
259
260               free (buf);
261             }
262           else
263             {
264               if (!S_ISDIR (st.st_mode) && *end && (can_mode != CAN_MISSING))
265                 {
266                   saved_errno = ENOTDIR;
267                   goto error;
268                 }
269             }
270         }
271     }
272   if (dest > rname + 1 && dest[-1] == '/')
273     --dest;
274   if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && *dest == '/')
275     dest++;
276   *dest = '\0';
277   if (rname_limit != dest + 1)
278     rname = xrealloc (rname, dest - rname + 1);
279
280   free (extra_buf);
281   if (ht)
282     hash_free (ht);
283   return rname;
284
285 error:
286   free (extra_buf);
287   free (rname);
288   if (ht)
289     hash_free (ht);
290   errno = saved_errno;
291   return NULL;
292 }