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