This source file includes following definitions.
- file_accessible
- suffix_requires_dir_check
- dir_check
- canonicalize_file_name
- multiple_bits_set
- seen_triple
- canonicalize_filename_mode_stk
- canonicalize_filename_mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include "canonicalize.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #include <filename.h>
29 #include <idx.h>
30 #include <intprops.h>
31 #include <scratch_buffer.h>
32
33 #include "attribute.h"
34 #include "file-set.h"
35 #include "hash-triple.h"
36 #include "xalloc.h"
37
38
39 #if defined GCC_LINT || defined lint
40 # define IF_LINT(Code) Code
41 #else
42 # define IF_LINT(Code)
43 #endif
44
45 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
46 # define DOUBLE_SLASH_IS_DISTINCT_ROOT false
47 #endif
48
49 #if ISSLASH ('\\')
50 # define SLASHES "/\\"
51 #else
52 # define SLASHES "/"
53 #endif
54
55
56
57 static bool
58 file_accessible (char const *file)
59 {
60 # if HAVE_FACCESSAT
61 return faccessat (AT_FDCWD, file, F_OK, AT_EACCESS) == 0;
62 # else
63 struct stat st;
64 return stat (file, &st) == 0 || errno == EOVERFLOW;
65 # endif
66 }
67
68
69
70
71
72
73
74
75 static bool _GL_ATTRIBUTE_PURE
76 suffix_requires_dir_check (char const *end)
77 {
78
79 while (ISSLASH (*end))
80 {
81
82 do
83 end++;
84 while (ISSLASH (*end));
85
86 switch (*end++)
87 {
88 default: return false;
89 case '\0': return true;
90 case '.': break;
91 }
92
93 if (!*end || (*end == '.' && (!end[1] || ISSLASH (end[1]))))
94 return true;
95 }
96
97 return false;
98 }
99
100
101
102
103
104
105 #ifdef LSTAT_FOLLOWS_SLASHED_SYMLINK
106 static char const dir_suffix[] = "/";
107 #else
108 static char const dir_suffix[] = "/./";
109 #endif
110
111
112
113
114
115 static bool
116 dir_check (char *dir, char *dirend)
117 {
118 strcpy (dirend, dir_suffix);
119 return file_accessible (dir);
120 }
121
122 #if !((HAVE_CANONICALIZE_FILE_NAME && FUNC_REALPATH_WORKS) \
123 || GNULIB_CANONICALIZE_LGPL)
124
125
126
127
128
129 char *
130 canonicalize_file_name (const char *name)
131 {
132 return canonicalize_filename_mode (name, CAN_EXISTING);
133 }
134 #endif
135
136 static bool
137 multiple_bits_set (canonicalize_mode_t i)
138 {
139 return (i & (i - 1)) != 0;
140 }
141
142
143
144 static bool
145 seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
146 {
147 if (*ht == NULL)
148 {
149 idx_t initial_capacity = 7;
150 *ht = hash_initialize (initial_capacity,
151 NULL,
152 triple_hash,
153 triple_compare_ino_str,
154 triple_free);
155 if (*ht == NULL)
156 xalloc_die ();
157 }
158
159 if (seen_file (*ht, filename, st))
160 return true;
161
162 record_file (*ht, filename, st);
163 return false;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178 #if _GL_GNUC_PREREQ (10, 1)
179 # if defined GCC_LINT || defined lint
180 __attribute__ ((__noinline__))
181 # elif __OPTIMIZE__ && !__NO_INLINE__
182 # define GCC_BOGUS_WRETURN_LOCAL_ADDR
183 # endif
184 #endif
185 static char *
186 canonicalize_filename_mode_stk (const char *name, canonicalize_mode_t can_mode,
187 struct scratch_buffer *rname_buf)
188 {
189 char *dest;
190 char const *start;
191 char const *end;
192 Hash_table *ht = NULL;
193 bool logical = (can_mode & CAN_NOLINKS) != 0;
194 int num_links = 0;
195
196 canonicalize_mode_t can_exist = can_mode & CAN_MODE_MASK;
197 if (multiple_bits_set (can_exist))
198 {
199 errno = EINVAL;
200 return NULL;
201 }
202
203 if (name == NULL)
204 {
205 errno = EINVAL;
206 return NULL;
207 }
208
209 if (name[0] == '\0')
210 {
211 errno = ENOENT;
212 return NULL;
213 }
214
215 struct scratch_buffer extra_buffer, link_buffer;
216 scratch_buffer_init (&extra_buffer);
217 scratch_buffer_init (&link_buffer);
218 scratch_buffer_init (rname_buf);
219 char *rname_on_stack = rname_buf->data;
220 char *rname = rname_on_stack;
221 bool end_in_extra_buffer = false;
222 bool failed = true;
223
224
225
226 idx_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
227
228 if (!IS_ABSOLUTE_FILE_NAME (name))
229 {
230 while (!getcwd (rname, rname_buf->length))
231 {
232 switch (errno)
233 {
234 case ERANGE:
235 if (scratch_buffer_grow (rname_buf))
236 break;
237 FALLTHROUGH;
238 case ENOMEM:
239 xalloc_die ();
240
241 default:
242 dest = rname;
243 goto error;
244 }
245 rname = rname_buf->data;
246 }
247 dest = rawmemchr (rname, '\0');
248 start = name;
249 prefix_len = FILE_SYSTEM_PREFIX_LEN (rname);
250 }
251 else
252 {
253 dest = mempcpy (rname, name, prefix_len);
254 *dest++ = '/';
255 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
256 {
257 if (prefix_len == 0
258 && ISSLASH (name[1]) && !ISSLASH (name[2]))
259 {
260 *dest++ = '/';
261 #if defined _WIN32 && !defined __CYGWIN__
262
263
264 {
265 idx_t i;
266 for (i = 2; name[i] != '\0' && !ISSLASH (name[i]); )
267 i++;
268 if (name[i] != '\0'
269 && i + 1 < rname_buf->length)
270 {
271 prefix_len = i;
272 memcpy (dest, name + 2, i - 2 + 1);
273 dest += i - 2 + 1;
274 }
275 else
276 {
277
278
279
280
281 }
282 }
283 #endif
284 }
285 *dest = '\0';
286 }
287 start = name + prefix_len;
288 }
289
290 for ( ; *start; start = end)
291 {
292
293 while (ISSLASH (*start))
294 ++start;
295
296
297 for (end = start; *end && !ISSLASH (*end); ++end)
298 ;
299
300
301
302 idx_t startlen = end - start;
303
304 if (startlen == 0)
305 break;
306 else if (startlen == 1 && start[0] == '.')
307 ;
308 else if (startlen == 2 && start[0] == '.' && start[1] == '.')
309 {
310
311 if (dest > rname + prefix_len + 1)
312 for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest)
313 continue;
314 if (DOUBLE_SLASH_IS_DISTINCT_ROOT
315 && dest == rname + 1 && !prefix_len
316 && ISSLASH (*dest) && !ISSLASH (dest[1]))
317 dest++;
318 }
319 else
320 {
321 if (!ISSLASH (dest[-1]))
322 *dest++ = '/';
323
324 while (rname + rname_buf->length - dest
325 < startlen + sizeof dir_suffix)
326 {
327 idx_t dest_offset = dest - rname;
328 if (!scratch_buffer_grow_preserve (rname_buf))
329 xalloc_die ();
330 rname = rname_buf->data;
331 dest = rname + dest_offset;
332 }
333
334 dest = mempcpy (dest, start, startlen);
335 *dest = '\0';
336
337 char *buf;
338 ssize_t n = -1;
339 if (!logical)
340 {
341 while (true)
342 {
343 buf = link_buffer.data;
344 idx_t bufsize = link_buffer.length;
345 n = readlink (rname, buf, bufsize - 1);
346 if (n < bufsize - 1)
347 break;
348 if (!scratch_buffer_grow (&link_buffer))
349 xalloc_die ();
350 }
351 }
352 if (0 <= n)
353 {
354
355
356 if (num_links < 20)
357 num_links++;
358 else if (*start)
359 {
360
361
362
363
364
365 struct stat st;
366 dest[- startlen] = '\0';
367 if (stat (*rname ? rname : ".", &st) != 0)
368 goto error;
369 dest[- startlen] = *start;
370
371
372
373
374
375
376 if (seen_triple (&ht, start, &st))
377 {
378 if (can_exist == CAN_MISSING)
379 continue;
380 errno = ELOOP;
381 goto error;
382 }
383 }
384
385 buf[n] = '\0';
386
387 char *extra_buf = extra_buffer.data;
388 idx_t end_idx IF_LINT (= 0);
389 if (end_in_extra_buffer)
390 end_idx = end - extra_buf;
391 size_t len = strlen (end);
392 if (INT_ADD_OVERFLOW (len, n))
393 xalloc_die ();
394 while (extra_buffer.length <= len + n)
395 {
396 if (!scratch_buffer_grow_preserve (&extra_buffer))
397 xalloc_die ();
398 extra_buf = extra_buffer.data;
399 }
400 if (end_in_extra_buffer)
401 end = extra_buf + end_idx;
402
403
404 memmove (&extra_buf[n], end, len + 1);
405 name = end = memcpy (extra_buf, buf, n);
406 end_in_extra_buffer = true;
407
408 if (IS_ABSOLUTE_FILE_NAME (buf))
409 {
410 idx_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf);
411
412 dest = mempcpy (rname, buf, pfxlen);
413 *dest++ = '/';
414 if (DOUBLE_SLASH_IS_DISTINCT_ROOT)
415 {
416 if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen)
417 *dest++ = '/';
418 *dest = '\0';
419 }
420
421 prefix_len = pfxlen;
422 }
423 else
424 {
425
426
427 if (dest > rname + prefix_len + 1)
428 for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest)
429 continue;
430 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1
431 && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len)
432 dest++;
433 }
434 }
435 else if (! (can_exist == CAN_MISSING
436 || (suffix_requires_dir_check (end)
437 ? dir_check (rname, dest)
438 : !logical
439 ? errno == EINVAL
440 : *end || file_accessible (rname))
441 || (can_exist == CAN_ALL_BUT_LAST
442 && errno == ENOENT
443 && !end[strspn (end, SLASHES)])))
444 goto error;
445 }
446 }
447 if (dest > rname + prefix_len + 1 && ISSLASH (dest[-1]))
448 --dest;
449 if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && !prefix_len
450 && ISSLASH (*dest) && !ISSLASH (dest[1]))
451 dest++;
452 failed = false;
453
454 error:
455 if (ht)
456 hash_free (ht);
457 scratch_buffer_free (&extra_buffer);
458 scratch_buffer_free (&link_buffer);
459
460 if (failed)
461 {
462 scratch_buffer_free (rname_buf);
463 return NULL;
464 }
465
466 *dest++ = '\0';
467 char *result = scratch_buffer_dupfree (rname_buf, dest - rname);
468 if (!result)
469 xalloc_die ();
470 return result;
471 }
472
473
474
475
476
477
478
479
480 char *
481 canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
482 {
483 #ifdef GCC_BOGUS_WRETURN_LOCAL_ADDR
484 #warning "GCC might issue a bogus -Wreturn-local-addr warning here."
485 #warning "See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644>."
486 #endif
487 struct scratch_buffer rname_buffer;
488 return canonicalize_filename_mode_stk (name, can_mode, &rname_buffer);
489 }