Ability to specify output compressed pathname.

Fix log level handling.
Trim commented code.
This commit is contained in:
Moinak Ghosh 2013-10-10 21:19:44 +05:30
parent 8c1f4ebe61
commit 28fd9848f9
3 changed files with 54 additions and 53 deletions

View file

@ -56,7 +56,7 @@ Usage
===== =====
To compress a file: To compress a file:
pcompress -c <algorithm> [-l <compress level>] [-s <chunk size>] <file> [-] pcompress -c <algorithm> [-l <compress level>] [-s <chunk size>] <file> [<target file>]
Where <algorithm> can be the folowing: Where <algorithm> can be the folowing:
lzfx - Very fast and small algorithm based on LZF. lzfx - Very fast and small algorithm based on LZF.
@ -90,8 +90,10 @@ Usage
just a hint and may get automatically adjusted. just a hint and may get automatically adjusted.
<compress_level> - Can be a number from 0 meaning minimum and 14 meaning <compress_level> - Can be a number from 0 meaning minimum and 14 meaning
maximum compression. maximum compression.
'-' - If '-' is given as the final argument then it specifies that <target file> - Optional argument specifying the destination compressed
compressed output should go to stdout. file. The '.pz' extension is appended. If this is '-' then
compressed output goes to stdout. If this argument is omitted then
source filename is used with the extension '.pz' appended.
NOTE: The option "libbsc" uses Ilya Grebnov's block sorting compression library NOTE: The option "libbsc" uses Ilya Grebnov's block sorting compression library
from http://libbsc.com/ . It is only available if pcompress in built with from http://libbsc.com/ . It is only available if pcompress in built with

View file

