tar: handle files that occur multiple times but have link count 1

This patch was inspired by the following patch that addressed a
similar problem in GNU coreutils du:
http://git.savannah.gnu.org/gitweb/?p=coreutils.git;h=efe53cc72b599979ea292754ecfe8abf7c839d22
* src/common.h (name_count): New decl.
* src/create.c (trivial_link_count): New static var.
(create_archive): Initialize it.
(dump_hard_link, file_count_links): Use it, so that files with
link count 1 are handled correctly when they are found multiple times.
* src/names.c (allocated_entries): Renamed from allocated_names,
since the identifier's name was misleading.  All uses changed.
(entries): Renamed from names.  All uses changed.
(scanned): Renamed from name_index.  All uses changed.
(name_count): New var.
(name_add_name): Increment it.
* tests/link04.at: New file.
* tests/testsuite.at: Add it.
* tests/Makefile.am (TESTSUITE_AT): Likewise.
This commit is contained in:
Paul Eggert
2010-08-23 19:12:25 -07:00
parent 09f039050e
commit 37ddfb0b7e
6 changed files with 86 additions and 14 deletions

View File

@@ -1265,6 +1265,12 @@ dump_dir (int fd, struct tar_stat_info *st, bool top_level,
return true;
}
/* Number of links a file can have without having to be entered into
the link table. Typically this is 1, but in trickier circumstances
it is 0. */
static nlink_t trivial_link_count;
/* Main functions of this module. */
@@ -1273,6 +1279,8 @@ create_archive (void)
{
struct name const *p;
trivial_link_count = name_count <= 1 && ! dereference_option;
open_archive (ACCESS_WRITE);
buffer_write_global_xheader ();
@@ -1380,7 +1388,8 @@ static Hash_table *link_table;
static bool
dump_hard_link (struct tar_stat_info *st)
{
if (link_table && (st->stat.st_nlink > 1 || remove_files_option))
if (link_table
&& (trivial_link_count < st->stat.st_nlink || remove_files_option))
{
struct link lp;
struct link *duplicate;
@@ -1427,7 +1436,7 @@ file_count_links (struct tar_stat_info *st)
{
if (hard_dereference_option)
return;
if (st->stat.st_nlink > 1)
if (trivial_link_count < st->stat.st_nlink)
{
struct link *duplicate;
char *linkname = NULL;