1 /* This file is part of GNU paxutils
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
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 3, or (at your option) any later
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.
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. */
23 /* Hash tables of strings. */
25 /* Calculate the hash of a string. */
27 hash_string_hasher (void const *name, size_t n_buckets)
29 return hash_string (name, n_buckets);
32 /* Compare two strings for equality. */
34 hash_string_compare (void const *name1, void const *name2)
36 return strcmp (name1, name2) == 0;
39 /* Return zero if TABLE contains a LEN-character long prefix of STRING,
40 otherwise, insert a newly allocated copy of this prefix to TABLE and
41 return 1. If RETURN_PREFIX is not NULL, point it to the allocated
44 hash_string_insert_prefix (Hash_table **table, char const *string, size_t len,
45 const char **return_prefix)
47 Hash_table *t = *table;
53 s = xmalloc (len + 1);
54 memcpy (s, string, len);
61 || (*table = t = hash_initialize (0, 0, hash_string_hasher,
62 hash_string_compare, 0)))
63 && (e = hash_insert (t, s))))
79 /* Return zero if TABLE contains a copy of STRING; otherwise, insert a
80 copy of STRING to TABLE and return 1. */
82 hash_string_insert (Hash_table **table, char const *string)
84 return hash_string_insert_prefix (table, string, 0, NULL);
87 /* Return 1 if TABLE contains STRING. */
89 hash_string_lookup (Hash_table const *table, char const *string)
91 return table && hash_lookup (table, string);
95 static Hash_table *prefix_table[2];
97 /* Return true if file names of some members in the archive were stripped off
98 their leading components. We could have used
99 return prefix_table[0] || prefix_table[1]
100 but the following seems to be safer: */
102 removed_prefixes_p (void)
104 return (prefix_table[0] && hash_get_n_entries (prefix_table[0]) != 0)
105 || (prefix_table[1] && hash_get_n_entries (prefix_table[1]) != 0);
108 /* Return a safer suffix of FILE_NAME, or "." if it has no safer
109 suffix. Check for fully specified file names and other atrocities.
110 Warn the user if we do not return NAME. If LINK_TARGET is 1,
111 FILE_NAME is the target of a hard link, not a member name.
112 If ABSOLUTE_NAMES is 0, strip filesystem prefix from the file name. */
115 safer_name_suffix (char const *file_name, bool link_target,
124 /* Skip file system prefixes, leading file name components that contain
125 "..", and leading slashes. */
127 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (file_name);
129 for (p = file_name + prefix_len; *p; )
131 if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
132 prefix_len = p + 2 - file_name;
143 for (p = file_name + prefix_len; ISSLASH (*p); p++)
145 prefix_len = p - file_name;
150 if (hash_string_insert_prefix (&prefix_table[link_target], file_name,
151 prefix_len, &prefix))
153 static char const *const diagnostic[] =
155 N_("Removing leading `%s' from member names"),
156 N_("Removing leading `%s' from hard link targets")
158 WARN ((0, 0, _(diagnostic[link_target]), prefix));
167 static char const *const diagnostic[] =
169 N_("Substituting `.' for empty member name"),
170 N_("Substituting `.' for empty hard link target")
172 WARN ((0, 0, "%s", _(diagnostic[link_target])));