]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/cpio/lib/paxnames.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / contrib / cpio / lib / paxnames.c
1 /* This file is part of GNU paxutils
2    Copyright (C) 2005 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    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <system.h>
19 #include <hash.h>
20 #include <paxlib.h>
21
22 \f
23 /* Hash tables of strings.  */
24
25 /* Calculate the hash of a string.  */
26 static size_t
27 hash_string_hasher (void const *name, size_t n_buckets)
28 {
29   return hash_string (name, n_buckets);
30 }
31
32 /* Compare two strings for equality.  */
33 static bool
34 hash_string_compare (void const *name1, void const *name2)
35 {
36   return strcmp (name1, name2) == 0;
37 }
38
39 /* Return zero if TABLE contains a copy of STRING; otherwise, insert a
40    copy of STRING to TABLE and return 1.  */
41 bool
42 hash_string_insert (Hash_table **table, char const *string)
43 {
44   Hash_table *t = *table;
45   char *s = xstrdup (string);
46   char *e;
47
48   if (! ((t
49           || (*table = t = hash_initialize (0, 0, hash_string_hasher,
50                                             hash_string_compare, 0)))
51          && (e = hash_insert (t, s))))
52     xalloc_die ();
53
54   if (e == s)
55     return 1;
56   else
57     {
58       free (s);
59       return 0;
60     }
61 }
62
63 /* Return 1 if TABLE contains STRING.  */
64 bool
65 hash_string_lookup (Hash_table const *table, char const *string)
66 {
67   return table && hash_lookup (table, string);
68 }
69
70 \f
71 static Hash_table *prefix_table[2];
72
73 /* Return true if file names of some members in the archive were stripped off
74    their leading components. We could have used
75         return prefix_table[0] || prefix_table[1]
76    but the following seems to be safer: */
77 bool
78 removed_prefixes_p (void)
79 {
80   return (prefix_table[0] && hash_get_n_entries (prefix_table[0]) != 0)
81          || (prefix_table[1] && hash_get_n_entries (prefix_table[1]) != 0);
82 }
83
84 /* Return a safer suffix of FILE_NAME, or "." if it has no safer
85    suffix.  Check for fully specified file names and other atrocities.
86    Warn the user if we do not return NAME.  If LINK_TARGET is 1,
87    FILE_NAME is the target of a hard link, not a member name.
88    If ABSOLUTE_NAMES is 0, strip filesystem prefix from the file name. */
89
90 char *
91 safer_name_suffix (char const *file_name, bool link_target, bool absolute_names)
92 {
93   char const *p;
94
95   if (absolute_names)
96     p = file_name;
97   else
98     {
99       /* Skip file system prefixes, leading file name components that contain
100          "..", and leading slashes.  */
101
102       size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (file_name);
103
104       for (p = file_name + prefix_len; *p; )
105         {
106           if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
107             prefix_len = p + 2 - file_name;
108
109           do
110             {
111               char c = *p++;
112               if (ISSLASH (c))
113                 break;
114             }
115           while (*p);
116         }
117
118       for (p = file_name + prefix_len; ISSLASH (*p); p++)
119         continue;
120       prefix_len = p - file_name;
121
122       if (prefix_len)
123         {
124           char *prefix = alloca (prefix_len + 1);
125           memcpy (prefix, file_name, prefix_len);
126           prefix[prefix_len] = '\0';
127
128           if (hash_string_insert (&prefix_table[link_target], prefix))
129             {
130               static char const *const diagnostic[] =
131               {
132                 N_("Removing leading `%s' from member names"),
133                 N_("Removing leading `%s' from hard link targets")
134               };
135               WARN ((0, 0, _(diagnostic[link_target]), prefix));
136             }
137         }
138     }
139
140   if (! *p)
141     {
142       if (p == file_name)
143         {
144           static char const *const diagnostic[] =
145           {
146             N_("Substituting `.' for empty member name"),
147             N_("Substituting `.' for empty hard link target")
148           };
149           WARN ((0, 0, "%s", _(diagnostic[link_target])));
150         }
151
152       p = ".";
153     }
154
155   return (char *) p;
156 }