aboutsummaryrefslogtreecommitdiff
path: root/tar/tar2sqfs.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-11-21 19:01:47 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-11-22 11:14:14 +0100
commitb4391c2c6e1d6a29ed4b8feed13a7f849e75bed9 (patch)
treefd7c791f41820772748d5c49ecfec00aa769cabe /tar/tar2sqfs.c
parent9071ddf137ea8a25de318161c2ed15345a5d5e6b (diff)
Ensure that tar2sqfs & sqfs2tar set stdin/out to binary mode
As usual, Windows has things different and is the platform where the problem was actually discovered. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tar/tar2sqfs.c')
-rw-r--r--tar/tar2sqfs.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c
index 94168fc..69ac5cf 100644
--- a/tar/tar2sqfs.c
+++ b/tar/tar2sqfs.c
@@ -13,6 +13,11 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
+#include <fcntl.h>
+
+#ifdef _WIN32
+#include <io.h>
+#endif
static struct option long_opts[] = {
{ "compressor", required_argument, NULL, 'c' },
@@ -87,6 +92,7 @@ static bool dont_skip = false;
static bool keep_time = true;
static sqfs_writer_cfg_t cfg;
static sqfs_writer_t sqfs;
+static FILE *input_file = NULL;
static void process_args(int argc, char **argv)
{
@@ -229,13 +235,13 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi,
for (sum = 0, it = hdr->sparse; it != NULL; it = it->next)
sum += it->count;
- file = sqfs_get_stdin_file(hdr->sparse, sum);
+ file = sqfs_get_stdin_file(input_file, hdr->sparse, sum);
if (file == NULL) {
perror("packing files");
return -1;
}
} else {
- file = sqfs_get_stdin_file(NULL, filesize);
+ file = sqfs_get_stdin_file(input_file, NULL, filesize);
if (file == NULL) {
perror("packing files");
return -1;
@@ -251,7 +257,7 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi,
if (ret)
return -1;
- return skip_padding(stdin, hdr->sparse == NULL ?
+ return skip_padding(input_file, hdr->sparse == NULL ?
filesize : hdr->record_size);
}
@@ -337,7 +343,7 @@ static int process_tar_ball(void)
int ret;
for (;;) {
- ret = read_header(stdin, &hdr);
+ ret = read_header(input_file, &hdr);
if (ret > 0)
break;
if (ret < 0)
@@ -395,7 +401,7 @@ static int process_tar_ball(void)
if (skip) {
if (dont_skip)
goto fail;
- if (skip_entry(stdin, hdr.sb.st_size))
+ if (skip_entry(input_file, hdr.sb.st_size))
goto fail;
clear_header(&hdr);
@@ -420,6 +426,18 @@ int main(int argc, char **argv)
process_args(argc, argv);
+#ifdef _WIN32
+ _setmode(_fileno(stdin), _O_BINARY);
+ input_file = stdin;
+#else
+ input_file = freopen(NULL, "rb", stdin);
+#endif
+
+ if (input_file == NULL) {
+ perror("changing stdin to binary mode");
+ return EXIT_FAILURE;
+ }
+
if (sqfs_writer_init(&sqfs, &cfg))
return EXIT_FAILURE;