diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2005-05-21 22:55:55 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2005-05-21 22:55:55 +0000 |
commit | 79eb56d3d5bca53797c1aeb69abf29f2f26e0206 (patch) | |
tree | 7c4fceb5e997b278477555fa45c65be03fcfc805 /paxlib | |
parent | 902d8626668f5314647fb531cfc6cd770a766ca5 (diff) | |
download | paxutils-79eb56d3d5bca53797c1aeb69abf29f2f26e0206.tar.gz |
Added to the repository
Diffstat (limited to 'paxlib')
-rw-r--r-- | paxlib/names.c | 156 | ||||
-rw-r--r-- | paxlib/paxlib.h | 115 |
2 files changed, 271 insertions, 0 deletions
diff --git a/paxlib/names.c b/paxlib/names.c new file mode 100644 index 0000000..3ca8bfa --- /dev/null +++ b/paxlib/names.c @@ -0,0 +1,156 @@ +/* This file is part of GNU paxutils + Copyright (C) 2005 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any later + version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <system.h> +#include <hash.h> +#include <paxlib.h> + + +/* Hash tables of strings. */ + +/* Calculate the hash of a string. */ +static size_t +hash_string_hasher (void const *name, size_t n_buckets) +{ + return hash_string (name, n_buckets); +} + +/* Compare two strings for equality. */ +static bool +hash_string_compare (void const *name1, void const *name2) +{ + return strcmp (name1, name2) == 0; +} + +/* Return zero if TABLE contains a copy of STRING; otherwise, insert a + copy of STRING to TABLE and return 1. */ +bool +hash_string_insert (Hash_table **table, char const *string) +{ + Hash_table *t = *table; + char *s = xstrdup (string); + char *e; + + if (! ((t + || (*table = t = hash_initialize (0, 0, hash_string_hasher, + hash_string_compare, 0))) + && (e = hash_insert (t, s)))) + xalloc_die (); + + if (e == s) + return 1; + else + { + free (s); + return 0; + } +} + +/* Return 1 if TABLE contains STRING. */ +bool +hash_string_lookup (Hash_table const *table, char const *string) +{ + return table && hash_lookup (table, string); +} + + +static Hash_table *prefix_table[2]; + +/* Return true if file names of some members in the archive were stripped off + their leading components. We could have used + return prefix_table[0] || prefix_table[1] + but the following seems to be safer: */ +bool +removed_prefixes_p (void) +{ + return (prefix_table[0] && hash_get_n_entries (prefix_table[0]) != 0) + || (prefix_table[1] && hash_get_n_entries (prefix_table[1]) != 0); +} + +/* Return a safer suffix of FILE_NAME, or "." if it has no safer + suffix. Check for fully specified file names and other atrocities. + Warn the user if we do not return NAME. If LINK_TARGET is 1, + FILE_NAME is the target of a hard link, not a member name. + If ABSOLUTE_NAMES is 0, strip filesystem prefix from the file name. */ + +char * +safer_name_suffix (char const *file_name, bool link_target, bool absolute_names) +{ + char const *p; + + if (absolute_names) + p = file_name; + else + { + /* Skip file system prefixes, leading file name components that contain + "..", and leading slashes. */ + + size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (file_name); + + for (p = file_name + prefix_len; *p; ) + { + if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2])) + prefix_len = p + 2 - file_name; + + do + { + char c = *p++; + if (ISSLASH (c)) + break; + } + while (*p); + } + + for (p = file_name + prefix_len; ISSLASH (*p); p++) + continue; + prefix_len = p - file_name; + + if (prefix_len) + { + char *prefix = alloca (prefix_len + 1); + memcpy (prefix, file_name, prefix_len); + prefix[prefix_len] = '\0'; + + if (hash_string_insert (&prefix_table[link_target], prefix)) + { + static char const *const diagnostic[] = + { + N_("Removing leading `%s' from member names"), + N_("Removing leading `%s' from hard link targets") + }; + WARN ((0, 0, _(diagnostic[link_target]), prefix)); + } + } + } + + if (! *p) + { + if (p == file_name) + { + static char const *const diagnostic[] = + { + N_("Substituting `.' for empty member name"), + N_("Substituting `.' for empty hard link target") + }; + WARN ((0, 0, "%s", _(diagnostic[link_target]))); + } + + p = "."; + } + + return (char *) p; +} diff --git a/paxlib/paxlib.h b/paxlib/paxlib.h new file mode 100644 index 0000000..03f15eb --- /dev/null +++ b/paxlib/paxlib.h @@ -0,0 +1,115 @@ +/* This file is part of GNU paxutils + + Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, + 2005 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef _paxlib_h_ +#define _paxlib_h_ + +#include <hash.h> + +/* Error reporting functions and definitions */ + +/* Exit status for paxutils app. Let's try to keep this list as simple as + possible. tar -d option strongly invites a status different for unequal + comparison and other errors. */ +#define PAXEXIT_SUCCESS 0 +#define PAXEXIT_DIFFERS 1 +#define PAXEXIT_FAILURE 2 + +/* Both WARN and ERROR write a message on stderr and continue processing, + however ERROR manages so tar will exit unsuccessfully. FATAL_ERROR + writes a message on stderr and aborts immediately, with another message + line telling so. USAGE_ERROR works like FATAL_ERROR except that the + other message line suggests trying --help. All four macros accept a + single argument of the form ((0, errno, _("FORMAT"), Args...)). errno + is zero when the error is not being detected by the system. */ + +#define WARN(Args) \ + error Args +#define ERROR(Args) \ + (error Args, exit_status = PAXEXIT_FAILURE) +#define FATAL_ERROR(Args) \ + (error Args, fatal_exit ()) +#define USAGE_ERROR(Args) \ + (error Args, usage (PAXEXIT_FAILURE)) + +extern int exit_status; + +void pax_decode_mode (mode_t mode, char *string); +void call_arg_error (char const *call, char const *name); +void call_arg_fatal (char const *call, char const *name) __attribute__ ((noreturn)); +void call_arg_warn (char const *call, char const *name); +void chmod_error_details (char const *name, mode_t mode); +void chown_error_details (char const *name, uid_t uid, gid_t gid); + +void decode_mode (mode_t, char *); + +void chdir_fatal (char const *) __attribute__ ((noreturn)); +void chmod_error_details (char const *, mode_t); +void chown_error_details (char const *, uid_t, gid_t); +void close_error (char const *); +void close_warn (char const *); +void exec_fatal (char const *) __attribute__ ((noreturn)); +void link_error (char const *, char const *); +void mkdir_error (char const *); +void mkfifo_error (char const *); +void mknod_error (char const *); +void open_error (char const *); +void open_fatal (char const *) __attribute__ ((noreturn)); +void open_warn (char const *); +void read_error (char const *); +void read_error_details (char const *, off_t, size_t); +void read_fatal (char const *) __attribute__ ((noreturn)); +void read_fatal_details (char const *, off_t, size_t) __attribute__ ((noreturn)); +void read_warn_details (char const *, off_t, size_t); +void readlink_error (char const *); +void readlink_warn (char const *); +void savedir_error (char const *); +void savedir_warn (char const *); +void seek_error (char const *); +void seek_error_details (char const *, off_t); +void seek_warn (char const *); +void seek_warn_details (char const *, off_t); +void stat_fatal (char const *); +void stat_error (char const *); +void stat_warn (char const *); +void symlink_error (char const *, char const *); +void truncate_error (char const *); +void truncate_warn (char const *); +void unlink_error (char const *); +void utime_error (char const *); +void waitpid_error (char const *); +void write_error (char const *); + +void pax_exit (void); +void fatal_exit (void) __attribute__ ((noreturn)); + +#define STRINGIFY_BIGINT(i, b) \ + stringify_uintmax_t_backwards ((uintmax_t) (i), (b) + UINTMAX_STRSIZE_BOUND) +char *stringify_uintmax_t_backwards (uintmax_t, char *); + + +/* Name-related functions */ +bool hash_string_insert (Hash_table **table, char const *string); +bool hash_string_lookup (Hash_table const *table, char const *string); + +bool removed_prefixes_p (void); +char *safer_name_suffix (char const *file_name, bool link_target, bool absolute_names); + +#endif |