Prepare to write compressed output by flushing stdout after each chunk is compressed.

This commit is contained in:
Con Kolivas 2011-03-12 19:56:08 +11:00
parent c75a50f723
commit 7fbec0a783
5 changed files with 34 additions and 8 deletions

29
lrzip.c
View file

@ -42,13 +42,15 @@
#include "util.h"
#include "liblrzip.h" /* flag defines */
char *make_magic(rzip_control *control, int fd_in)
#define MAGIC_LEN (39)
static char *make_magic(rzip_control *control, int fd_in)
{
struct timeval tv;
struct stat st;
char *magic;
magic = calloc(39, 1);
magic = calloc(MAGIC_LEN, 1);
if (unlikely(!magic))
fatal("Failed to calloc magic in make_magic\n");
strcpy(magic, "LRZI");
@ -89,6 +91,15 @@ char *make_magic(rzip_control *control, int fd_in)
return magic;
}
void write_stdout_header(rzip_control *control, int fd_in)
{
char *magic = make_magic(control, fd_in);
memcpy(control->tmp_outbuf, magic, MAGIC_LEN);
free(magic);
}
static void write_magic(rzip_control *control, int fd_in, int fd_out)
{
char *magic = make_magic(control, fd_in);
@ -96,13 +107,15 @@ static void write_magic(rzip_control *control, int fd_in, int fd_out)
if (unlikely(lseek(fd_out, 0, SEEK_SET)))
fatal("Failed to seek to BOF to write Magic Header\n");
if (unlikely(write(fd_out, magic, 39) != 39))
if (unlikely(write(fd_out, magic, MAGIC_LEN) != MAGIC_LEN))
fatal("Failed to write magic header\n");
free(magic);
}
void read_magic(rzip_control *control, int fd_in, i64 *expected_size)
{
char magic[39];
char magic[MAGIC_LEN];
uint32_t v;
int md5, i;
@ -198,6 +211,10 @@ int open_tmpoutfile(rzip_control *control)
return fd_out;
}
void flush_stdout(rzip_control *control)
{
}
/* Dump temporary outputfile to perform stdout */
void dump_tmpoutfile(rzip_control *control, int fd_out)
{
@ -666,7 +683,7 @@ static void open_tmpoutbuf(rzip_control *control)
control->tmp_outbuf = malloc(control->maxram);
if (unlikely(!control->tmp_outbuf))
fatal("Failed to malloc tmp_outbuf in open_tmpoutbuf\n");
control->out_ofs = 39;
control->out_ofs = MAGIC_LEN;
}
/*
@ -678,7 +695,7 @@ void compress_file(rzip_control *control)
* Spares a compiler warning
*/
int fd_in, fd_out;
char header[39];
char header[MAGIC_LEN];
memset(header, 0, sizeof(header));

View file

@ -32,5 +32,6 @@ void decompress_file(rzip_control *control);
void get_header_info(rzip_control *control, int fd_in, uchar *ctype, i64 *c_len, i64 *u_len, i64 *last_head);
void get_fileinfo(rzip_control *control);
void compress_file(rzip_control *control);
char *make_magic(rzip_control *control, int fd_in);
void write_stdout_header(rzip_control *control, int fd_in);
void flush_stdout(rzip_control *control);
#endif

View file

@ -161,6 +161,7 @@ struct rzip_control {
char *tmpdir; // when stdin, stdout, or test used
char *tmp_outbuf; // Temporary file storage for stdout
i64 out_ofs; // Output offset when tmp_outbuf in use
i64 out_len; // Total length of tmp_outbuf
FILE *msgout; //stream for output messages
const char *suffix;
int compression_level;

5
rzip.c
View file

@ -958,6 +958,11 @@ retry:
last.tv_usec = current.tv_usec;
rzip_chunk(control, st, fd_in, fd_out, offset, pct_base, pct_multiple);
if (STDOUT) {
if (len == 0) // No header written yet
write_stdout_header(control, fd_in);
flush_stdout(control);
}
/* st->chunk_size may be shrunk in rzip_chunk */
last_chunk = st->chunk_size;

View file

@ -637,10 +637,12 @@ const i64 one_g = 1000 * 1024 * 1024;
ssize_t put_fdout(rzip_control *control, int fd, void *offset_buf, ssize_t ret)
{
if (!STDOUT)
if (!STDOUT || DECOMPRESS)
return write(fd, offset_buf, (size_t)ret);
memcpy(control->tmp_outbuf, offset_buf, ret);
control->out_ofs += ret;
if (likely(control->out_ofs > control->out_len))
control->out_len = control->out_ofs;
return ret;
}