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:
@@ -636,6 +636,7 @@ int set_file_atime (int fd, char const *file,
|
||||
|
||||
/* Module names.c. */
|
||||
|
||||
extern size_t name_count;
|
||||
extern struct name *gnu_list_name;
|
||||
|
||||
void gid_to_gname (gid_t gid, char **gname);
|
||||
|
||||
13
src/create.c
13
src/create.c
@@ -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;
|
||||
|
||||
26
src/names.c
26
src/names.c
@@ -226,19 +226,20 @@ struct name_elt /* A name_array element. */
|
||||
};
|
||||
|
||||
static struct name_elt *name_array; /* store an array of names */
|
||||
static size_t allocated_names; /* how big is the array? */
|
||||
static size_t names; /* how many entries does it have? */
|
||||
static size_t name_index; /* how many of the entries have we scanned? */
|
||||
static size_t allocated_entries; /* how big is the array? */
|
||||
static size_t entries; /* how many entries does it have? */
|
||||
static size_t scanned; /* how many of the entries have we scanned? */
|
||||
size_t name_count; /* how many of the entries are names? */
|
||||
|
||||
/* Check the size of name_array, reallocating it as necessary. */
|
||||
static void
|
||||
check_name_alloc (void)
|
||||
{
|
||||
if (names == allocated_names)
|
||||
if (entries == allocated_entries)
|
||||
{
|
||||
if (allocated_names == 0)
|
||||
allocated_names = 10; /* Set initial allocation */
|
||||
name_array = x2nrealloc (name_array, &allocated_names,
|
||||
if (allocated_entries == 0)
|
||||
allocated_entries = 10; /* Set initial allocation */
|
||||
name_array = x2nrealloc (name_array, &allocated_entries,
|
||||
sizeof (name_array[0]));
|
||||
}
|
||||
}
|
||||
@@ -251,17 +252,18 @@ name_add_name (const char *name, int matching_flags)
|
||||
struct name_elt *ep;
|
||||
|
||||
check_name_alloc ();
|
||||
ep = &name_array[names++];
|
||||
ep = &name_array[entries++];
|
||||
if (prev_flags != matching_flags)
|
||||
{
|
||||
ep->type = NELT_FMASK;
|
||||
ep->v.matching_flags = matching_flags;
|
||||
prev_flags = matching_flags;
|
||||
check_name_alloc ();
|
||||
ep = &name_array[names++];
|
||||
ep = &name_array[entries++];
|
||||
}
|
||||
ep->type = NELT_NAME;
|
||||
ep->v.name = name;
|
||||
name_count++;
|
||||
}
|
||||
|
||||
/* Add to name_array a chdir request for the directory NAME */
|
||||
@@ -270,7 +272,7 @@ name_add_dir (const char *name)
|
||||
{
|
||||
struct name_elt *ep;
|
||||
check_name_alloc ();
|
||||
ep = &name_array[names++];
|
||||
ep = &name_array[entries++];
|
||||
ep->type = NELT_CHDIR;
|
||||
ep->v.name = name;
|
||||
}
|
||||
@@ -314,12 +316,12 @@ name_next_elt (int change_dirs)
|
||||
const char *source;
|
||||
char *cursor;
|
||||
|
||||
while (name_index != names)
|
||||
while (scanned != entries)
|
||||
{
|
||||
struct name_elt *ep;
|
||||
size_t source_len;
|
||||
|
||||
ep = &name_array[name_index++];
|
||||
ep = &name_array[scanned++];
|
||||
if (ep->type == NELT_FMASK)
|
||||
{
|
||||
matching_flags = ep->v.matching_flags;
|
||||
|
||||
Reference in New Issue
Block a user