Debian Codegraph @

libidu/walker.c

raw symbols ◀ ▶ SCIP
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 if (*links && **links) /* Not in / */
622 while (*links)
623 {
624 struct stat st;
625 char const* link_name = *links++;
626 dir_link = get_link_from_string (link_name, dir_link);
627 if (!dir_link->fl_flags)
628 dir_link->fl_flags = classify_link (dir_link, &st);
629 }
630 chdir_to_link (dir_link);
631 free (links_0);
632 free (cwd);
633 current_dir_link = dir_link;
634 return dir_link;
635 }
636
637 static int
638 same_as_dot (char const *cwd)
639 {
640 struct stat cwd_st;
641 struct stat dot_st;
642
643 if (cwd == 0 || *cwd != '/'
644 || stat (cwd, &cwd_st) < 0
645 || stat (".", &dot_st) < 0)
646 return 0;
647 return ((cwd_st.st_ino == dot_st.st_ino) && (cwd_st.st_dev == dot_st.st_dev));
648 }
649
650 /* Change the working directory to the directory represented by
651 `dir_link'. Choose the shortest path to the destination based on
652 the cached value of the current directory. */
653
654 int
655 chdir_to_link (struct file_link *dir_link)
656 {
657 char *to_dir_name = alloca (PATH_MAX);
658
659 if (current_dir_link == dir_link)
660 return 1;
661
662 if (current_dir_link)
663 maybe_relative_file_name (to_dir_name, dir_link, current_dir_link);
664 else
665 absolute_file_name (to_dir_name, dir_link);
666 if (chdir (to_dir_name) < 0)
667 {
668 if (IS_ABSOLUTE (to_dir_name))
669 error (0, errno, _("can't chdir to `%s'"), to_dir_name);
670 else
671 {
672 char *from_dir_name = alloca (PATH_MAX);
673 absolute_file_name (from_dir_name, current_dir_link);
674 error (0, errno, _("can't chdir to `%s' from `%s'"), to_dir_name, from_dir_name);
675 }
676 return 0;
677 }
678 else
679 {
680 current_dir_link = dir_link;
681 return 1;
682 }
683 }
684
685 static char **
686 vectorize_string (char *string, char const *delimiter_class)
687 {
688 char **vector_0 = xmalloc (sizeof(char *) * (2 + strlen (string) / 2));
689 char **vector = vector_0;
690
691 *vector++ = strsep (&string, delimiter_class);
692 if (vector[-1])
693 {
694 /* Toss first field if empty */
695 if (vector[-1][0] == '\0')
696 vector--;
697 do
698 *vector = strsep (&string, delimiter_class);
699 while (*vector++);
700 }
701 return xnrealloc (vector_0, vector - vector_0, sizeof *vector_0);
702 }
703
704 void
705 prune_file_names (char *str, struct file_link *from_link)
706 {
707 char **file_names_0 = vectorize_string (str, white_space);
708 char **file_names = file_names_0;
709
710 while (*file_names)
711 {
712 struct file_link *flink = parse_file_name (*file_names++, from_link);
713 if (flink)
714 flink->fl_flags |= FL_PRUNE;
715 }
716 free (file_names_0);
717 }
718
719
720 /****************************************************************************/
721 /* Gather information about the link at `flink'. If it's a good
722 file or directory, return its mod-time and type. */
723
724 static int
725 classify_link (struct file_link *flink, struct stat *stp)
726 {
727 unsigned int flags = 0;
728
729 if (!chdir_to_link (flink->fl_parent))
730 return 0;
731
732 #ifdef S_IFLNK
733 if (lstat (flink->fl_name, stp) < 0)
734 {
735 error (0, errno, _("can't lstat `%s' from `%s'"), flink->fl_name, xgetcwd ());
736 return 0;
737 }
738 if (S_ISLNK (stp->st_mode))
739 {
740 #endif
741 if (stat (flink->fl_name, stp) < 0)
742 {
743 error (0, errno, _("can't stat `%s' from `%s'"), flink->fl_name, xgetcwd ());
744 return 0;
745 }
746 #ifdef S_IFLNK
747 flags |= FL_SYM_LINK;
748 }
749 #endif
750 if (S_ISDIR (stp->st_mode))
751 flags |= FL_TYPE_DIR;
752 else if (stp->st_size == 0)
753 return 0;
754 else if (S_ISREG (stp->st_mode))
755 flags |= FL_TYPE_FILE;
756 else
757 return 0;
758 return flags;
759 }
760
761
762 /****************************************************************************/
763 /* Retrieve an existing flink; or if none exists, create one. */
764
765 static struct file_link *
766 get_link_from_dirent (struct dirent *dirent, struct file_link *parent)
767 {
768 struct file_link **slot;
769 struct file_link *new_link;
770
771 new_link = make_link_from_dirent (dirent, parent);
772 slot = (struct file_link **) hash_find_slot (&idh.idh_file_link_table, new_link);
773 if (HASH_VACANT (*slot))
774 slot = (struct file_link **) hash_insert_at (&idh.idh_file_link_table,
775 new_link, slot);
776 else
777 obstack_free (&idh.idh_file_link_obstack, new_link);
778 return *slot;
779 }
780
781 static struct file_link *
782 get_link_from_string (char const *name, struct file_link *parent)
783 {
784 struct file_link **slot;
785 struct file_link *new_link;
786
787 new_link = make_link_from_string (name, parent);
788 slot = (struct file_link **) hash_find_slot (&idh.idh_file_link_table, new_link);
789 if (HASH_VACANT (*slot))
790 slot = (struct file_link **) hash_insert_at (&idh.idh_file_link_table,
791 new_link, slot);
792 else
793 obstack_free (&idh.idh_file_link_obstack, new_link);
794 return *slot;
795 }
796
797 struct file_link *
798 make_link_from_dirent (struct dirent* dirent, struct file_link *parent)
799 {
800 struct file_link *flink;
801
802 flink = (struct file_link *) obstack_alloc (&idh.idh_file_link_obstack,
803 sizeof (struct file_link) + strlen (dirent->d_name));
804 strcpy (flink->fl_name, dirent->d_name);
805 flink->fl_parent = parent ? parent : flink;
806 flink->fl_flags = 0;
807
808 return flink;
809 }
810
811 static struct file_link *
812 make_link_from_string (char const* name, struct file_link *parent)
813 {
814 struct file_link *flink;
815
816 flink = (struct file_link *) obstack_alloc (&idh.idh_file_link_obstack,
817 sizeof (struct file_link) + strlen (name));
818 strcpy (flink->fl_name, name);
819 flink->fl_parent = parent ? parent : flink;
820 flink->fl_flags = 0;
821
822 return flink;
823 }
824
825
826 /****************************************************************************/
827 /* Convert a `file_link' chain to a vector of component `file_link's,
828 with the root at [0]. Return a pointer beyond the final component. */
829
830 static struct file_link const **
831 fill_link_vector (struct file_link const **vec_buf,
832 struct file_link const *flink)
833 {
834 vec_buf = fill_link_vector_1 (vec_buf, flink);
835 *vec_buf = 0;
836 return vec_buf;
837 }
838
839 static struct file_link const **
840 fill_link_vector_1 (struct file_link const **vec_buf,
841 struct file_link const *flink)
842 {
843 if (!IS_ROOT_FILE_LINK (flink))
844 vec_buf = fill_link_vector_1 (vec_buf, flink->fl_parent);
845 *vec_buf++ = flink;
846 return vec_buf;
847 }
848
849
850 /****************************************************************************/
851 /* Fill BUF_0 with a path to TO_LINK. If a relative path from
852 FROM_LINK is possible (i.e., no intervening symbolic-links) and
853 shorter, return the relative path; otherwise, return an absolute
854 path. */
855
856 char *
857 maybe_relative_file_name (char *buf_0, struct file_link const *to_link, struct file_link const *from_link)
858 {
859 struct file_link const **to_link_vec_0 = alloca (sizeof (struct file_link const *) * (PATH_MAX/2));
860 struct file_link const **from_link_vec_0 = alloca (sizeof (struct file_link const *) * (PATH_MAX/2));
861 struct file_link const **to_link_vec = to_link_vec_0;
862 struct file_link const **from_link_vec = from_link_vec_0;
863 struct file_link const **from_link_end;
864 struct file_link const **from_links;
865 int alloc_me = (buf_0 == 0);
866 char *buf;
867 int levels;
868
869 if (from_link == 0)
870 from_link = current_dir_link;
871
872 if (alloc_me)
873 buf_0 = xmalloc (PATH_MAX);
874
875 /* Optimize common cases. */
876 if (to_link == from_link)
877 strcpy (buf_0, ".");
878 else if (to_link->fl_parent == from_link)
879 strcpy (buf_0, to_link->fl_name);
880 else if (from_link->fl_flags & FL_SYM_LINK)
881 absolute_file_name (buf_0, to_link);
882 else if (to_link == from_link->fl_parent)
883 strcpy (buf_0, "..");
884 else if (to_link->fl_parent == from_link->fl_parent)
885 {
886 strcpy (buf_0, DOT_DOT_SLASH);
887 strcpy (&buf_0[3], to_link->fl_name);
888 }
889 else
890 {
891 from_link_end = fill_link_vector (from_link_vec, from_link);
892 fill_link_vector (to_link_vec, to_link);
893 while (*to_link_vec == *from_link_vec)
894 {
895 if (*to_link_vec == 0)
896 {
897 strcpy (buf_0, ".");
898 goto out;
899 }
900 to_link_vec++;
901 from_link_vec++;
902 }
903 levels = from_link_end - from_link_vec;
904 if (levels >= (from_link_vec - from_link_vec_0))
905 {
906 absolute_file_name (buf_0, to_link);
907 goto out;
908 }
909 for (from_links = from_link_vec; *from_links; from_links++)
910 if ((*from_links)->fl_flags & FL_SYM_LINK)
911 {
912 absolute_file_name (buf_0, to_link);
913 goto out;
914 }
915 buf = fill_dot_dots (buf_0, levels);
916 if (*to_link_vec == 0)
917 *--buf = '\0';
918 else
919 {
920 do
921 {
922 strcpy (buf, (*to_link_vec)->fl_name);
923 buf += strlen (buf);
924 if ((*to_link_vec)->fl_name[0] != SLASH_CHAR && *++to_link_vec)
925 *buf++ = SLASH_CHAR;
926 }
927 while (*to_link_vec);
928 }
929 }
930 out:
931 if (alloc_me)
932 buf_0 = xrealloc (buf_0, (1 + strlen (buf_0)));
933 return buf_0;
934 }
935
936 /* Fill `buf' with sequences of "../" in order to ascend so many
937 `levels' in a directory tree. */
938
939 static char *
940 fill_dot_dots (char *buf, int levels)
941 {
942 while (levels--)
943 {
944 strcpy (buf, DOT_DOT_SLASH);
945 buf += 3;
946 }
947 return buf;
948 }
949
950
951 /****************************************************************************/
952 /* Fill `buffer' with the absolute path to `flink'. */
953
954 char *
955 absolute_file_name (char *buf_0, struct file_link const *flink)
956 {
957 char *end;
958 int alloc_me = (buf_0 == 0);
959
960 if (alloc_me)
961 buf_0 = xmalloc (PATH_MAX);
962 end = absolute_file_name_1 (buf_0, flink);
963 /* Clip the trailing slash. */
964 #if HAVE_LINK
965 if (end > &buf_0[1])
966 end--;
967 #else
968 if (end > &buf_0[3])
969 end--;
970 #endif
971 *end++ = '\0';
972 if (alloc_me)
973 buf_0 = xrealloc (buf_0, (size_t)(end - buf_0));
974 return buf_0;
975 }
976
977 static char *
978 absolute_file_name_1 (char *buf, struct file_link const *flink)
979 {
980 char *end;
981 if (IS_ROOT_FILE_LINK (flink))
982 end = buf;
983 else
984 end = absolute_file_name_1 (buf, flink->fl_parent);
985 strcpy (end, flink->fl_name);
986 if (*end == SLASH_CHAR)
987 end++;
988 else
989 {
990 end += strlen (end);
991 *end++ = SLASH_CHAR;
992 }
993 return end;
994 }
995
996
997 /****************************************************************************/
998 /* Hash stuff for `struct member_file'. */
999
1000 static unsigned long
1001 member_file_hash_1 (void const *key)
1002 {
1003 return_ADDRESS_HASH_1 (((struct member_file const *) key)->mf_link);
1004 }
1005
1006 static unsigned long
1007 member_file_hash_2 (void const *key)
1008 {
1009 return_ADDRESS_HASH_2 (((struct member_file const *) key)->mf_link);
1010 }
1011
1012 static int
1013 member_file_hash_compare (void const *x, void const *y)
1014 {
1015 return_ADDRESS_COMPARE (((struct member_file const *) x)->mf_link,
1016 ((struct member_file const *) y)->mf_link);
1017 }
1018
1019 /* Collating sequence:
1020 - language.map index
1021 - mf_link: breadth-first, alphabetical */
1022
1023 int
1024 member_file_qsort_compare (void const *x, void const *y)
1025 {
1026 struct member_file const *mfx = *(struct member_file const *const *) x;
1027 struct member_file const *mfy = *(struct member_file const *const *) y;
1028 int result;
1029
1030 INTEGER_COMPARE (mfx->mf_lang_args->la_index, mfy->mf_lang_args->la_index, result);
1031 if (result)
1032 return result;
1033 else
1034 {
1035 struct file_link const *flx = mfx->mf_link;
1036 struct file_link const *fly = mfy->mf_link;
1037 if (flx->fl_parent == fly->fl_parent)
1038 return strcmp (flx->fl_name, fly->fl_name);
1039 result = (links_depth (flx) - links_depth (fly));
1040 if (result)
1041 return result;
1042 while (flx->fl_parent != fly->fl_parent)
1043 {
1044 flx = flx->fl_parent;
1045 fly = fly->fl_parent;
1046 }
1047 return strcmp (flx->fl_name, fly->fl_name);
1048 }
1049 }
1050
1051 /****************************************************************************/
1052 /* Hash stuff for `struct file_link'. */
1053
1054 static unsigned long _GL_ATTRIBUTE_PURE
1055 file_link_hash_1 (void const *key)
1056 {
1057 unsigned long result = 0;
1058 struct file_link const *parent = (IS_ROOT_FILE_LINK (((struct file_link const *) key))
1059 ? 0 : ((struct file_link const *) key)->fl_parent);
1060 STRING_HASH_1 (((struct file_link const *) key)->fl_name, result);
1061 ADDRESS_HASH_1 (parent, result);
1062 return result;
1063 }
1064
1065 static unsigned long _GL_ATTRIBUTE_PURE
1066 file_link_hash_2 (void const *key)
1067 {
1068 unsigned long result = 0;
1069 struct file_link const *parent = (IS_ROOT_FILE_LINK (((struct file_link const *) key))
1070 ? 0 : ((struct file_link const *) key)->fl_parent);
1071 STRING_HASH_2 (((struct file_link const *) key)->fl_name, result);
1072 ADDRESS_HASH_2 (parent, result);
1073 return result;
1074 }
1075
1076 static int _GL_ATTRIBUTE_PURE
1077 file_link_hash_compare (void const *x, void const *y)
1078 {
1079 int result;
1080 struct file_link const *x_parent = (IS_ROOT_FILE_LINK (((struct file_link const *) x))
1081 ? 0 : ((struct file_link const *) x)->fl_parent);
1082 struct file_link const *y_parent = (IS_ROOT_FILE_LINK (((struct file_link const *) y))
1083 ? 0 : ((struct file_link const *) y)->fl_parent);
1084 ADDRESS_COMPARE (x_parent, y_parent, result);
1085 if (result)
1086 return result;
1087 STRING_COMPARE (((struct file_link const *) x)->fl_name,
1088 ((struct file_link const *) y)->fl_name, result);
1089 return result;
1090 }
1091
1092 /* Count directory components between flink and its root. */
1093
1094 int
1095 links_depth (struct file_link const *flink)
1096 {
1097 int depth = 0;
1098 while (!IS_ROOT_FILE_LINK (flink))
1099 {
1100 depth++;
1101 flink = flink->fl_parent;
1102 }
1103 return depth;
1104 }
1105
1106 #if HAVE_LINK
1107
1108 /****************************************************************************/
1109 /* Hash stuff for `struct dev_ino'. */
1110
1111 /* FIXME: consider dumping all of this in favor of gnulib's di-set module. */
1112 /* Expand to the definition of a function that hashes a dev+inode pair,
1113 with application of XFORM. */
1114 #define DEV_INO_HASH_DEFUN(FN_NAME, XFORM) \
•1115 static unsigned long _GL_ATTRIBUTE_PURE \
1116 FN_NAME (void const *x) \
1117 { \
1118 struct dev_ino const *p = x; \
1119 ino_t ino = p->di_ino; \
1120 \
•1121 /* When INO is wider than unsigned long, XOR the words of INO into H. \
1122 This avoids loss of info, without applying % to the wider type, \
1123 which could be quite slow on some systems. */ \
•1124 unsigned long h = XFORM (ino); \
1125 unsigned int n_words = sizeof ino / sizeof h + (sizeof ino % sizeof h != 0); \
1126 for (unsigned int i = 1; i < n_words; i++) \
1127 h ^= XFORM (ino >> CHAR_BIT * sizeof h * i); \
1128 \
1129 dev_t dev = p->di_dev; \
1130 h ^= XFORM (dev); \
1131 n_words = sizeof dev / sizeof h + (sizeof dev % sizeof h != 0); \
1132 for (unsigned int i = 1; i < n_words; i++) \
1133 h ^= XFORM (dev >> CHAR_BIT * sizeof h * i); \
1134 \
1135 return h; \
1136 }
•1137 static inline unsigned long xform_NOP (unsigned long k) { return k; }
1138 static inline unsigned long xform_NOT (unsigned long k) { return ~k; }
1139 DEV_INO_HASH_DEFUN(dev_ino_hash_1, xform_NOP)
1140 DEV_INO_HASH_DEFUN(dev_ino_hash_2, xform_NOT)
1141
•1142 /* Note, this is a compare, not a boolian test for equality.
1143 It returns negative, positive, or 0, with 0 indicating equality. */
1144 static int
1145 dev_ino_hash_compare (void const *xv, void const *yv)
1146 {
1147 struct dev_ino const *x = xv;
1148 struct dev_ino const *y = yv;
•1149 int result;
1150 INTEGER_COMPARE ( x->di_ino,
1151 y->di_ino, result);
1152 if (result)
1153 return result;
1154 INTEGER_COMPARE (x->di_dev,
1155 y->di_dev, result);
1156 return result;
1157 }
1158
1159 #endif
1160
1161 /*******************************************************************/
1162
1163 struct file_link *
1164 init_walker (struct idhead *idhp)
1165 {
1166 init_idh_obstacks (idhp);
1167 init_idh_tables (idhp);
1168 return get_current_dir_link ();
1169 }
1170
1171 void
1172 init_idh_obstacks (struct idhead *idhp)
1173 {
1174 obstack_init (&idhp->idh_member_file_obstack);
1175 obstack_init (&idhp->idh_file_link_obstack);
1176 #if HAVE_LINK
1177 obstack_init (&idhp->idh_dev_ino_obstack);
1178 #endif
1179 }
1180
1181 void
1182 init_idh_tables (struct idhead *idhp)
1183 {
1184 hash_init (&idhp->idh_member_file_table, 16*1024,
1185 member_file_hash_1, member_file_hash_2, member_file_hash_compare);
1186 hash_init (&idhp->idh_file_link_table, 16*1024,
1187 file_link_hash_1, file_link_hash_2, file_link_hash_compare);
1188 #if HAVE_LINK
1189 hash_init (&idhp->idh_dev_ino_table, 16*1024,
1190 dev_ino_hash_1, dev_ino_hash_2, dev_ino_hash_compare);
1191 #endif
1192 }