tar: shrink hardlink name handling code

function                                             old     new   delta
data_extract_all                                    1069    1040     -29

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-10-22 13:30:34 +02:00
parent 62ae323df0
commit f167e4503d

View File

@ -12,12 +12,11 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
file_header_t *file_header = archive_handle->file_header; file_header_t *file_header = archive_handle->file_header;
int dst_fd; int dst_fd;
int res; int res;
char *hard_link;
#if ENABLE_FEATURE_TAR_LONG_OPTIONS #if ENABLE_FEATURE_TAR_LONG_OPTIONS
char *dst_name; char *dst_name;
char *dst_link;
#else #else
# define dst_name (file_header->name) # define dst_name (file_header->name)
# define dst_link (file_header->link_target)
#endif #endif
#if ENABLE_FEATURE_TAR_SELINUX #if ENABLE_FEATURE_TAR_SELINUX
@ -31,9 +30,14 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
} }
#endif #endif
/* Hard links are encoded as regular files of size 0
* with a nonempty link field */
hard_link = NULL;
if (S_ISREG(file_header->mode) && file_header->size == 0)
hard_link = file_header->link_target;
#if ENABLE_FEATURE_TAR_LONG_OPTIONS #if ENABLE_FEATURE_TAR_LONG_OPTIONS
dst_name = file_header->name; dst_name = file_header->name;
dst_link = file_header->link_target;
if (archive_handle->tar__strip_components) { if (archive_handle->tar__strip_components) {
unsigned n = archive_handle->tar__strip_components; unsigned n = archive_handle->tar__strip_components;
do { do {
@ -47,21 +51,18 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
* Link target is shortened only for hardlinks: * Link target is shortened only for hardlinks:
* softlinks restored unchanged. * softlinks restored unchanged.
*/ */
if (S_ISREG(file_header->mode) if (hard_link) {
&& file_header->size == 0
&& dst_link
) {
// GNU tar 1.26 does not check that we reached end of link name: // GNU tar 1.26 does not check that we reached end of link name:
// if "dir/hardlink" is hardlinked to "file", // if "dir/hardlink" is hardlinked to "file",
// tar xvf a.tar --strip-components=1 says: // tar xvf a.tar --strip-components=1 says:
// tar: hardlink: Cannot hard link to '': No such file or directory // tar: hardlink: Cannot hard link to '': No such file or directory
// and continues processing. We silently skip such entries. // and continues processing. We silently skip such entries.
dst_link = strchr(dst_link, '/'); hard_link = strchr(hard_link, '/');
if (!dst_link || dst_link[1] == '\0') { if (!hard_link || hard_link[1] == '\0') {
data_skip(archive_handle); data_skip(archive_handle);
return; return;
} }
dst_link++; hard_link++;
} }
} while (--n != 0); } while (--n != 0);
} }
@ -79,12 +80,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) { if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
/* Remove the entry if it exists */ /* Remove the entry if it exists */
if (!S_ISDIR(file_header->mode)) { if (!S_ISDIR(file_header->mode)) {
/* Is it hardlink? if (hard_link) {
* We encode hard links as regular files of size 0 with a symlink */
if (S_ISREG(file_header->mode)
&& file_header->size == 0
&& dst_link
) {
/* Ugly special case: /* Ugly special case:
* tar cf t.tar hardlink1 hardlink2 hardlink1 * tar cf t.tar hardlink1 hardlink2 hardlink1
* results in this tarball structure: * results in this tarball structure:
@ -92,7 +88,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
* hardlink2 -> hardlink1 * hardlink2 -> hardlink1
* hardlink1 -> hardlink1 <== !!! * hardlink1 -> hardlink1 <== !!!
*/ */
if (strcmp(dst_link, dst_name) == 0) if (strcmp(hard_link, dst_name) == 0)
goto ret; goto ret;
} }
/* Proceed with deleting */ /* Proceed with deleting */
@ -128,19 +124,14 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
} }
} }
/* Handle hard links separately /* Handle hard links separately */
* We encode hard links as regular files of size 0 with a symlink */ if (hard_link) {
if (S_ISREG(file_header->mode) res = link(hard_link, dst_name);
&& file_header->size == 0
&& dst_link
) {
/* Hard link */
res = link(dst_link, dst_name);
if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) { if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
bb_perror_msg("can't create %slink " bb_perror_msg("can't create %slink "
"from %s to %s", "hard", "from %s to %s", "hard",
dst_name, dst_name,
dst_link); hard_link);
} }
/* Hardlinks have no separate mode/ownership, skip chown/chmod */ /* Hardlinks have no separate mode/ownership, skip chown/chmod */
goto ret; goto ret;