]> Dogcows Code - chaz/tar/blob - src/names.c
(namelist_match, excluded_name): Do not match subfiles of a directory
[chaz/tar] / src / names.c
1 /* Various processing of names.
2 Copyright 1988, 92, 94, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 #include "system.h"
19
20 #include <fnmatch.h>
21 #include <grp.h>
22 #include <hash.h>
23 #include <pwd.h>
24 #include <quotearg.h>
25
26 #include "common.h"
27 \f
28 /* User and group names. */
29
30 struct group *getgrnam ();
31 struct passwd *getpwnam ();
32 #if ! HAVE_DECL_GETPWUID
33 struct passwd *getpwuid ();
34 #endif
35 #if ! HAVE_DECL_GETGRGID
36 struct group *getgrgid ();
37 #endif
38
39 /* Make sure you link with the proper libraries if you are running the
40 Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
41 This code should also be modified for non-UNIX systems to do something
42 reasonable. */
43
44 static char cached_uname[UNAME_FIELD_SIZE];
45 static char cached_gname[GNAME_FIELD_SIZE];
46
47 static uid_t cached_uid; /* valid only if cached_uname is not empty */
48 static gid_t cached_gid; /* valid only if cached_gname is not empty */
49
50 /* These variables are valid only if nonempty. */
51 static char cached_no_such_uname[UNAME_FIELD_SIZE];
52 static char cached_no_such_gname[GNAME_FIELD_SIZE];
53
54 /* These variables are valid only if nonzero. It's not worth optimizing
55 the case for weird systems where 0 is not a valid uid or gid. */
56 static uid_t cached_no_such_uid;
57 static gid_t cached_no_such_gid;
58
59 /* Given UID, find the corresponding UNAME. */
60 void
61 uid_to_uname (uid_t uid, char uname[UNAME_FIELD_SIZE])
62 {
63 struct passwd *passwd;
64
65 if (uid != 0 && uid == cached_no_such_uid)
66 {
67 *uname = '\0';
68 return;
69 }
70
71 if (!cached_uname[0] || uid != cached_uid)
72 {
73 passwd = getpwuid (uid);
74 if (passwd)
75 {
76 cached_uid = uid;
77 strncpy (cached_uname, passwd->pw_name, UNAME_FIELD_SIZE);
78 }
79 else
80 {
81 cached_no_such_uid = uid;
82 *uname = '\0';
83 return;
84 }
85 }
86 strncpy (uname, cached_uname, UNAME_FIELD_SIZE);
87 }
88
89 /* Given GID, find the corresponding GNAME. */
90 void
91 gid_to_gname (gid_t gid, char gname[GNAME_FIELD_SIZE])
92 {
93 struct group *group;
94
95 if (gid != 0 && gid == cached_no_such_gid)
96 {
97 *gname = '\0';
98 return;
99 }
100
101 if (!cached_gname[0] || gid != cached_gid)
102 {
103 group = getgrgid (gid);
104 if (group)
105 {
106 cached_gid = gid;
107 strncpy (cached_gname, group->gr_name, GNAME_FIELD_SIZE);
108 }
109 else
110 {
111 cached_no_such_gid = gid;
112 *gname = '\0';
113 return;
114 }
115 }
116 strncpy (gname, cached_gname, GNAME_FIELD_SIZE);
117 }
118
119 /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
120 int
121 uname_to_uid (char uname[UNAME_FIELD_SIZE], uid_t *uidp)
122 {
123 struct passwd *passwd;
124
125 if (cached_no_such_uname[0]
126 && strncmp (uname, cached_no_such_uname, UNAME_FIELD_SIZE) == 0)
127 return 0;
128
129 if (!cached_uname[0]
130 || uname[0] != cached_uname[0]
131 || strncmp (uname, cached_uname, UNAME_FIELD_SIZE) != 0)
132 {
133 passwd = getpwnam (uname);
134 if (passwd)
135 {
136 cached_uid = passwd->pw_uid;
137 strncpy (cached_uname, uname, UNAME_FIELD_SIZE);
138 }
139 else
140 {
141 strncpy (cached_no_such_uname, uname, UNAME_FIELD_SIZE);
142 return 0;
143 }
144 }
145 *uidp = cached_uid;
146 return 1;
147 }
148
149 /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
150 int
151 gname_to_gid (char gname[GNAME_FIELD_SIZE], gid_t *gidp)
152 {
153 struct group *group;
154
155 if (cached_no_such_gname[0]
156 && strncmp (gname, cached_no_such_gname, GNAME_FIELD_SIZE) == 0)
157 return 0;
158
159 if (!cached_gname[0]
160 || gname[0] != cached_gname[0]
161 || strncmp (gname, cached_gname, GNAME_FIELD_SIZE) != 0)
162 {
163 group = getgrnam (gname);
164 if (group)
165 {
166 cached_gid = group->gr_gid;
167 strncpy (cached_gname, gname, GNAME_FIELD_SIZE);
168 }
169 else
170 {
171 strncpy (cached_no_such_gname, gname, GNAME_FIELD_SIZE);
172 return 0;
173 }
174 }
175 *gidp = cached_gid;
176 return 1;
177 }
178 \f
179 /* Names from the command call. */
180
181 static struct name *namelist; /* first name in list, if any */
182 static struct name **nametail = &namelist; /* end of name list */
183 static const char **name_array; /* store an array of names */
184 static int allocated_names; /* how big is the array? */
185 static int names; /* how many entries does it have? */
186 static int name_index; /* how many of the entries have we scanned? */
187
188 /* Initialize structures. */
189 void
190 init_names (void)
191 {
192 allocated_names = 10;
193 name_array = xmalloc (sizeof (const char *) * allocated_names);
194 names = 0;
195 }
196
197 /* Add NAME at end of name_array, reallocating it as necessary. */
198 void
199 name_add (const char *name)
200 {
201 if (names == allocated_names)
202 {
203 allocated_names *= 2;
204 name_array =
205 xrealloc (name_array, sizeof (const char *) * allocated_names);
206 }
207 name_array[names++] = name;
208 }
209 \f
210 /* Names from external name file. */
211
212 static FILE *name_file; /* file to read names from */
213 static char *name_buffer; /* buffer to hold the current file name */
214 static size_t name_buffer_length; /* allocated length of name_buffer */
215
216 /* FIXME: I should better check more closely. It seems at first glance that
217 is_pattern is only used when reading a file, and ignored for all
218 command line arguments. */
219
220 static inline int
221 is_pattern (const char *string)
222 {
223 return strchr (string, '*') || strchr (string, '[') || strchr (string, '?');
224 }
225
226 /* Set up to gather file names for tar. They can either come from a
227 file or were saved from decoding arguments. */
228 void
229 name_init (int argc, char *const *argv)
230 {
231 name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
232 name_buffer_length = NAME_FIELD_SIZE;
233
234 if (files_from_option)
235 {
236 if (!strcmp (files_from_option, "-"))
237 {
238 request_stdin ("-T");
239 name_file = stdin;
240 }
241 else if (name_file = fopen (files_from_option, "r"), !name_file)
242 open_fatal (files_from_option);
243 }
244 }
245
246 void
247 name_term (void)
248 {
249 free (name_buffer);
250 free (name_array);
251 }
252
253 /* Read the next filename from name_file and null-terminate it. Put
254 it into name_buffer, reallocating and adjusting name_buffer_length
255 if necessary. Return 0 at end of file, 1 otherwise. */
256 static int
257 read_name_from_file (void)
258 {
259 int character;
260 size_t counter = 0;
261
262 /* FIXME: getc may be called even if character was EOF the last time here. */
263
264 /* FIXME: This + 2 allocation might serve no purpose. */
265
266 while (character = getc (name_file),
267 character != EOF && character != filename_terminator)
268 {
269 if (counter == name_buffer_length)
270 {
271 name_buffer_length += NAME_FIELD_SIZE;
272 name_buffer = xrealloc (name_buffer, name_buffer_length + 2);
273 }
274 name_buffer[counter++] = character;
275 }
276
277 if (counter == 0 && character == EOF)
278 return 0;
279
280 if (counter == name_buffer_length)
281 {
282 name_buffer_length += NAME_FIELD_SIZE;
283 name_buffer = xrealloc (name_buffer, name_buffer_length + 2);
284 }
285 name_buffer[counter] = '\0';
286
287 return 1;
288 }
289
290 /* Get the next name from ARGV or the file of names. Result is in
291 static storage and can't be relied upon across two calls.
292
293 If CHANGE_DIRS is true, treat a filename of the form "-C" as
294 meaning that the next filename is the name of a directory to change
295 to. If filename_terminator is NUL, CHANGE_DIRS is effectively
296 always false. */
297 char *
298 name_next (int change_dirs)
299 {
300 const char *source;
301 char *cursor;
302 int chdir_flag = 0;
303
304 if (filename_terminator == '\0')
305 change_dirs = 0;
306
307 while (1)
308 {
309 /* Get a name, either from file or from saved arguments. */
310
311 if (name_index == names)
312 {
313 if (! name_file)
314 break;
315 if (! read_name_from_file ())
316 break;
317 }
318 else
319 {
320 source = name_array[name_index++];
321 if (strlen (source) > name_buffer_length)
322 {
323 free (name_buffer);
324 name_buffer_length = strlen (source);
325 name_buffer = xmalloc (name_buffer_length + 2);
326 }
327 strcpy (name_buffer, source);
328 }
329
330 /* Zap trailing slashes. */
331
332 cursor = name_buffer + strlen (name_buffer) - 1;
333 while (cursor > name_buffer && *cursor == '/')
334 *cursor-- = '\0';
335
336 if (chdir_flag)
337 {
338 if (chdir (name_buffer) < 0)
339 chdir_fatal (name_buffer);
340 chdir_flag = 0;
341 }
342 else if (change_dirs && strcmp (name_buffer, "-C") == 0)
343 chdir_flag = 1;
344 else
345 {
346 unquote_string (name_buffer);
347 return name_buffer;
348 }
349 }
350
351 /* No more names in file. */
352
353 if (name_file && chdir_flag)
354 FATAL_ERROR ((0, 0, _("Missing file name after -C")));
355
356 return 0;
357 }
358
359 /* Close the name file, if any. */
360 void
361 name_close (void)
362 {
363 if (name_file && name_file != stdin)
364 if (fclose (name_file) != 0)
365 close_error (name_buffer);
366 }
367
368 /* Gather names in a list for scanning. Could hash them later if we
369 really care.
370
371 If the names are already sorted to match the archive, we just read
372 them one by one. name_gather reads the first one, and it is called
373 by name_match as appropriate to read the next ones. At EOF, the
374 last name read is just left in the buffer. This option lets users
375 of small machines extract an arbitrary number of files by doing
376 "tar t" and editing down the list of files. */
377
378 void
379 name_gather (void)
380 {
381 /* Buffer able to hold a single name. */
382 static struct name *buffer;
383 static size_t allocated_length;
384
385 char const *name;
386
387 if (same_order_option)
388 {
389 static int change_dir;
390
391 if (allocated_length == 0)
392 {
393 allocated_length = sizeof (struct name) + NAME_FIELD_SIZE;
394 buffer = xmalloc (allocated_length);
395 /* FIXME: This memset is overkill, and ugly... */
396 memset (buffer, 0, allocated_length);
397 }
398
399 while ((name = name_next (0)) && strcmp (name, "-C") == 0)
400 {
401 char const *dir = name_next (0);
402 if (! dir)
403 FATAL_ERROR ((0, 0, _("Missing file name after -C")));
404 change_dir = chdir_arg (xstrdup (dir));
405 }
406
407 if (name)
408 {
409 buffer->length = strlen (name);
410 if (sizeof (struct name) + buffer->length >= allocated_length)
411 {
412 allocated_length = sizeof (struct name) + buffer->length;
413 buffer = xrealloc (buffer, allocated_length);
414 }
415 buffer->change_dir = change_dir;
416 memcpy (buffer->name, name, buffer->length + 1);
417 buffer->next = 0;
418 buffer->found = 0;
419
420 namelist = buffer;
421 nametail = &namelist->next;
422 }
423 }
424 else
425 {
426 /* Non sorted names -- read them all in. */
427 int change_dir = 0;
428
429 for (;;)
430 {
431 int change_dir0 = change_dir;
432 while ((name = name_next (0)) && strcmp (name, "-C") == 0)
433 {
434 char const *dir = name_next (0);
435 if (! dir)
436 FATAL_ERROR ((0, 0, _("Missing file name after -C")));
437 change_dir = chdir_arg (xstrdup (dir));
438 }
439 if (name)
440 addname (name, change_dir);
441 else
442 {
443 if (change_dir != change_dir0)
444 addname (0, change_dir);
445 break;
446 }
447 }
448 }
449 }
450
451 /* Add a name to the namelist. */
452 struct name *
453 addname (char const *string, int change_dir)
454 {
455 struct name *name;
456 size_t length;
457
458 length = string ? strlen (string) : 0;
459 name = xmalloc (sizeof (struct name) + length);
460 memset (name, 0, sizeof (struct name) + length);
461 name->next = 0;
462
463 if (string)
464 {
465 name->fake = 0;
466 name->length = length;
467 memcpy (name->name, string, length + 1);
468 }
469 else
470 name->fake = 1;
471
472 name->found = 0;
473 name->regexp = 0; /* assume not a regular expression */
474 name->firstch = 1; /* assume first char is literal */
475 name->change_dir = change_dir;
476 name->dir_contents = 0;
477
478 if (string && is_pattern (string))
479 {
480 name->regexp = 1;
481 if (string[0] == '*' || string[0] == '[' || string[0] == '?')
482 name->firstch = 0;
483 }
484
485 *nametail = name;
486 nametail = &name->next;
487 return name;
488 }
489
490 /* Find a match for PATH (whose string length is LENGTH) in the name
491 list. */
492 static struct name *
493 namelist_match (char const *path, size_t length)
494 {
495 struct name *p;
496
497 for (p = namelist; p; p = p->next)
498 {
499 /* If first chars don't match, quick skip. */
500
501 if (p->firstch && p->name[0] != path[0])
502 continue;
503
504 if (p->regexp
505 ? fnmatch (p->name, path, recursion_option) == 0
506 : (p->length <= length
507 && (path[p->length] == '\0' || path[p->length] == '/')
508 && memcmp (path, p->name, p->length) == 0))
509 return p;
510 }
511
512 return 0;
513 }
514
515 /* Return true if and only if name PATH (from an archive) matches any
516 name from the namelist. */
517 int
518 name_match (const char *path)
519 {
520 size_t length = strlen (path);
521
522 while (1)
523 {
524 struct name *cursor = namelist;
525
526 if (!cursor)
527 return ! files_from_option;
528
529 if (cursor->fake)
530 {
531 chdir_do (cursor->change_dir);
532 namelist = 0;
533 nametail = &namelist;
534 return ! files_from_option;
535 }
536
537 cursor = namelist_match (path, length);
538 if (cursor)
539 {
540 cursor->found = 1; /* remember it matched */
541 if (starting_file_option)
542 {
543 free (namelist);
544 namelist = 0;
545 nametail = &namelist;
546 }
547 chdir_do (cursor->change_dir);
548
549 /* We got a match. */
550 return 1;
551 }
552
553 /* Filename from archive not found in namelist. If we have the whole
554 namelist here, just return 0. Otherwise, read the next name in and
555 compare it. If this was the last name, namelist->found will remain
556 on. If not, we loop to compare the newly read name. */
557
558 if (same_order_option && namelist->found)
559 {
560 name_gather (); /* read one more */
561 if (namelist->found)
562 return 0;
563 }
564 else
565 return 0;
566 }
567 }
568
569 /* Print the names of things in the namelist that were not matched. */
570 void
571 names_notfound (void)
572 {
573 struct name const *cursor;
574
575 for (cursor = namelist; cursor; cursor = cursor->next)
576 if (!cursor->found && !cursor->fake)
577 ERROR ((0, 0, _("%s: Not found in archive"),
578 quotearg_colon (cursor->name)));
579
580 /* Don't bother freeing the name list; we're about to exit. */
581 namelist = 0;
582 nametail = &namelist;
583
584 if (same_order_option)
585 {
586 char *name;
587
588 while (name = name_next (1), name)
589 ERROR ((0, 0, _("%s: Not found in archive"),
590 quotearg_colon (name)));
591 }
592 }
593 \f
594 /* Sorting name lists. */
595
596 /* Sort linked LIST of names, of given LENGTH, using COMPARE to order
597 names. Return the sorted list. Apart from the type `struct name'
598 and the definition of SUCCESSOR, this is a generic list-sorting
599 function, but it's too painful to make it both generic and portable
600 in C. */
601
602 static struct name *
603 merge_sort (struct name *list, int length,
604 int (*compare) (struct name const*, struct name const*))
605 {
606 struct name *first_list;
607 struct name *second_list;
608 int first_length;
609 int second_length;
610 struct name *result;
611 struct name **merge_point;
612 struct name *cursor;
613 int counter;
614
615 # define SUCCESSOR(name) ((name)->next)
616
617 if (length == 1)
618 return list;
619
620 if (length == 2)
621 {
622 if ((*compare) (list, SUCCESSOR (list)) > 0)
623 {
624 result = SUCCESSOR (list);
625 SUCCESSOR (result) = list;
626 SUCCESSOR (list) = 0;
627 return result;
628 }
629 return list;
630 }
631
632 first_list = list;
633 first_length = (length + 1) / 2;
634 second_length = length / 2;
635 for (cursor = list, counter = first_length - 1;
636 counter;
637 cursor = SUCCESSOR (cursor), counter--)
638 continue;
639 second_list = SUCCESSOR (cursor);
640 SUCCESSOR (cursor) = 0;
641
642 first_list = merge_sort (first_list, first_length, compare);
643 second_list = merge_sort (second_list, second_length, compare);
644
645 merge_point = &result;
646 while (first_list && second_list)
647 if ((*compare) (first_list, second_list) < 0)
648 {
649 cursor = SUCCESSOR (first_list);
650 *merge_point = first_list;
651 merge_point = &SUCCESSOR (first_list);
652 first_list = cursor;
653 }
654 else
655 {
656 cursor = SUCCESSOR (second_list);
657 *merge_point = second_list;
658 merge_point = &SUCCESSOR (second_list);
659 second_list = cursor;
660 }
661 if (first_list)
662 *merge_point = first_list;
663 else
664 *merge_point = second_list;
665
666 return result;
667
668 #undef SUCCESSOR
669 }
670
671 /* A comparison function for sorting names. Put found names last;
672 break ties by string comparison. */
673
674 static int
675 compare_names (struct name const *n1, struct name const *n2)
676 {
677 int found_diff = n2->found - n1->found;
678 return found_diff ? found_diff : strcmp (n1->name, n2->name);
679 }
680 \f
681 /* Add all the dirs under NAME, which names a directory, to the namelist.
682 DIRSIZE is the size of the directory, or -1 if not known.
683 If any of the files is a directory, recurse on the subdirectory.
684 DEVICE is the device not to leave, if the -l option is specified. */
685
686 static void
687 add_hierarchy_to_namelist (struct name *name, off_t dirsize, dev_t device)
688 {
689 char *path = name->name;
690 char *buffer = get_directory_contents (path, dirsize, device);
691
692 if (! buffer)
693 name->dir_contents = "\0\0\0\0";
694 else
695 {
696 size_t name_length = name->length;
697 size_t allocated_length = (name_length >= NAME_FIELD_SIZE
698 ? name_length + NAME_FIELD_SIZE
699 : NAME_FIELD_SIZE);
700 char *name_buffer = xmalloc (allocated_length + 1);
701 /* FIXME: + 2 above? */
702 char *string;
703 size_t string_length;
704 int change_dir = name->change_dir;
705
706 name->dir_contents = buffer;
707 strcpy (name_buffer, path);
708 if (name_buffer[name_length - 1] != '/')
709 {
710 name_buffer[name_length++] = '/';
711 name_buffer[name_length] = '\0';
712 }
713
714 for (string = buffer; *string; string += string_length + 1)
715 {
716 string_length = strlen (string);
717 if (*string == 'D')
718 {
719 if (name_length + string_length >= allocated_length)
720 {
721 while (name_length + string_length >= allocated_length)
722 allocated_length += NAME_FIELD_SIZE;
723 name_buffer = xrealloc (name_buffer, allocated_length + 1);
724 }
725 strcpy (name_buffer + name_length, string + 1);
726 add_hierarchy_to_namelist (addname (name_buffer, change_dir),
727 -1, device);
728 }
729 }
730
731 free (name_buffer);
732 }
733 }
734 \f
735 /* Collect all the names from argv[] (or whatever), expand them into a
736 directory tree, and sort them. This gets only subdirectories, not
737 all files. */
738
739 void
740 collect_and_sort_names (void)
741 {
742 struct name *name;
743 struct name *next_name;
744 int num_names;
745 struct stat statbuf;
746
747 name_gather ();
748
749 if (listed_incremental_option)
750 read_directory_file ();
751
752 if (!namelist)
753 addname (".", 0);
754
755 for (name = namelist; name; name = next_name)
756 {
757 next_name = name->next;
758 if (name->found || name->dir_contents)
759 continue;
760 if (name->regexp) /* FIXME: just skip regexps for now */
761 continue;
762 chdir_do (name->change_dir);
763 if (name->fake)
764 continue;
765
766 if (deref_stat (dereference_option, name->name, &statbuf) != 0)
767 {
768 stat_error (name->name);
769 continue;
770 }
771 if (S_ISDIR (statbuf.st_mode))
772 {
773 name->found = 1;
774 add_hierarchy_to_namelist (name, statbuf.st_size, statbuf.st_dev);
775 }
776 }
777
778 num_names = 0;
779 for (name = namelist; name; name = name->next)
780 num_names++;
781 namelist = merge_sort (namelist, num_names, compare_names);
782
783 for (name = namelist; name; name = name->next)
784 name->found = 0;
785 }
786
787 /* This is like name_match, except that it returns a pointer to the
788 name it matched, and doesn't set FOUND in structure. The caller
789 will have to do that if it wants to. Oh, and if the namelist is
790 empty, it returns null, unlike name_match, which returns TRUE. */
791 struct name *
792 name_scan (const char *path)
793 {
794 size_t length = strlen (path);
795
796 while (1)
797 {
798 struct name *cursor = namelist_match (path, length);
799 if (cursor)
800 return cursor;
801
802 /* Filename from archive not found in namelist. If we have the whole
803 namelist here, just return 0. Otherwise, read the next name in and
804 compare it. If this was the last name, namelist->found will remain
805 on. If not, we loop to compare the newly read name. */
806
807 if (same_order_option && namelist && namelist->found)
808 {
809 name_gather (); /* read one more */
810 if (namelist->found)
811 return 0;
812 }
813 else
814 return 0;
815 }
816 }
817
818 /* This returns a name from the namelist which doesn't have ->found
819 set. It sets ->found before returning, so successive calls will
820 find and return all the non-found names in the namelist. */
821 struct name *gnu_list_name;
822
823 char *
824 name_from_list (void)
825 {
826 if (!gnu_list_name)
827 gnu_list_name = namelist;
828 while (gnu_list_name && (gnu_list_name->found | gnu_list_name->fake))
829 gnu_list_name = gnu_list_name->next;
830 if (gnu_list_name)
831 {
832 gnu_list_name->found = 1;
833 chdir_do (gnu_list_name->change_dir);
834 return gnu_list_name->name;
835 }
836 return 0;
837 }
838
839 void
840 blank_name_list (void)
841 {
842 struct name *name;
843
844 gnu_list_name = 0;
845 for (name = namelist; name; name = name->next)
846 name->found = 0;
847 }
848
849 /* Yield a newly allocated file name consisting of PATH concatenated to
850 NAME, with an intervening slash if PATH does not already end in one. */
851 char *
852 new_name (const char *path, const char *name)
853 {
854 size_t pathlen = strlen (path);
855 size_t namesize = strlen (name) + 1;
856 int slash = pathlen && path[pathlen - 1] != '/';
857 char *buffer = xmalloc (pathlen + slash + namesize);
858 memcpy (buffer, path, pathlen);
859 buffer[pathlen] = '/';
860 memcpy (buffer + pathlen + slash, name, namesize);
861 return buffer;
862 }
863
864 /* Return nonzero if file NAME is excluded. Exclude a name if its
865 prefix matches a pattern that contains slashes, or if one of its
866 components matches a pattern that contains no slashes. */
867 int
868 excluded_name (char const *name)
869 {
870 char const *p;
871 name += FILESYSTEM_PREFIX_LEN (name);
872
873 if (excluded_filename (excluded_with_slash, name,
874 FNM_FILE_NAME | recursion_option))
875 return 1;
876
877 for (p = name; *p; p++)
878 if (((p == name || ISSLASH (p[-1])) && !ISSLASH (p[0]))
879 && excluded_filename (excluded_without_slash, p,
880 FNM_FILE_NAME | FNM_LEADING_DIR))
881 return 1;
882
883 return 0;
884 }
885 \f
886 /* Names to avoid dumping. */
887 static Hash_table *avoided_name_table;
888
889 /* Calculate the hash of an avoided name. */
890 static unsigned
891 hash_avoided_name (void const *name, unsigned n_buckets)
892 {
893 return hash_string (name, n_buckets);
894 }
895
896 /* Compare two avoided names for equality. */
897 static bool
898 compare_avoided_names (void const *name1, void const *name2)
899 {
900 return strcmp (name1, name2) == 0;
901 }
902
903 /* Remember to not archive NAME. */
904 void
905 add_avoided_name (char const *name)
906 {
907 if (! ((avoided_name_table
908 || (avoided_name_table = hash_initialize (0, 0, hash_avoided_name,
909 compare_avoided_names, 0)))
910 && hash_insert (avoided_name_table, xstrdup (name))))
911 xalloc_die ();
912 }
913
914 /* Should NAME be avoided when archiving? */
915 int
916 is_avoided_name (char const *name)
917 {
918 return avoided_name_table && hash_lookup (avoided_name_table, name);
919 }
This page took 0.081661 seconds and 5 git commands to generate.