]> Dogcows Code - chaz/tar/commitdiff
tar: handle files that occur multiple times but have link count 1
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 24 Aug 2010 02:12:25 +0000 (19:12 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 24 Aug 2010 02:13:12 +0000 (19:13 -0700)
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.

src/common.h
src/create.c
src/names.c
tests/Makefile.am
tests/link04.at [new file with mode: 0644]
tests/testsuite.at

index 24227f4bd7adfb0ced64d404315436bdd37fc759..9d0b779abc08ce0b2a14c6f71b66fd4daadde3b0 100644 (file)
@@ -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);
index ce7d966f6aec94e0652736f33eadbd372f53354b..0a6bacf183aac19b32ccb8acff57dd5696b39d30 100644 (file)
@@ -1265,6 +1265,12 @@ dump_dir (int fd, struct tar_stat_info *st, bool top_level,
   return true;
 }
 
+\f
+/* 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;
+
 \f
 /* 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;
index f1e5ab1bee33a8c5af3ea1f7c0482d9dbcdf1cf2..d2f14b04f20b860f8d0fbdd063f7e6cefe18545b 100644 (file)
@@ -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;
index 01b0bae6d05a7a9b7540f73a3e2ce03487d29c03..e2e338a18a1e9bde405850e213df4beaf13500ae 100644 (file)
@@ -97,6 +97,7 @@ TESTSUITE_AT = \
  link01.at\
  link02.at\
  link03.at\
+ link04.at\
  listed01.at\
  listed02.at\
  listed03.at\
diff --git a/tests/link04.at b/tests/link04.at
new file mode 100644 (file)
index 0000000..a950476
--- /dev/null
@@ -0,0 +1,58 @@
+# Process this file with autom4te to create testsuite. -*- Autotest -*-
+
+# Test suite for GNU tar.
+# Copyright (C) 2010 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 3, 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.
+
+# written by Paul Eggert
+
+AT_SETUP([link count is 1 but multiple occurrences])
+AT_KEYWORDS([hardlinks link04])
+
+AT_TAR_CHECK([
+mkdir dir
+echo TEST > dir/file
+ln -s file dir/symlink || AT_SKIP_TEST
+
+tar cf archive dir dir
+tar tvf archive | sed '
+  s,.*[[0-9]] dir/,dir/,
+' | sort
+
+echo ==
+
+tar chf archive dir
+tar tvf archive | sed '
+  s,.*[[0-9]] dir/,dir/,
+  s,file,FOO,g
+  s,symlink,FOO,g
+' | sort
+],
+[0],
+[dir/
+dir/
+dir/file
+dir/file link to dir/file
+dir/symlink -> file
+dir/symlink link to dir/symlink
+==
+dir/
+dir/FOO
+dir/FOO link to dir/FOO
+])
+
+AT_CLEANUP
index 1048a0a785a4f9746e03f7cc8c7f1cbdce8a4aa1..d8b9b099806fa32ffe8f1be899da861613858734 100644 (file)
@@ -184,6 +184,7 @@ m4_include([ignfail.at])
 m4_include([link01.at])
 m4_include([link02.at])
 m4_include([link03.at])
+m4_include([link04.at])
 
 m4_include([longv7.at])
 m4_include([long01.at])
This page took 0.027776 seconds and 4 git commands to generate.