Debian Codegraph @

libidu/walker.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* walker.c -- nifty file-tree walker
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <fnmatch.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <alloca.h>
28 #include <xalloc.h>
29 #include <error.h>
30 #include <pathmax.h>
31 #include <sys/stat.h>
32
33 #include "xnls.h"
34 #include "idfile.h"
35 #include "dynvec.h"
36 #include "scanners.h"
37 #include "iduglobal.h"
38
39 int walker_verbose_flag = 0;
40 off_t largest_member_file = 0;
41
42 static char **vectorize_string (char *string, char const *delimiter_class);
43 static int walk_dir (struct file_link *dir_link);
44 static struct member_file *get_member_file (struct file_link *flink);
45 static struct lang_args *get_lang_args (struct file_link const *flink);
46 static void print_member_file (struct member_file *member);
47 static int walk_sub_dirs (struct dynvec *sub_dirs_vec);
48 static void reparent_children (struct file_link *dlink, struct file_link *slink);
49 static int classify_link (struct file_link *flink, struct stat *stp);
50 static struct file_link *get_link_from_dirent (struct dirent *dirent,
51 struct file_link *parent);
52 static struct file_link *make_link_from_dirent (struct dirent *dirent,
53 struct file_link *parent);
54 static struct file_link *get_link_from_string (char const *name,
55 struct file_link *parent);
56 static struct file_link *make_link_from_string (char const *name,
57 struct file_link *parent);
58 static int lang_wanted (char const *lang_name);
59 static char **append_strings_to_vector (char **vector_0, char *string,
60 char const *delimiter_class);
61 static int string_in_vector (char const *string, char **vector);
62 static int same_as_dot (char const *cwd);
63 static struct file_link const
64 **fill_link_vector (struct file_link const **vec_buf,
65 struct file_link const *flink);
66 static struct file_link const
67 **fill_link_vector_1 (struct file_link const **vec_buf,
68 struct file_link const *flink);
69 static char *fill_dot_dots (char *buf, int levels);
70 static char *absolute_file_name_1 (char *buffer, struct file_link const *flink);
71 static unsigned long member_file_hash_1 (void const *key);
72 static unsigned long member_file_hash_2 (void const *key);
73 static int member_file_hash_compare (void const *x, void const *y);
74 static unsigned long file_link_hash_1 (void const *key);
75 static unsigned long file_link_hash_2 (void const *key);
76 static int file_link_hash_compare (void const *x, void const *y);
77 static unsigned long dev_ino_hash_1 (void const *key);
78 static unsigned long dev_ino_hash_2 (void const *key);
79 static int dev_ino_hash_compare (void const *x, void const *y);
80 static int symlink_ancestry (struct file_link *flink);
81
82 #if HAVE_LINK
83
84 static struct file_link *find_alias_link (struct file_link *flink,
85 struct stat *stp);
86 static struct member_file *maybe_get_member_file (struct file_link *flink,
87 struct stat *stp);
88
89 #endif
90
91 #define IS_DOT(s) ((s)[0] == '.' && (s)[1] == '\0')
92 #define IS_DOT_DOT(s) ((s)[0] == '.' && (s)[1] == '.' && (s)[2] == '\0')
93 #define IS_DOT_or_DOT_DOT(s) \
94 (((s)[0] == '.') && (((s)[1] == '\0') || ((s)[1] == '.' && (s)[2] == '\0')))
95
96 static struct file_link *current_dir_link = 0;
97
98 static char const white_space[] = " \t\r\n\v\f";
99
100 char* xgetcwd (void);
101
102
103 /****************************************************************************/
104 /* Walk the file-system tree rooted at `dir_link', looking for files
105 that are eligible for scanning. */
106
107 static int
108 walk_dir (struct file_link *dir_link)
109 {
110 int scannable_files;
111 struct dynvec *sub_dirs_vec;
112 DIR *dirp;
113
114 if (!chdir_to_link (dir_link))
115 return 0;
116 dirp = opendir (".");
117 if (dirp == 0)
118 {
119 char *file_name = alloca (PATH_MAX);
120 absolute_file_name (file_name, dir_link);
121 error (0, errno, _("can't read directory `%s' (`.' from `%s')"), file_name, xgetcwd ());
122 return 0;
123 }
124 sub_dirs_vec = make_dynvec (32);
125 scannable_files = 0;
126 for (;;)
127 {
128 struct file_link *flink;
129 struct dirent *dirent = readdir (dirp);
130
131 if (dirent == 0)
132 break;
133 if (IS_DOT_or_DOT_DOT (dirent->d_name))
134 continue;
135
136 flink = get_link_from_dirent (dirent, dir_link);
137 if (!(flink->fl_flags & FL_PRUNE))
138 walk_flink (flink, sub_dirs_vec);
139 }
140 closedir (dirp);
141
142 scannable_files += walk_sub_dirs (sub_dirs_vec);
143 dynvec_free (sub_dirs_vec);
144 return scannable_files;
145 }
146
147 /* Walk the directories found by walk_dir, calling walk_dir
148 recursively for each directory. */
149
150 static int
151 walk_sub_dirs (struct dynvec *sub_dirs_vec)
152 {
153 struct file_link **sub_dirs;
154 struct file_link **sub_dirs_end;
155 int total_scannable_files = 0;
156
157 dynvec_freeze (sub_dirs_vec);
158 sub_dirs_end = (struct file_link **)
159 &sub_dirs_vec->dv_vec[sub_dirs_vec->dv_fill];
160 sub_dirs = (struct file_link **) sub_dirs_vec->dv_vec;
161 for ( ; sub_dirs < sub_dirs_end; sub_dirs++)
162 {
163 struct file_link *sub_dir_link = *sub_dirs;
164 int scannable_files = walk_dir (sub_dir_link);
165 if (scannable_files)
166 total_scannable_files += scannable_files;
167 }
168 return total_scannable_files;
169 }
170
171 void
172 walk_flink (struct file_link *flink, struct dynvec *sub_dirs_vec)
173 {
174 struct stat st;
175 unsigned int old_flags;
176 unsigned int new_flags;
177
178 new_flags = classify_link (flink, &st);
179 if (new_flags == 0)
180 return;
181
182 old_flags = flink->fl_flags;
183 if ((old_flags & FL_TYPE_MASK)
184 && (old_flags & FL_TYPE_MASK) != (new_flags & FL_TYPE_MASK))
185 {
186 char *file_name = alloca (PATH_MAX);
187 absolute_file_name (file_name, flink);
188 error (0, 0, _("notice: `%s' was a %s, but is now a %s!"), file_name,
189 (FL_IS_FILE (old_flags) ? _("file") : _("directory")),
190 (FL_IS_FILE (new_flags) ? _("file") : _("directory")));
191 }
192
193 flink->fl_flags = (old_flags & ~(FL_TYPE_MASK|FL_SYM_LINK)) | new_flags;
194 if (FL_IS_DIR (new_flags))
195 {
196
197 struct file_link *alias_link;
198 #if HAVE_LINK
199 alias_link = find_alias_link (flink, &st);
200 #else
201 alias_link = 0;
202 #endif /* HAVE_LINK */
203
204 if (alias_link)
205 {
206 if (!(new_flags & FL_SYM_LINK))
207 reparent_children (flink, alias_link);
208 }
209 else if (sub_dirs_vec == 0)
210 walk_dir (flink);
211 else
212 dynvec_append (sub_dirs_vec, flink);
213 }
214 else
215 {
216 struct member_file *member;
217 #if HAVE_LINK
218 member = maybe_get_member_file (flink, &st);
219 #else
220 member = get_member_file (flink);
221 #endif
222 if (member)
223 {
224 if (st.st_size > largest_member_file)
225 largest_member_file = st.st_size;
226 if (walker_verbose_flag)
227 print_member_file (member);
228 }
229 }
230 }
231
232 /* Take child file_link nodes from a symlinked directory and give them
233 to a hard linked directory. This is something of a pain since a
234 file_link's parent node is part of its hash-table key. We must
235 search the entire hash-table for the children. With each child, we
236 must delete it, reset its parent link, then reinsert. */
237
238 static void
239 reparent_children (struct file_link *dlink, struct file_link *slink)
240 {
241 void **slot = idh.idh_file_link_table.ht_vec;
242 void **end = &idh.idh_file_link_table.ht_vec[idh.idh_file_link_table.ht_size];
243
244 for ( ; slot < end; slot++)
245 {
246 if (!HASH_VACANT (*slot))
247 {
248 struct file_link *child = (struct file_link *) *slot;
249 if (child->fl_parent == slink)
250 {
251 void **new_slot;
252 *slot = hash_deleted_item;
253 child->fl_parent = dlink;
254 new_slot = hash_find_slot (&idh.idh_file_link_table, child);
255 *new_slot = child;
256 }
257 }
258 }
259 }
260
261
262 /****************************************************************************/
263 /* Separate the wheat from the chaff. Mark those file_links that are
264 components in member files. */
265
266 void
267 mark_member_file_links (struct idhead *idhp)
268 {
269 struct member_file **members_0
270 = (struct member_file **) hash_dump (&idhp->idh_member_file_table,
271 0, member_file_qsort_compare);
272 struct member_file **end = &members_0[idhp->idh_member_file_table.ht_fill];
273 struct member_file **members;
274 long new_index = 0;
275
276 for (members = members_0; members < end; members++)
277 {
278 struct member_file *member = *members;
279 struct file_link *flink;
280 member->mf_index = new_index++;
281 for (flink = member->mf_link;
282 !(flink->fl_flags & FL_USED); flink = flink->fl_parent)
283 flink->fl_flags |= FL_USED;
284 }
285 free (members_0);
286 }
287
288
289 #if HAVE_LINK
290
291 /****************************************************************************/
292 /* Return a `member_file' for this `flink' *if* the filename matches
293 some scan pattern, and no alias for the file takes precedence ([1]
294 hard-links dominate symbolic-links; [2] for two hard-links: first
295 come, first served). */
296
297 static struct member_file *
298 maybe_get_member_file (struct file_link *flink, struct stat *stp)
299 {
300 struct file_link *alias_link;
301 struct member_file *member;
302 struct member_file *alias_member = 0;
303
304 member = get_member_file (flink);
305 alias_link = find_alias_link (flink, stp);
306 if (alias_link)
307 alias_member = find_member_file (alias_link);
308
309 if (member && alias_member)
310 {
311 int ancestry = symlink_ancestry (flink);
312 int alias_ancestry = symlink_ancestry (alias_link);
313 if (member->mf_lang_args != alias_member->mf_lang_args)
314 {
315 char *file_name = alloca (PATH_MAX);
316 char *alias_file_name = alloca (PATH_MAX);
317 absolute_file_name (file_name, flink);
318 absolute_file_name (alias_file_name, alias_link);
319 error (0, 0, _("warning: `%s' and `%s' are the same file, but yield different scans!"),
320 file_name, alias_file_name);
321 }
322 else if (alias_ancestry > ancestry)
323 {
324 hash_delete (&idh.idh_member_file_table, member);
325 member->mf_link->fl_flags &= ~FL_MEMBER;
326 return 0;
327 }
328 else
329 {
330 hash_delete (&idh.idh_member_file_table, alias_member);
331 alias_member->mf_link->fl_flags &= ~FL_MEMBER;
332 }
333 }
334 return member;
335 }
336
337 /* Return a previously registered alias for `flink', if any. */
338
339 static struct file_link *
340 find_alias_link (struct file_link *flink, struct stat *stp)
341 {
342 struct dev_ino *dev_ino;
343 struct dev_ino **slot;
344
345 dev_ino = (struct dev_ino *) obstack_alloc (&idh.idh_dev_ino_obstack, sizeof (struct dev_ino));
346 dev_ino->di_dev = stp->st_dev;
347 dev_ino->di_ino = stp->st_ino;
348 slot = (struct dev_ino **) hash_find_slot (&idh.idh_dev_ino_table, dev_ino);
349 if (HASH_VACANT (*slot))
350 {
351 dev_ino->di_link = flink;
352 hash_insert_at (&idh.idh_dev_ino_table, dev_ino, slot);
353 return 0;
354 }
355 else
356 {
357 obstack_free (&idh.idh_dev_ino_obstack, dev_ino);
358 return (*slot)->di_link;
359 }
360 }
361
362 /* Return the distance from `flink' to a symbolic-link ancestor
363 directory. PATH_MAX is considered an infinite distance (e.g.,
364 there are no symlinks between `flink' and the root). */
365
366 static int _GL_ATTRIBUTE_PURE
367 symlink_ancestry (struct file_link *flink)
368 {
369 int ancestry = 0;
370 while (!IS_ROOT_FILE_LINK (flink))
371 {
372 if (flink->fl_flags & FL_SYM_LINK)
373 return ancestry;
374 ancestry++;
375 flink = flink->fl_parent;
376 }
377 return PATH_MAX;
378 }
379
380 #endif /* HAVE_LINK */
381
382 static struct member_file *
383 get_member_file (struct file_link *flink)
384 {
385 struct member_file *member;
386 struct member_file **slot;
387 struct lang_args const *args;
388
389 args = get_lang_args (flink);
390 if (args == 0)
391 return 0;
392 if (!lang_wanted (args->la_language->lg_name))
393 return 0;
394
395 member = (struct member_file *) obstack_alloc (&idh.idh_member_file_obstack,
396 sizeof (struct member_file));
397 member->mf_link = flink;
398 slot = (struct member_file **) hash_find_slot (&idh.idh_member_file_table, member);
399 if (HASH_VACANT (*slot))
400 {
401 member->mf_index = -1;
402 hash_insert_at (&idh.idh_member_file_table, member, slot);
403 flink->fl_flags |= FL_MEMBER;
404 }
405 else
406 {
407 obstack_free (&idh.idh_member_file_obstack, member);
408 #if 0
409 if (member->mf_lang_args != args)
410 {
411 char *file_name = alloca (PATH_MAX);
412 absolute_file_name (file_name, flink);
413 error (0, 0, _("notice: scan parameters changed for `%s'"), file_name);
414 member->mf_old_index = -1;
415 }
416 #endif
417 member = *slot;
418 }
419 member->mf_lang_args = args;
420 return member;
421 }
422
423 struct member_file *
424 find_member_file (struct file_link const *flink)
425 {
426 struct member_file key;
427 struct member_file **slot;
428
429 key.mf_link = (struct file_link *) flink;
430 slot = (struct member_file **) hash_find_slot (&idh.idh_member_file_table, &key);
431 if (HASH_VACANT (*slot))
432 return 0;
433 return *slot;
434 }
435
436 /* March down the list of lang_args, and return the first one whose
437 pattern matches FLINK. Return the matching lang_args, if a
438 scanner exists for that language, otherwise return 0. */
439
440 static struct lang_args *
441 get_lang_args (struct file_link const *flink)
442 {
443 struct lang_args *args = lang_args_list;
444 char *file_name = alloca (PATH_MAX);
445
446 while (args)
447 {
448 if (strchr (args->la_pattern, SLASH_CHAR))
449 {
450 absolute_file_name (file_name, flink);
451 if (fnmatch (args->la_pattern, file_name, MAYBE_FNM_CASEFOLD | FNM_FILE_NAME) == 0)
452 return (args->la_language ? args : 0);
453 }
454 else
455 {
456 if (fnmatch (args->la_pattern, flink->fl_name, MAYBE_FNM_CASEFOLD) == 0)
457 return (args->la_language ? args : 0);
458 }
459 args = args->la_next;
460 }
461 return ((lang_args_default && lang_args_default->la_language)
462 ? lang_args_default : 0);
463 }
464
465 static void
466 print_member_file (struct member_file *member)
467 {
468 char *file_name = alloca (PATH_MAX);
469 absolute_file_name (file_name, member->mf_link);
470 printf ("%lu: %s: %s\n", idh.idh_member_file_table.ht_fill - 1,
471 member->mf_lang_args->la_language->lg_name, file_name);
472 }
473
474
475 /****************************************************************************/
476
477 static char **langs_included;
478 static char **langs_excluded;
479
480 static int _GL_ATTRIBUTE_PURE
481 lang_wanted (char const *lang_name)
482 {
483 if (langs_excluded)
484 return !string_in_vector (lang_name, langs_excluded);
485 else if (langs_included)
486 return string_in_vector (lang_name, langs_included);
487 else
488 return 1;
489 }
490
491 void
492 include_languages (char *lang_names)
493 {
494 if (langs_excluded)
495 error (EXIT_FAILURE, 0, _("can't mix --include and --exclude options"));
496 langs_included = append_strings_to_vector (langs_included, lang_names, white_space);
497 }
498
499 void
500 exclude_languages (char *lang_names)
501 {
502 if (langs_included)
503 error (EXIT_FAILURE, 0, _("can't mix --include and --exclude options"));
504 langs_excluded = append_strings_to_vector (langs_excluded, lang_names, white_space);
505 }
506
507 static int _GL_ATTRIBUTE_PURE
508 vector_length (char **vector)
509 {
510 int length = 0;
511 while (*vector++)
512 length++;
513 return length;
514 }
515
516 static char **
517 append_strings_to_vector (char **vector_0, char *string,
518 char const *delimiter_class)
519 {
520 char **vector;
521 if (vector_0)
522 {
523 int length = vector_length (vector_0);
524 vector_0 = xnrealloc (vector_0, length + 2 + strlen (string) / 2,
525 sizeof *vector_0);
526 vector = &vector_0[length];
527 }
528 else
529 vector = vector_0 = xnmalloc (2 + strlen (string) / 2, sizeof *vector);
530
531 do
532 *vector = strsep (&string, delimiter_class);
533 while (*vector++);
534 return xnrealloc (vector_0, vector - vector_0, sizeof *vector_0);
535 }
536
537 static int _GL_ATTRIBUTE_PURE
538 string_in_vector (char const *string, char **vector)
539 {
540 while (*vector)
541 if (strequ (string, *vector++))
542 return 1;
543 return 0;
544 }
545
546
547 /****************************************************************************/
548 /* Convert a file name string to an absolute chain of `file_link's. */
549
550 struct file_link *
551 parse_file_name (char *file_name, struct file_link *relative_dir_link)
552 {
553 struct file_link *flink;
554 char **links_0;
555 char **links;
556
557 if (IS_ABSOLUTE (file_name))
558 {
559 #if HAVE_LINK
560 flink = get_link_from_string (SLASH_STRING, 0);
561 #else
562 flink = 0;
563 #endif
564 }
565 else if (relative_dir_link)
566 flink = relative_dir_link;
567 else if (current_dir_link)
568 flink = current_dir_link;
569 else
570 flink = get_current_dir_link ();
571
572 links = links_0 = vectorize_string (file_name, SLASH_STRING);
573 while (*links)
574 {
575 char const* link_name = *links++;
576 if (*link_name == '\0' || IS_DOT (link_name))
577 ;
578 else if (IS_DOT_DOT (link_name))
579 flink = flink->fl_parent;
580 else
581 {
582 struct stat st;
583 flink = get_link_from_string (link_name, flink);
584 if (!flink->fl_flags)
585 {
586 flink->fl_flags = classify_link (flink, &st);
587 if (!flink->fl_flags)
588 return 0;
589 }
590 }
591 }
592 free (links_0);
593 return flink;
594 }
595
596 /* Return an absolute chain of `file_link's representing the current
597 working directory. */
598
599 struct file_link *
600 get_current_dir_link (void)
601 {
602 struct file_link *dir_link;
603 char *cwd;
604 char **links_0;
605 char **links;
606
607 if (current_dir_link)
608 return current_dir_link;
609
610 cwd = getenv ("PWD");
611 cwd = same_as_dot (cwd) ? xstrdup (cwd) : xgetcwd ();
612 if (cwd == NULL)
613 error (EXIT_FAILURE, errno, _("can't get working directory"));
614 #if HAVE_LINK
615 dir_link = get_link_from_string (SLASH_STRING, 0);
616 dir_link->fl_flags = (dir_link->fl_flags & ~FL_TYPE_MASK) | FL_TYPE_DIR;
617 #else
618 dir_link = 0;
619 #endif
620 links = links_0 = vectorize_string (cwd, SLASH_STRING);
621 while (*links)
622 {
623 struct stat st;
624 char const* link_name = *links++;
625 dir_link = get_link_from_string (link_name, dir_link);
626 if (!dir_link->fl_flags)
627 dir_link->fl_flags = classify_link (dir_link, &st);
628 }
629 chdir_to_link (dir_link);
630 free (links_0);
631 free (cwd);
632 current_dir_link = dir_link;
633 return dir_link;
634 }
635
636 static int
637 same_as_dot (char const *cwd)
638 {
639 struct stat cwd_st;
640 struct stat dot_st;
641
642 if (cwd == 0 || *cwd != '/'
643 || stat (cwd, &cwd_st) < 0
644 || stat (".", &dot_st) < 0)
645 return 0;
646 return ((cwd_st.st_ino == dot_st.st_ino) && (cwd_st.st_dev == dot_st.st_dev));
647 }
648
649 /* Change the working directory to the directory represented by
650 `dir_link'. Choose the shortest path to the destination based on
651 the cached value of the current directory. */
652
653 int
654 chdir_to_link (struct file_link *dir_link)
655 {
656 char *to_dir_name = alloca (PATH_MAX);
657
658 if (current_dir_link == dir_link)
659 return 1;
660
661 if (current_dir_link)
662 maybe_relative_file_name (to_dir_name, dir_link, current_dir_link);
663 else
664 absolute_file_name (to_dir_name, dir_link);
665 if (chdir (to_dir_name) < 0)
666 {
667 if (IS_ABSOLUTE (to_dir_name))
668 error (0, errno, _("can't chdir to `%s'"), to_dir_name);
669 else
670 {
671 char *from_dir_name = alloca (PATH_MAX);
672 absolute_file_name (from_dir_name, current_dir_link);
673 error (0, errno, _("can't chdir to `%s' from `%s'"), to_dir_name, from_dir_name);
674 }
675 return 0;
676 }
677 else
678 {
679 current_dir_link = dir_link;
680 return 1;
681 }
682 }
683
684 static char **
685 vectorize_string (char *string, char const *delimiter_class)
686 {
687 char **vector_0 = xmalloc (sizeof(char *) * (2 + strlen (string) / 2));
688 char **vector = vector_0;
689
690 *vector++ = strsep (&string, delimiter_class);
691 if (vector[-1])
692 {
693 /* Toss first field if empty */
694 if (vector[-1][0] == '\0')
695 vector--;
696 do
697 *vector = strsep (&string, delimiter_class);
698 while (*vector++);
699 }
700 return xnrealloc (vector_0, vector - vector_0, sizeof *vector_0);
701 }
702
703 void
704 prune_file_names (char *str, struct file_link *from_link)
705 {
706 char **file_names_0 = vectorize_string (str, white_space);
707 char **file_names = file_names_0;
708
709 while (*file_names)
710 {
711 struct file_link *flink = parse_file_name (*file_names++, from_link);
712 if (flink)
713 flink->fl_flags |= FL_PRUNE;
714 }
715 free (file_names_0);
716 }
717
718
719 /****************************************************************************/
720 /* Gather information about the link at `flink'. If it's a good
721 file or directory, return its mod-time and type. */
722
723 static int
724 classify_link (struct file_link *flink, struct stat *stp)
725 {
726 unsigned int flags = 0;
727
728 if (!chdir_to_link (flink->fl_parent))
729 return 0;
730
731 #ifdef S_IFLNK
732 if (lstat (flink->fl_name, stp) < 0)
733 {
734 error (0, errno, _("can't lstat `%s' from `%s'"), flink->fl_name, xgetcwd ());
735 return 0;
736 }
737 if (S_ISLNK (stp->st_mode))
738 {
739 #endif
740 if (stat (flink->fl_name, stp) < 0)
741 {
742 error (0, errno, _("can't stat `%s' from `%s'"), flink->fl_name, xgetcwd ());
743 return 0;
744 }
745 #ifdef S_IFLNK
746 flags |= FL_SYM_LINK;
747 }
748 #endif
749 if (S_ISDIR (stp->st_mode))
750 flags |= FL_TYPE_DIR;
751 else if (stp->st_size == 0)
752 return 0;
753 else if (S_ISREG (stp->st_mode))
754 flags |= FL_TYPE_FILE;
755 else
756 return 0;
757 return flags;
758 }
759
760
761 /****************************************************************************/
762 /* Retrieve an existing flink; or if none exists, create one. */
763
764 static struct file_link *
765 get_link_from_dirent (struct dirent *dirent, struct file_link *parent)
766 {
767 struct file_link **slot;
768 struct file_link *new_link;
769
770 new_link = make_link_from_dirent (dirent, parent);
771 slot = (struct file_link **) hash_find_slot (&idh.idh_file_link_table, new_link);
772 if (HASH_VACANT (*slot))
773 slot = (struct file_link **) hash_insert_at (&idh.idh_file_link_table,
774 new_link, slot);
775 else
776 obstack_free (&idh.idh_file_link_obstack, new_link);
777 return *slot;
778 }
779
780 static struct file_link *
781 get_link_from_string (char const *name, struct file_link *parent)
782 {
783 struct file_link **slot;
784 struct file_link *new_link;
785
786 new_link = make_link_from_string (name, parent);
787 slot = (struct file_link **) hash_find_slot (&idh.idh_file_link_table, new_link);
788 if (HASH_VACANT (*slot))
789 slot = (struct file_link **) hash_insert_at (&idh.idh_file_link_table,
790 new_link, slot);
791 else
792 obstack_free (&idh.idh_file_link_obstack, new_link);
793 return *slot;
794 }
795
796 struct file_link *
797 make_link_from_dirent (struct dirent* dirent, struct file_link *parent)
798 {
799 struct file_link *flink;
800
801 flink = (struct file_link *) obstack_alloc (&idh.idh_file_link_obstack,
802 sizeof (struct file_link) + strlen (dirent->d_name));
803 strcpy (flink->fl_name, dirent->d_name);
804 flink->fl_parent = parent ? parent : flink;
805 flink->fl_flags = 0;
806
807 return flink;
808 }
809
810 static struct file_link *
811 make_link_from_string (char const* name, struct file_link *parent)
812 {
813 struct file_link *flink;
814
815 flink = (struct file_link *) obstack_alloc (&idh.idh_file_link_obstack,
816 sizeof (struct file_link) + strlen (name));
817 strcpy (flink->fl_name, name);
818 flink->fl_parent = parent ? parent : flink;
819 flink->fl_flags = 0;
820
821 return flink;
822 }
823
824
825 /****************************************************************************/
826 /* Convert a `file_link' chain to a vector of component `file_link's,
827 with the root at [0]. Return a pointer beyond the final component. */
828
829 static struct file_link const **
830 fill_link_vector (struct file_link const **vec_buf,
831 struct file_link const *flink)
832 {
833 vec_buf = fill_link_vector_1 (vec_buf, flink);
834 *vec_buf = 0;
835 return vec_buf;
836 }
837
838 static struct file_link const **
839 fill_link_vector_1 (struct file_link const **vec_buf,
840 struct file_link const *flink)
841 {
842 if (!IS_ROOT_FILE_LINK (flink))
843 vec_buf = fill_link_vector_1 (vec_buf, flink->fl_parent);
844 *vec_buf++ = flink;
845 return vec_buf;
846 }
847
848
849 /****************************************************************************/
850 /* Fill BUF_0 with a path to TO_LINK. If a relative path from
851 FROM_LINK is possible (i.e., no intervening symbolic-links) and
852 shorter, return the relative path; otherwise, return an absolute
853 path. */
854
855 char *
856 maybe_relative_file_name (char *buf_0, struct file_link const *to_link, struct file_link const *from_link)
857 {
858 struct file_link const **to_link_vec_0 = alloca (sizeof (struct file_link const *) * (PATH_MAX/2));
859 struct file_link const **from_link_vec_0 = alloca (sizeof (struct file_link const *) * (PATH_MAX/2));
860 struct file_link const **to_link_vec = to_link_vec_0;
861 struct file_link const **from_link_vec = from_link_vec_0;
862 struct file_link const **from_link_end;
863 struct file_link const **from_links;
864 int alloc_me = (buf_0 == 0);
865 char *buf;
866 int levels;
867
868 if (from_link == 0)
869 from_link = current_dir_link;
870
871 if (alloc_me)
872 buf_0 = xmalloc (PATH_MAX);
873
874 /* Optimize common cases. */
875 if (to_link == from_link)
876 strcpy (buf_0, ".");
877 else if (to_link->fl_parent == from_link)
878 strcpy (buf_0, to_link->fl_name);
879 else if (from_link->fl_flags & FL_SYM_LINK)
880 absolute_file_name (buf_0, to_link);
881 else if (to_link == from_link->fl_parent)
882 strcpy (buf_0, "..");
883 else if (to_link->fl_parent == from_link->fl_parent)
884 {
885 strcpy (buf_0, DOT_DOT_SLASH);
886 strcpy (&buf_0[3], to_link->fl_name);
887 }
888 else
889 {
890 from_link_end = fill_link_vector (from_link_vec, from_link);
891 fill_link_vector (to_link_vec, to_link);
892 while (*to_link_vec == *from_link_vec)
893 {
894 if (*to_link_vec == 0)
895 {
896 strcpy (buf_0, ".");
897 goto out;
898 }
899 to_link_vec++;
900 from_link_vec++;
901 }
902 levels = from_link_end - from_link_vec;
903 if (levels >= (from_link_vec - from_link_vec_0))
904 {
905 absolute_file_name (buf_0, to_link);
906 goto out;
907 }
908 for (from_links = from_link_vec; *from_links; from_links++)
909 if ((*from_links)->fl_flags & FL_SYM_LINK)
910 {
911 absolute_file_name (buf_0, to_link);
912 goto out;
913 }
914 buf = fill_dot_dots (buf_0, levels);
915 if (*to_link_vec == 0)
916 *--buf = '\0';
917 else
918 {
919 do
920 {
921 strcpy (buf, (*to_link_vec)->fl_name);
922 buf += strlen (buf);
923 if ((*to_link_vec)->fl_name[0] != SLASH_CHAR && *++to_link_vec)
924 *buf++ = SLASH_CHAR;
925 }
926 while (*to_link_vec);
927 }
928 }
929 out:
930 if (alloc_me)
931 buf_0 = xrealloc (buf_0, (1 + strlen (buf_0)));
932 return buf_0;
933 }
934
935 /* Fill `buf' with sequences of "../" in order to ascend so many
936 `levels' in a directory tree. */
937
938 static char *
939 fill_dot_dots (char *buf, int levels)
940 {
941 while (levels--)
942 {
943 strcpy (buf, DOT_DOT_SLASH);
944 buf += 3;
945 }
946 return buf;
947 }
948
949
950 /****************************************************************************/
951 /* Fill `buffer' with the absolute path to `flink'. */
952
953 char *
954 absolute_file_name (char *buf_0, struct file_link const *flink)
955 {
956 char *end;
957 int alloc_me = (buf_0 == 0);
958
959 if (alloc_me)
960 buf_0 = xmalloc (PATH_MAX);
961 end = absolute_file_name_1 (buf_0, flink);
962 /* Clip the trailing slash. */
963 #if HAVE_LINK
964 if (end > &buf_0[1])
965 end--;
966 #else
967 if (end > &buf_0[3])
968 end--;
969 #endif
970 *end++ = '\0';
971 if (alloc_me)
972 buf_0 = xrealloc (buf_0, (size_t)(end - buf_0));
973 return buf_0;
974 }
975
976 static char *
977 absolute_file_name_1 (char *buf, struct file_link const *flink)
978 {
979 char *end;
980 if (IS_ROOT_FILE_LINK (flink))
981 end = buf;
982 else
983 end = absolute_file_name_1 (buf, flink->fl_parent);
984 strcpy (end, flink->fl_name);
985 if (*end == SLASH_CHAR)
986 end++;
987 else
988 {
989 end += strlen (end);
990 *end++ = SLASH_CHAR;
991 }
992 return end;
993 }
994
995
996 /****************************************************************************/
997 /* Hash stuff for `struct member_file'. */
998
999 static unsigned long
1000 member_file_hash_1 (void const *key)
1001 {
1002 return_ADDRESS_HASH_1 (((struct member_file const *) key)->mf_link);
1003 }
1004
1005 static unsigned long
1006 member_file_hash_2 (void const *key)
1007 {
1008 return_ADDRESS_HASH_2 (((struct member_file const *) key)->mf_link);
1009 }
1010
1011 static int
1012 member_file_hash_compare (void const *x, void const *y)
1013 {
1014 return_ADDRESS_COMPARE (((struct member_file const *) x)->mf_link,
1015 ((struct member_file const *) y)->mf_link);
1016 }
1017
1018 /* Collating sequence:
1019 - language.map index
1020 - mf_link: breadth-first, alphabetical */
1021
1022 int
1023 member_file_qsort_compare (void const *x, void const *y)
1024 {
1025 struct member_file const *mfx = *(struct member_file const *const *) x;
1026 struct member_file const *mfy = *(struct member_file const *const *) y;
1027 int result;
1028
1029 INTEGER_COMPARE (mfx->mf_lang_args->la_index, mfy->mf_lang_args->la_index, result);
1030 if (result)
1031 return result;
1032 else
1033 {
1034 struct file_link const *flx = mfx->mf_link;
1035 struct file_link const *fly = mfy->mf_link;
1036 if (flx->fl_parent == fly->fl_parent)
1037 return strcmp (flx->fl_name, fly->fl_name);
1038 result = (links_depth (flx) - links_depth (fly));
1039 if (result)
1040 return result;
1041 while (flx->fl_parent != fly->fl_parent)
1042 {
1043 flx = flx->fl_parent;
1044 fly = fly->fl_parent;
1045 }
1046 return strcmp (flx->fl_name, fly->fl_name);
1047 }
1048 }
1049
1050 /****************************************************************************/
1051 /* Hash stuff for `struct file_link'. */
1052
1053 static unsigned long _GL_ATTRIBUTE_PURE
1054 file_link_hash_1 (void const *key)
1055 {
1056 unsigned long result = 0;
1057 struct file_link const *parent = (IS_ROOT_FILE_LINK (((struct file_link const *) key))
1058 ? 0 : ((struct file_link const *) key)->fl_parent);
1059 STRING_HASH_1 (((struct file_link const *) key)->fl_name, result);
1060 ADDRESS_HASH_1 (parent, result);
1061 return result;
1062 }
1063
1064 static unsigned long _GL_ATTRIBUTE_PURE
1065 file_link_hash_2 (void const *key)
1066 {
1067 unsigned long result = 0;
1068 struct file_link const *parent = (IS_ROOT_FILE_LINK (((struct file_link const *) key))
1069 ? 0 : ((struct file_link const *) key)->fl_parent);
1070 STRING_HASH_2 (((struct file_link const *) key)->fl_name, result);
1071 ADDRESS_HASH_2 (parent, result);
1072 return result;
1073 }
1074
1075 static int _GL_ATTRIBUTE_PURE
1076 file_link_hash_compare (void const *x, void const *y)
1077 {
1078 int result;
1079 struct file_link const *x_parent = (IS_ROOT_FILE_LINK (((struct file_link const *) x))
1080 ? 0 : ((struct file_link const *) x)->fl_parent);
1081 struct file_link const *y_parent = (IS_ROOT_FILE_LINK (((struct file_link const *) y))
1082 ? 0 : ((struct file_link const *) y)->fl_parent);
1083 ADDRESS_COMPARE (x_parent, y_parent, result);
1084 if (result)
1085 return result;
1086 STRING_COMPARE (((struct file_link const *) x)->fl_name,
1087 ((struct file_link const *) y)->fl_name, result);
1088 return result;
1089 }
1090
1091 /* Count directory components between flink and its root. */
1092
1093 int
1094 links_depth (struct file_link const *flink)
1095 {
1096 int depth = 0;
1097 while (!IS_ROOT_FILE_LINK (flink))
1098 {
1099 depth++;
1100 flink = flink->fl_parent;
1101 }
1102 return depth;
1103 }
1104
1105 #if HAVE_LINK
1106
1107 /****************************************************************************/
1108 /* Hash stuff for `struct dev_ino'. */
1109
1110 /* FIXME: consider dumping all of this in favor of gnulib's di-set module. */
1111 /* Expand to the definition of a function that hashes a dev+inode pair,
1112 with application of XFORM. */
1113 #define DEV_INO_HASH_DEFUN(FN_NAME, XFORM) \
1114 static size_t _GL_ATTRIBUTE_PURE \
1115 FN_NAME (void const *x) \
1116 { \
1117 struct dev_ino const *p = x; \
1118 ino_t ino = p->di_ino; \
1119 \
1120 /* When INO is wider than size_t, XOR the words of INO into H. \
1121 This avoids loss of info, without applying % to the wider type, \
1122 which could be quite slow on some systems. */ \
1123 size_t h = XFORM (ino); \
1124 unsigned int n_words = sizeof ino / sizeof h + (sizeof ino % sizeof h != 0); \
1125 for (unsigned int i = 1; i < n_words; i++) \
1126 h ^= XFORM (ino >> CHAR_BIT * sizeof h * i); \
1127 \
1128 dev_t dev = p->di_dev; \
1129 h ^= XFORM (dev); \
1130 n_words = sizeof dev / sizeof h + (sizeof dev % sizeof h != 0); \
1131 for (unsigned int i = 1; i < n_words; i++) \
1132 h ^= XFORM (dev >> CHAR_BIT * sizeof h * i); \
1133 \
1134 return h; \
1135 }
1136 static inline size_t xform_NOP (size_t k) { return k; }
1137 static inline size_t xform_NOT (size_t k) { return ~k; }
1138 DEV_INO_HASH_DEFUN(dev_ino_hash_1, xform_NOP)
1139 DEV_INO_HASH_DEFUN(dev_ino_hash_2, xform_NOT)
1140
1141 static int
1142 dev_ino_hash_compare (void const *xv, void const *yv)
1143 {
1144 struct dev_ino const *x = xv;
1145 struct dev_ino const *y = yv;
1146 return x->di_ino == y->di_ino && x->di_dev == y->di_dev;
1147 }
1148
1149 #endif
1150
1151 /*******************************************************************/
1152
1153 struct file_link *
1154 init_walker (struct idhead *idhp)
1155 {
1156 init_idh_obstacks (idhp);
1157 init_idh_tables (idhp);
1158 return get_current_dir_link ();
1159 }
1160
1161 void
1162 init_idh_obstacks (struct idhead *idhp)
1163 {
1164 obstack_init (&idhp->idh_member_file_obstack);
1165 obstack_init (&idhp->idh_file_link_obstack);
1166 #if HAVE_LINK
1167 obstack_init (&idhp->idh_dev_ino_obstack);
1168 #endif
1169 }
1170
1171 void
1172 init_idh_tables (struct idhead *idhp)
1173 {
1174 hash_init (&idhp->idh_member_file_table, 16*1024,
1175 member_file_hash_1, member_file_hash_2, member_file_hash_compare);
1176 hash_init (&idhp->idh_file_link_table, 16*1024,
1177 file_link_hash_1, file_link_hash_2, file_link_hash_compare);
1178 #if HAVE_LINK
1179 hash_init (&idhp->idh_dev_ino_table, 16*1024,
1180 dev_ino_hash_1, dev_ino_hash_2, dev_ino_hash_compare);
1181 #endif
1182 }