@ -85,7 +85,7 @@ usage(pc_ctx_t *pctx)
"\nPcompress Version %s\n\n" "\nPcompress Version %s\n\n"
"Usage:\n" "Usage:\n"
"1) To compress a file:\n" "1) To compress a file:\n"
" %s -c <algorithm> [-l <compress level>] [-s <chunk size>] <file>\n" " %s -c <algorithm> [-l <compress level>] [-s <chunk size>] <file> [<target file>]\n"
" Where <algorithm> can be the folowing:\n" " Where <algorithm> can be the folowing:\n"
" lzfx - Very fast and small algorithm based on LZF.\n" " lzfx - Very fast and small algorithm based on LZF.\n"
" lz4 - Ultra fast, high-throughput algorithm reaching RAM B/W at level1.\n" " lz4 - Ultra fast, high-throughput algorithm reaching RAM B/W at level1.\n"
@ -115,6 +115,10 @@ usage(pc_ctx_t *pctx)
" Larger chunks produce better compression at the cost of memory.\n" " Larger chunks produce better compression at the cost of memory.\n"
" <compress_level> - Can be a number from 0 meaning minimum and 14 meaning\n" " <compress_level> - Can be a number from 0 meaning minimum and 14 meaning\n"
" maximum compression.\n\n" " maximum compression.\n\n"
" <target file> - Optional argument specifying the destination compressed\n"
" file. The '.pz' extension is appended. If this is '-' then\n"
" compressed output goes to stdout. If this argument is omitted then\n"
" source filename is used with the extension '.pz' appended.\n"
"2) To decompress a file compressed using above command:\n" "2) To decompress a file compressed using above command:\n"
" %s -d <compressed file> <target file>\n" " %s -d <compressed file> <target file>\n"
"3) To operate as a pipe, read from stdin and write to stdout:\n" "3) To operate as a pipe, read from stdin and write to stdout:\n"
@ -1882,6 +1886,7 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
COMP_BAIL; COMP_BAIL;
} }
} else { } else {
if (pctx->to_filename == NULL) {
strcat(tmpfile1, "/.pcompXXXXXX"); strcat(tmpfile1, "/.pcompXXXXXX");
snprintf(to_filename, sizeof (to_filename), "%s" COMP_EXTN, filename); snprintf(to_filename, sizeof (to_filename), "%s" COMP_EXTN, filename);
if ((compfd = mkstemp(tmpfile1)) == -1) { if ((compfd = mkstemp(tmpfile1)) == -1) {
@ -1889,6 +1894,14 @@ start_compress(pc_ctx_t *pctx, const char *filename, uint64_t chunksize, int lev
COMP_BAIL; COMP_BAIL;
} }
add_fname(tmpfile1); add_fname(tmpfile1);
} else {
snprintf(to_filename, sizeof (to_filename), "%s" COMP_EXTN, pctx->to_filename);
if ((compfd = open(to_filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) == -1) {
perror("open ");
COMP_BAIL;
}
add_fname(to_filename);
}
} }
signal(SIGINT, Int_Handler); signal(SIGINT, Int_Handler);
signal(SIGTERM, Int_Handler); signal(SIGTERM, Int_Handler);
@ -2369,12 +2382,17 @@ comp_done:
fchmod(compfd, sbuf.st_mode); fchmod(compfd, sbuf.st_mode);
if (fchown(compfd, sbuf.st_uid, sbuf.st_gid) == -1) if (fchown(compfd, sbuf.st_uid, sbuf.st_gid) == -1)
perror("chown "); perror("chown ");
close(compfd);
if (pctx->to_filename == NULL) {
if (rename(tmpfile1, to_filename) == -1) { if (rename(tmpfile1, to_filename) == -1) {
perror("Cannot rename temporary file "); perror("Cannot rename temporary file ");
unlink(tmpfile1); unlink(tmpfile1);
} }
rm_fname(tmpfile1); rm_fname(tmpfile1);
} else {
rm_fname(to_filename);
}
} }
} }
if (dary != NULL) { if (dary != NULL) {
@ -2823,13 +2841,11 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
if (*(argv[my_optind]) == '-') { if (*(argv[my_optind]) == '-') {
pctx->to_filename = "-"; pctx->to_filename = "-";
pctx->pipe_out = 1; pctx->pipe_out = 1;
} pctx->to_filename = NULL;
pctx->to_filename = realpath(argv[my_optind], NULL);
} else { } else {
strcpy(apath, pctx->filename); strcpy(apath, argv[my_optind]);
strcat(apath, COMP_EXTN); strcat(apath, COMP_EXTN);
pctx->to_filename = realpath(apath, NULL); pctx->to_filename = realpath(apath, NULL);
}
/* Check if compressed file exists */ /* Check if compressed file exists */
if (pctx->to_filename != NULL) { if (pctx->to_filename != NULL) {
@ -2837,6 +2853,20 @@ init_pc_context(pc_ctx_t *pctx, int argc, char *argv[])
free((void *)(pctx->to_filename)); free((void *)(pctx->to_filename));
return (1); return (1);
} }
pctx->to_filename = argv[my_optind];
}
} else {
strcpy(apath, pctx->filename);
strcat(apath, COMP_EXTN);
pctx->to_filename = realpath(apath, NULL);
/* Check if compressed file exists */
if (pctx->to_filename != NULL) {
log_msg(LOG_ERR, 0, "Compressed file %s exists\n", pctx->to_filename);
free((void *)(pctx->to_filename));
return (1);
}
}
} else if (pctx->do_uncompress && num_rem == 2) { } else if (pctx->do_uncompress && num_rem == 2) {
/* /*
* While decompressing, input can be stdin and output a physical file. * While decompressing, input can be stdin and output a physical file.

View file

@ -57,37 +57,6 @@ init_pcompress() {
XXH32_module_init(); XXH32_module_init();
} }
/*
void
err_exit(int show_errno, const char *format, ...)
{
int err = errno;
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
if (show_errno)
fprintf(stderr, "\nError: %s\n", strerror(err));
exit(1);
}
void
err_print(int show_errno, const char *format, ...)
{
int err = errno;
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
if (show_errno)
fprintf(stderr, "\nError: %s\n", strerror(err));
}
*/
/* /*
* Fetch the command name that started the current process. * Fetch the command name that started the current process.
* The returned string must be freed by the caller. * The returned string must be freed by the caller.
@ -475,7 +444,7 @@ log_msg(log_level_t log_level, int show_errno, const char *format, ...)
va_list args; va_list args;
char msg[1024]; char msg[1024];
if (log_level <= cur_log_level) return; if (log_level > cur_log_level) return;
va_start(args, format); va_start(args, format);
written = vsnprintf(msg, 1024, format, args); written = vsnprintf(msg, 1024, format, args);