diff options
author | Harald van Dijk <harald@gigawatt.nl> | 2018-05-22 17:34:31 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-05-22 17:34:31 +0200 |
commit | 8c24af9dcfe0d89a660c39838eec4d20bc13efbf (patch) | |
tree | 1be6f930fb21383ba8c46a6d0491cafe3e26d687 | |
parent | a40a661d971cf75c617c83357ac0e87a45f41f4f (diff) | |
download | busybox-w32-8c24af9dcfe0d89a660c39838eec4d20bc13efbf.tar.gz busybox-w32-8c24af9dcfe0d89a660c39838eec4d20bc13efbf.tar.bz2 busybox-w32-8c24af9dcfe0d89a660c39838eec4d20bc13efbf.zip |
tar: fix interaction of delayed symlink and hardlink creation
function old new delta
create_or_remember_link - 106 +106
create_links_from_list - 93 +93
find_applet_by_name 124 128 +4
unzip_main 2724 2726 +2
data_extract_all 891 873 -18
create_symlinks_from_list 64 - -64
create_or_remember_symlink 94 - -94
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 2/1 up/down: 205/-176) Total: 29 bytes
Signed-off-by: Harald van Dijk <harald@gigawatt.nl>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | archival/cpio.c | 2 | ||||
-rw-r--r-- | archival/libarchive/data_extract_all.c | 16 | ||||
-rw-r--r-- | archival/libarchive/get_header_ar.c | 2 | ||||
-rw-r--r-- | archival/libarchive/unsafe_symlink_target.c | 21 | ||||
-rw-r--r-- | archival/tar.c | 2 | ||||
-rw-r--r-- | archival/unzip.c | 7 | ||||
-rw-r--r-- | include/bb_archive.h | 11 | ||||
-rwxr-xr-x | testsuite/tar.tests | 16 |
8 files changed, 47 insertions, 30 deletions
diff --git a/archival/cpio.c b/archival/cpio.c index 308ec1b25..221667112 100644 --- a/archival/cpio.c +++ b/archival/cpio.c | |||
@@ -508,7 +508,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv) | |||
508 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) | 508 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
509 | continue; | 509 | continue; |
510 | 510 | ||
511 | create_symlinks_from_list(archive_handle->symlink_placeholders); | 511 | create_links_from_list(archive_handle->link_placeholders); |
512 | 512 | ||
513 | if (archive_handle->cpio__blocks != (off_t)-1 | 513 | if (archive_handle->cpio__blocks != (off_t)-1 |
514 | && !(opt & OPT_QUIET) | 514 | && !(opt & OPT_QUIET) |
diff --git a/archival/libarchive/data_extract_all.c b/archival/libarchive/data_extract_all.c index 8fa69ffaf..4c95db4a6 100644 --- a/archival/libarchive/data_extract_all.c +++ b/archival/libarchive/data_extract_all.c | |||
@@ -122,13 +122,10 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle) | |||
122 | 122 | ||
123 | /* Handle hard links separately */ | 123 | /* Handle hard links separately */ |
124 | if (hard_link) { | 124 | if (hard_link) { |
125 | res = link(hard_link, dst_name); | 125 | create_or_remember_link(&archive_handle->link_placeholders, |
126 | if (res != 0) { | 126 | hard_link, |
127 | /* shared message */ | 127 | dst_name, |
128 | bb_perror_msg("can't create %slink '%s' to '%s'", | 128 | 1); |
129 | "hard", dst_name, hard_link | ||
130 | ); | ||
131 | } | ||
132 | /* Hardlinks have no separate mode/ownership, skip chown/chmod */ | 129 | /* Hardlinks have no separate mode/ownership, skip chown/chmod */ |
133 | goto ret; | 130 | goto ret; |
134 | } | 131 | } |
@@ -195,9 +192,10 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle) | |||
195 | * | 192 | * |
196 | * Untarring bug.tar would otherwise place evil.py in '/tmp'. | 193 | * Untarring bug.tar would otherwise place evil.py in '/tmp'. |
197 | */ | 194 | */ |
198 | create_or_remember_symlink(&archive_handle->symlink_placeholders, | 195 | create_or_remember_link(&archive_handle->link_placeholders, |
199 | file_header->link_target, | 196 | file_header->link_target, |
200 | dst_name); | 197 | dst_name, |
198 | 0); | ||
201 | break; | 199 | break; |
202 | case S_IFSOCK: | 200 | case S_IFSOCK: |
203 | case S_IFBLK: | 201 | case S_IFBLK: |
diff --git a/archival/libarchive/get_header_ar.c b/archival/libarchive/get_header_ar.c index adcde46d5..7ce9c615c 100644 --- a/archival/libarchive/get_header_ar.c +++ b/archival/libarchive/get_header_ar.c | |||
@@ -126,7 +126,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle) | |||
126 | struct archive_handle_t *sa = archive_handle->dpkg__sub_archive; | 126 | struct archive_handle_t *sa = archive_handle->dpkg__sub_archive; |
127 | while (archive_handle->dpkg__action_data_subarchive(sa) == EXIT_SUCCESS) | 127 | while (archive_handle->dpkg__action_data_subarchive(sa) == EXIT_SUCCESS) |
128 | continue; | 128 | continue; |
129 | create_symlinks_from_list(sa->symlink_placeholders); | 129 | create_links_from_list(sa->link_placeholders); |
130 | } else | 130 | } else |
131 | #endif | 131 | #endif |
132 | archive_handle->action_data(archive_handle); | 132 | archive_handle->action_data(archive_handle); |
diff --git a/archival/libarchive/unsafe_symlink_target.c b/archival/libarchive/unsafe_symlink_target.c index 8dcafeaa1..f8dc8033d 100644 --- a/archival/libarchive/unsafe_symlink_target.c +++ b/archival/libarchive/unsafe_symlink_target.c | |||
@@ -5,13 +5,14 @@ | |||
5 | #include "libbb.h" | 5 | #include "libbb.h" |
6 | #include "bb_archive.h" | 6 | #include "bb_archive.h" |
7 | 7 | ||
8 | void FAST_FUNC create_or_remember_symlink(llist_t **symlink_placeholders, | 8 | void FAST_FUNC create_or_remember_link(llist_t **link_placeholders, |
9 | const char *target, | 9 | const char *target, |
10 | const char *linkname) | 10 | const char *linkname, |
11 | int hard_link) | ||
11 | { | 12 | { |
12 | if (target[0] == '/' || strstr(target, "..")) { | 13 | if (hard_link || target[0] == '/' || strstr(target, "..")) { |
13 | llist_add_to(symlink_placeholders, | 14 | llist_add_to_end(link_placeholders, |
14 | xasprintf("%s%c%s", linkname, '\0', target) | 15 | xasprintf("%c%s%c%s", hard_link, linkname, '\0', target) |
15 | ); | 16 | ); |
16 | return; | 17 | return; |
17 | } | 18 | } |
@@ -23,17 +24,17 @@ void FAST_FUNC create_or_remember_symlink(llist_t **symlink_placeholders, | |||
23 | } | 24 | } |
24 | } | 25 | } |
25 | 26 | ||
26 | void FAST_FUNC create_symlinks_from_list(llist_t *list) | 27 | void FAST_FUNC create_links_from_list(llist_t *list) |
27 | { | 28 | { |
28 | while (list) { | 29 | while (list) { |
29 | char *target; | 30 | char *target; |
30 | 31 | ||
31 | target = list->data + strlen(list->data) + 1; | 32 | target = list->data + 1 + strlen(list->data + 1) + 1; |
32 | if (symlink(target, list->data)) { | 33 | if ((*list->data ? link : symlink) (target, list->data + 1)) { |
33 | /* shared message */ | 34 | /* shared message */ |
34 | bb_error_msg_and_die("can't create %slink '%s' to '%s'", | 35 | bb_error_msg_and_die("can't create %slink '%s' to '%s'", |
35 | "sym", | 36 | *list->data ? "hard" : "sym", |
36 | list->data, target | 37 | list->data + 1, target |
37 | ); | 38 | ); |
38 | } | 39 | } |
39 | list = list->link; | 40 | list = list->link; |
diff --git a/archival/tar.c b/archival/tar.c index a8b888fa9..ae1ccc756 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -1244,7 +1244,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv) | |||
1244 | while (get_header_tar(tar_handle) == EXIT_SUCCESS) | 1244 | while (get_header_tar(tar_handle) == EXIT_SUCCESS) |
1245 | bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */ | 1245 | bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */ |
1246 | 1246 | ||
1247 | create_symlinks_from_list(tar_handle->symlink_placeholders); | 1247 | create_links_from_list(tar_handle->link_placeholders); |
1248 | 1248 | ||
1249 | /* Check that every file that should have been extracted was */ | 1249 | /* Check that every file that should have been extracted was */ |
1250 | while (tar_handle->accept) { | 1250 | while (tar_handle->accept) { |
diff --git a/archival/unzip.c b/archival/unzip.c index 0d00d8dc9..96b7ab69b 100644 --- a/archival/unzip.c +++ b/archival/unzip.c | |||
@@ -372,9 +372,10 @@ static void unzip_extract_symlink(llist_t **symlink_placeholders, | |||
372 | target[xstate.mem_output_size] = '\0'; | 372 | target[xstate.mem_output_size] = '\0'; |
373 | #endif | 373 | #endif |
374 | } | 374 | } |
375 | create_or_remember_symlink(symlink_placeholders, | 375 | create_or_remember_link(symlink_placeholders, |
376 | target, | 376 | target, |
377 | dst_fn); | 377 | dst_fn, |
378 | 0); | ||
378 | free(target); | 379 | free(target); |
379 | } | 380 | } |
380 | #endif | 381 | #endif |
@@ -990,7 +991,7 @@ int unzip_main(int argc, char **argv) | |||
990 | } | 991 | } |
991 | 992 | ||
992 | #if ENABLE_FEATURE_UNZIP_CDF | 993 | #if ENABLE_FEATURE_UNZIP_CDF |
993 | create_symlinks_from_list(symlink_placeholders); | 994 | create_links_from_list(symlink_placeholders); |
994 | #endif | 995 | #endif |
995 | 996 | ||
996 | if ((opts & OPT_l) && quiet <= 1) { | 997 | if ((opts & OPT_l) && quiet <= 1) { |
diff --git a/include/bb_archive.h b/include/bb_archive.h index 0252488bf..2ee30f794 100644 --- a/include/bb_archive.h +++ b/include/bb_archive.h | |||
@@ -64,8 +64,8 @@ typedef struct archive_handle_t { | |||
64 | /* Currently processed file's header */ | 64 | /* Currently processed file's header */ |
65 | file_header_t *file_header; | 65 | file_header_t *file_header; |
66 | 66 | ||
67 | /* List of symlink placeholders */ | 67 | /* List of link placeholders */ |
68 | llist_t *symlink_placeholders; | 68 | llist_t *link_placeholders; |
69 | 69 | ||
70 | /* Process the header component, e.g. tar -t */ | 70 | /* Process the header component, e.g. tar -t */ |
71 | void FAST_FUNC (*action_header)(const file_header_t *); | 71 | void FAST_FUNC (*action_header)(const file_header_t *); |
@@ -203,10 +203,11 @@ void seek_by_jump(int fd, off_t amount) FAST_FUNC; | |||
203 | void seek_by_read(int fd, off_t amount) FAST_FUNC; | 203 | void seek_by_read(int fd, off_t amount) FAST_FUNC; |
204 | 204 | ||
205 | const char *strip_unsafe_prefix(const char *str) FAST_FUNC; | 205 | const char *strip_unsafe_prefix(const char *str) FAST_FUNC; |
206 | void create_or_remember_symlink(llist_t **symlink_placeholders, | 206 | void create_or_remember_link(llist_t **link_placeholders, |
207 | const char *target, | 207 | const char *target, |
208 | const char *linkname) FAST_FUNC; | 208 | const char *linkname, |
209 | void create_symlinks_from_list(llist_t *list) FAST_FUNC; | 209 | int hard_link) FAST_FUNC; |
210 | void create_links_from_list(llist_t *list) FAST_FUNC; | ||
210 | 211 | ||
211 | void data_align(archive_handle_t *archive_handle, unsigned boundary) FAST_FUNC; | 212 | void data_align(archive_handle_t *archive_handle, unsigned boundary) FAST_FUNC; |
212 | const llist_t *find_list_entry(const llist_t *list, const char *filename) FAST_FUNC; | 213 | const llist_t *find_list_entry(const llist_t *list, const char *filename) FAST_FUNC; |
diff --git a/testsuite/tar.tests b/testsuite/tar.tests index 1675b07b1..6c479d3fe 100755 --- a/testsuite/tar.tests +++ b/testsuite/tar.tests | |||
@@ -365,4 +365,20 @@ n8fYaKlioCTzL2oXYczyUUIP4u5IpwoSEwWdtoA= | |||
365 | SKIP= | 365 | SKIP= |
366 | cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null | 366 | cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null |
367 | 367 | ||
368 | mkdir tar.tempdir && cd tar.tempdir || exit 1 | ||
369 | testing "Symlinks and hardlinks coexist" '\ | ||
370 | mkdir dir | ||
371 | >dir/a | ||
372 | ln -s ../dir/a dir/b | ||
373 | ln dir/b dir/c | ||
374 | mkdir new | ||
375 | tar cf - dir/* | tar -C new -xvf - 2>&1 | ||
376 | ' "\ | ||
377 | dir/a | ||
378 | dir/b | ||
379 | dir/c | ||
380 | " \ | ||
381 | "" "" | ||
382 | cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null | ||
383 | |||
368 | exit $FAILCOUNT | 384 | exit $FAILCOUNT |