unzip: support symlinks. Closes 10031
function old new delta unzip_main 2519 2667 +148 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
@ -334,6 +334,38 @@ static void unzip_create_leading_dirs(const char *fn)
|
|||||||
free(name);
|
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)
|
static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||||
{
|
{
|
||||||
transformer_state_t xstate;
|
transformer_state_t xstate;
|
||||||
@ -346,12 +378,6 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NB: to support symlinks, need to extract symlink target. A-la:
|
|
||||||
// xstate.mem_output_size_max = zip->fmt.ucmpsize;
|
|
||||||
// ...unpack...
|
|
||||||
// if (xstate.mem_output_buf) { success, xstate.mem_output_size is the size }
|
|
||||||
// Although archives I've seen have fmt.method == 0 for symlinks.
|
|
||||||
|
|
||||||
init_transformer_state(&xstate);
|
init_transformer_state(&xstate);
|
||||||
xstate.bytes_in = zip->fmt.cmpsize;
|
xstate.bytes_in = zip->fmt.cmpsize;
|
||||||
xstate.src_fd = zip_fd;
|
xstate.src_fd = zip_fd;
|
||||||
@ -719,7 +745,6 @@ int unzip_main(int argc, char **argv)
|
|||||||
if ((cdf.fmt.version_made_by >> 8) == 3) {
|
if ((cdf.fmt.version_made_by >> 8) == 3) {
|
||||||
/* This archive is created on Unix */
|
/* This archive is created on Unix */
|
||||||
dir_mode = file_mode = (cdf.fmt.external_attributes >> 16);
|
dir_mode = file_mode = (cdf.fmt.external_attributes >> 16);
|
||||||
//TODO: if (S_ISLNK(file_mode)) this is a symlink
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -833,7 +858,7 @@ int unzip_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
check_file:
|
check_file:
|
||||||
/* Extract file */
|
/* Extract file */
|
||||||
if (stat(dst_fn, &stat_buf) == -1) {
|
if (lstat(dst_fn, &stat_buf) == -1) {
|
||||||
/* File does not exist */
|
/* File does not exist */
|
||||||
if (errno != ENOENT) {
|
if (errno != ENOENT) {
|
||||||
bb_perror_msg_and_die("can't stat '%s'", dst_fn);
|
bb_perror_msg_and_die("can't stat '%s'", dst_fn);
|
||||||
@ -854,6 +879,7 @@ int unzip_main(int argc, char **argv)
|
|||||||
goto do_open_and_extract;
|
goto do_open_and_extract;
|
||||||
printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
|
printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
|
||||||
my_fgets80(key_buf);
|
my_fgets80(key_buf);
|
||||||
|
//TODO: redo lstat + ISREG check! user input could have taken a long time!
|
||||||
|
|
||||||
switch (key_buf[0]) {
|
switch (key_buf[0]) {
|
||||||
case 'A':
|
case 'A':
|
||||||
@ -862,7 +888,8 @@ int unzip_main(int argc, char **argv)
|
|||||||
do_open_and_extract:
|
do_open_and_extract:
|
||||||
unzip_create_leading_dirs(dst_fn);
|
unzip_create_leading_dirs(dst_fn);
|
||||||
#if ENABLE_FEATURE_UNZIP_CDF
|
#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
|
#else
|
||||||
dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
|
dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
|
||||||
#endif
|
#endif
|
||||||
@ -872,10 +899,18 @@ int unzip_main(int argc, char **argv)
|
|||||||
? " extracting: %s\n"
|
? " extracting: %s\n"
|
||||||
: */ " inflating: %s\n", dst_fn);
|
: */ " inflating: %s\n", dst_fn);
|
||||||
}
|
}
|
||||||
unzip_extract(&zip, dst_fd);
|
#if ENABLE_FEATURE_UNZIP_CDF
|
||||||
if (dst_fd != STDOUT_FILENO) {
|
if (S_ISLNK(file_mode)) {
|
||||||
/* closing STDOUT is potentially bad for future business */
|
if (dst_fd != STDOUT_FILENO) /* no -p */
|
||||||
close(dst_fd);
|
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;
|
break;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user