File unzip-support-symlinks.-Closes-10031.patch of Package busybox

From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Thu Jul 20 18:34:51 2017 +0200
Subject: unzip: support symlinks. Closes 10031
Patch-mainline: 13ae85edd1e097299297062a1054ccbe8fb97fe3
Git-repo: https://git.busybox.net/busybox
Git-commit: e3f6d5e237f1a43a18dddcb464575e9e9d312bb2
References: 

function                                             old     new   delta
unzip_main                                          2519    2667    +148

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Egbert Eich <eich@suse.de>
---
 archival/unzip.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 48 insertions(+), 6 deletions(-)
diff --git a/archival/unzip.c b/archival/unzip.c
index aea06b57d..105d725f0 100644
--- a/archival/unzip.c
+++ b/archival/unzip.c
@@ -329,6 +329,38 @@ static void unzip_create_leading_dirs(const char *fn)
 	free(name);
 }
 
+static void unzip_extract_symlink(zip_header_t *zip, const char *dst_fn)
+{
+	char *target;
+
+	if (zip->fmt.ucmpsize > 0xfff) /* no funny business please */
+		bb_error_msg_and_die("bad archive");
+
+	if (zip->fmt.method == 0) {
+		/* Method 0 - stored (not compressed) */
+		target = xzalloc(zip->fmt.ucmpsize + 1);
+		xread(zip_fd, target, zip->fmt.ucmpsize);
+	} else {
+#if 1
+		bb_error_msg_and_die("compressed symlink is not supported");
+#else
+		transformer_state_t xstate;
+		init_transformer_state(&xstate);
+		xstate.mem_output_size_max = zip->fmt.ucmpsize;
+		/* ...unpack... */
+		if (!xstate.mem_output_buf)
+			WTF();
+		target = xstate.mem_output_buf;
+		target = xrealloc(target, xstate.mem_output_size + 1);
+		target[xstate.mem_output_size] = '\0';
+#endif
+	}
+//TODO: libbb candidate
+	if (symlink(target, dst_fn))
+		bb_perror_msg_and_die("can't create symlink '%s'", dst_fn);
+	free(target);
+}
+
 static void unzip_extract(zip_header_t *zip, int dst_fd)
 {
 	transformer_state_t xstate;
@@ -799,7 +831,7 @@ int unzip_main(int argc, char **argv)
 		}
  check_file:
 		/* Extract file */
-		if (stat(dst_fn, &stat_buf) == -1) {
+		if (lstat(dst_fn, &stat_buf) == -1) {
 			/* File does not exist */
 			if (errno != ENOENT) {
 				bb_perror_msg_and_die("can't stat '%s'", dst_fn);
@@ -820,6 +852,7 @@ int unzip_main(int argc, char **argv)
 			goto do_open_and_extract;
 		printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
 		my_fgets80(key_buf);
+//TODO: redo lstat + ISREG check! user input could have taken a long time!
 
 		switch (key_buf[0]) {
 		case 'A':
@@ -828,7 +861,8 @@ int unzip_main(int argc, char **argv)
  do_open_and_extract:
 			unzip_create_leading_dirs(dst_fn);
 #if ENABLE_FEATURE_UNZIP_CDF
-			dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
+			if (!S_ISLNK(file_mode))
+				dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
 #else
 			dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
 #endif
@@ -838,10 +872,18 @@ int unzip_main(int argc, char **argv)
 					? " extracting: %s\n"
 					: */ "  inflating: %s\n", dst_fn);
 			}
-			unzip_extract(&zip, dst_fd);
-			if (dst_fd != STDOUT_FILENO) {
-				/* closing STDOUT is potentially bad for future business */
-				close(dst_fd);
+#if ENABLE_FEATURE_UNZIP_CDF
+			if (S_ISLNK(file_mode)) {
+				if (dst_fd != STDOUT_FILENO) /* no -p */
+					unzip_extract_symlink(&zip, dst_fn);
+			} else
+#endif
+			{
+				unzip_extract(&zip, dst_fd);
+				if (dst_fd != STDOUT_FILENO) {
+					/* closing STDOUT is potentially bad for future business */
+					close(dst_fd);
+				}
 			}
 			break;
 
openSUSE Build Service is sponsored by