bug-tar
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-tar] [PATCH] tar: handle files that occur multiple times but have l


From: Paul Eggert
Subject: [Bug-tar] [PATCH] tar: handle files that occur multiple times but have link count 1
Date: Mon, 23 Aug 2010 19:16:26 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6

I just now installed the following patch.  I'm not sure whether it's
worth a NEWS entry, but if so, please let me know and I'll write one.

>From 6e4593c2b0bcfd20d3ecbe72b3cdcf7149d88f14 Mon Sep 17 00:00:00 2001
From: Paul Eggert <address@hidden>
Date: Mon, 23 Aug 2010 19:12:25 -0700
Subject: [PATCH] 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.
---
 src/common.h       |    1 +
 src/create.c       |   13 +++++++++-
 src/names.c        |   26 ++++++++++++----------
 tests/Makefile.am  |    1 +
 tests/link04.at    |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/testsuite.at |    1 +
 6 files changed, 86 insertions(+), 14 deletions(-)
 create mode 100644 tests/link04.at

diff --git a/src/common.h b/src/common.h
index 24227f4..9d0b779 100644
--- a/src/common.h
+++ b/src/common.h
@@ -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);
diff --git a/src/create.c b/src/create.c
index ce7d966..0a6bacf 100644
--- a/src/create.c
+++ b/src/create.c
@@ -1266,6 +1266,12 @@ dump_dir (int fd, struct tar_stat_info *st, bool 
top_level,
 }
 
 
+/* 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.  */
 
 void
@@ -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;
diff --git a/src/names.c b/src/names.c
index f1e5ab1..d2f14b0 100644
--- a/src/names.c
+++ b/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;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 01b0bae..e2e338a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 0000000..a950476
--- /dev/null
+++ b/tests/link04.at
@@ -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
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 1048a0a..d8b9b09 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -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])
-- 
1.7.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]