Skip to content

Commit

Permalink
Merge pull request #77 from kata198/master
Browse files Browse the repository at this point in the history
Allow liblrzip to work with FILE (other than stdin) and fix memory leak
  • Loading branch information
ckolivas committed Jun 4, 2017
2 parents ac393ef + 1510f4a commit 1a30639
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
3 changes: 2 additions & 1 deletion liblrzip.c
Expand Up @@ -543,7 +543,8 @@ bool lrzip_run(Lrzip *lr)
lr->control->infile = lr->infilenames[0];
else {
lr->control->inFILE = lr->infiles[0];
control->flags |= FLAG_STDIN;
if ( lr->infiles[0] == stdin )
control->flags |= FLAG_STDIN;
}

if ((!STDOUT) && (!lr->control->msgout)) lr->control->msgout = stdout;
Expand Down
46 changes: 31 additions & 15 deletions lrzip.c
Expand Up @@ -684,7 +684,7 @@ bool decompress_file(rzip_control *control)
i64 expected_size = 0, free_space;
struct statvfs fbuf;

if (!STDIN) {
if (!STDIN && !IS_FROM_FILE) {
struct stat fdin_stat;

stat(control->infile, &fdin_stat);
Expand Down Expand Up @@ -734,7 +734,10 @@ bool decompress_file(rzip_control *control)
print_progress("Output filename is: %s\n", control->outfile);
}

if (STDIN) {
if ( IS_FROM_FILE ) {
fd_in = fileno(control->inFILE);
}
else if (STDIN) {
fd_in = open_tmpinfile(control);
read_tmpinmagic(control);
if (ENCRYPT)
Expand Down Expand Up @@ -785,8 +788,9 @@ bool decompress_file(rzip_control *control)
}
}

if (unlikely(!open_tmpoutbuf(control)))
return false;
if ( STDOUT )
if (unlikely(!open_tmpoutbuf(control)))
return false;

if (!STDIN) {
if (unlikely(!read_magic(control, fd_in, &expected_size)))
Expand Down Expand Up @@ -842,6 +846,9 @@ bool decompress_file(rzip_control *control)
else
print_progress("[OK] \n");

if (TMP_OUTBUF)
close_tmpoutbuf(control);

if (fd_out > 0) {
if (unlikely(close(fd_hist) || close(fd_out)))
fatal_return(("Failed to close files\n"), false);
Expand All @@ -850,7 +857,9 @@ bool decompress_file(rzip_control *control)
if (unlikely(!STDIN && !STDOUT && !TEST_ONLY && !preserve_times(control, fd_in)))
return false;

close(fd_in);
if ( ! IS_FROM_FILE ) {
close(fd_in);
}

if (!KEEP_FILES && !STDIN) {
if (unlikely(unlink(control->infile)))
Expand Down Expand Up @@ -947,7 +956,9 @@ bool get_fileinfo(rzip_control *control)
infilecopy = strdupa(control->infile);
}

if (STDIN)
if ( IS_FROM_FILE )
fd_in = fileno(control->inFILE);
else if (STDIN)
fd_in = 0;
else {
fd_in = open(infilecopy, O_RDONLY);
Expand All @@ -966,7 +977,8 @@ bool get_fileinfo(rzip_control *control)

if (ENCRYPT) {
print_output("Encrypted lrzip archive. No further information available\n");
if (!STDIN) close(fd_in);
if (!STDIN && !IS_FROM_FILE)
close(fd_in);
goto out;
}

Expand Down Expand Up @@ -1130,14 +1142,15 @@ bool get_fileinfo(rzip_control *control)
print_output("\n");
} else
print_output("CRC32 used for integrity testing\n");
if (unlikely(close(fd_in)))
fatal_return(("Failed to close fd_in in get_fileinfo\n"), false);
if ( !IS_FROM_FILE )
if (unlikely(close(fd_in)))
fatal_return(("Failed to close fd_in in get_fileinfo\n"), false);

out:
free(control->outfile);
return true;
error:
if (!STDIN) close(fd_in);
if (!STDIN && ! IS_FROM_FILE) close(fd_in);
return false;
}

Expand All @@ -1159,17 +1172,20 @@ bool compress_file(rzip_control *control)
return false;
memset(header, 0, sizeof(header));

if (!STDIN) {
/* is extension at end of infile? */
if ( IS_FROM_FILE )
fd_in = fileno(control->inFILE);
else if (!STDIN) {
/* is extension at end of infile? */
if ((tmp = strrchr(control->infile, '.')) && !strcmp(tmp, control->suffix)) {
print_err("%s: already has %s suffix. Skipping...\n", control->infile, control->suffix);
return false;
}

fd_in = open(control->infile, O_RDONLY);
fd_in = open(control->infile, O_RDONLY);
if (unlikely(fd_in == -1))
fatal_return(("Failed to open %s\n", control->infile), false);
} else
}
else
fd_in = 0;

if (!STDOUT) {
Expand Down Expand Up @@ -1269,7 +1285,7 @@ bool compress_file(rzip_control *control)
free(control->outfile);
return true;
error:
if (STDIN && (fd_in > 0))
if (! IS_FROM_FILE && STDIN && (fd_in > 0))
close(fd_in);
if ((!STDOUT) && (fd_out > 0))
close(fd_out);
Expand Down
2 changes: 2 additions & 0 deletions lrzip_private.h
Expand Up @@ -308,6 +308,8 @@ typedef sem_t cksem_t;
#define TMP_INBUF (control->flags & FLAG_TMP_INBUF)
#define ENCRYPT (control->flags & FLAG_ENCRYPT)

#define IS_FROM_FILE ( !!(control->inFILE) && !STDIN )


/* Structure to save state of computation between the single steps. */
struct md5_ctx
Expand Down

0 comments on commit 1a30639

Please sign in to comment.