1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
*
* Copyright (c) 2018, Jue Ruan <ruanjue@gmail.com>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pgzf.h"
int usage(int ret){
fprintf(stdout,
"PGZF: Parallel gzip file IO\n"
"Author: Jue Ruan <ruanjue@gmail.com>\n"
"Version: 1.1\n"
"Usage: pgzf [options] file1 [file2 ...]\n"
"Options:\n"
" -d Decompress mode\n"
" -t <int> Number of threads, [8]\n"
" -f Force to overwrite\n"
" -o <string> Output file name, support directory\n"
" -x Delete input files after done\n"
" -b <int> Block size in MB, 1 ~ 256 [16]\n"
" -l <int> Compress level, 1-9, see gzip, [6]\n"
" -h Show this document\n"
" -V Print version information and exit\n"
"\n"
"File format:\n"
" PGZF fellows standard GZIP format (rfc1952), and is blocked compressed.\n"
" It defines two TAGs in each GZIP header, ZS: block size, ZX: random access index.\n"
" Program pgzf can decompress .pgzf and .gz files. When decompressing .gz files,\n"
" pgzf is in fact a buffered gzip reader. Also, .pgzf files can be decompressed\n"
" by program gzip.\n"
"\n"
"In plan to support random access\n"
);
return ret;
}
int main(int argc, char **argv){
PGZF *pz;
char *outf, *ftag;
FILE *in, *out;
void *buff;
u4i bufsize, nbyte;
int c, rw, ncpu, level, overwrite, del, is_dir;
rw = PGZF_MODE_W;
ncpu = 8;
bufsize = PGZF_DEFAULT_BUFF_SIZE;
level = 6;
overwrite = 0;
del = 0;
outf = NULL;
while((c = getopt(argc, argv, "hdxft:b:l:o:V")) != -1){
switch(c){
case 'h': return usage(0);
case 'd': rw = PGZF_MODE_R; break;
case 't': ncpu = atoi(optarg); break;
case 'b': bufsize = (atol(optarg) << 20); break;
case 'l': level = atoi(optarg); break;
case 'f': overwrite = 1; break;
case 'o': outf = optarg; break;
case 'x': del = 1; break;
case 'V': fprintf(stdout, "pgzf 1.1\n"); return 0;
default: return usage(1);
}
}
if(optind == argc){
return usage(1);
}
if(0 && del){
if(outf == NULL && overwrite == 0){
if(optind < argc){
fprintf(stderr, " ** WARNNING: won't delete input files. To force delete input files, please specify -o or/and -f\n");
}
del = 0;
}
}
is_dir = 0;
out = NULL;
if(outf){
if(file_exists(outf)){
if(overwrite == 0){
fprintf(stderr, " ** ERROR: '%s' exists\n", outf);
return 1;
} else {
for(c=optind;c<argc;c++){
if(strcmp(outf, argv[c]) == 0){
fprintf(stderr, " ** ERROR: The same file in INPUT and OUTPUT, '%s'\n", outf);
return 1;
}
}
}
out = open_file_for_write(outf, NULL, overwrite);
} else if(dir_exists(outf)){
is_dir = 1;
} else {
out = open_file_for_write(outf, NULL, overwrite);
}
}
buff = malloc(bufsize);
if(rw == PGZF_MODE_R){
if(outf == NULL || is_dir){
for(c=optind;c<argc;c++){
if(strlen(argv[c]) < 4 || strcasecmp(argv[c] + strlen(argv[c]) - 3, ".gz")){
fprintf(stderr, " ** ERROR: cannot auto generate output file name for '%s'\n", argv[c]);
return 1;
} else if(is_dir){
char *rtag;
rtag = relative_filename(argv[c]);
rtag[strlen(rtag) - 3] = 0;
ftag = malloc(strlen(outf) + 1 + strlen(rtag) + 1);
sprintf(ftag, "%s/%s", outf, rtag);
free(rtag);
if(overwrite == 0 && file_exists(ftag)){
fprintf(stderr, " ** ERROR: '%s' exists\n", ftag);
return 1;
}
free(ftag);
} else {
ftag = strdup(argv[optind]);
ftag[strlen(ftag) - 3] = 0;
if(overwrite == 0 && file_exists(ftag)){
fprintf(stderr, " ** ERROR: '%s' exists\n", ftag);
return 1;
}
free(ftag);
}
}
}
do {
in = open_file_for_read(argv[optind], NULL);
if(outf == NULL){
ftag = strdup(argv[optind]);
ftag[strlen(ftag) - 3] = 0;
out = open_file_for_write(ftag, NULL, overwrite);
free(ftag);
} else if(is_dir){
char *rtag;
rtag = relative_filename(argv[optind]);
rtag[strlen(rtag) - 3] = 0;
ftag = malloc(strlen(outf) + 1 + strlen(rtag) + 1);
sprintf(ftag, "%s/%s", outf, rtag);
free(rtag);
out = open_file_for_write(ftag, NULL, overwrite);
free(ftag);
}
pz = open_pgzf_reader(in, bufsize, ncpu);
while((nbyte = read_pgzf(pz, buff, bufsize))){
fwrite(buff, 1, nbyte, out);
}
if(pz->error){
fprintf(stderr, " ** ERROR: error code (%d)'\n", pz->error);
return 1;
}
close_pgzf(pz);
if(in != stdin){
fclose(in);
if(del){
unlink(argv[optind]);
}
}
optind ++;
if(outf == NULL || is_dir){
fclose(out);
}
} while(optind < argc);
} else {
if(outf && !is_dir){
pz = open_pgzf_writer(out, bufsize, ncpu, level);
} else {
pz = NULL;
for(c=optind;c<argc;c++){
if(strlen(argv[c]) >= 4 && strcasecmp(argv[c] + strlen(argv[c]) - 3, ".gz") == 0){
fprintf(stderr, " ** ERROR: file seems already compressed '%s'\n", argv[c]);
return 1;
} else if(strcmp(argv[c], "-") == 0){
fprintf(stderr, " ** ERROR: Please specify output file when read from STDIN '%s'\n", argv[c]);
return 1;
} else if(is_dir){
char *rtag;
rtag = relative_filename(argv[c]);
ftag = malloc(strlen(outf) + 1 + strlen(rtag) + 3 + 1);
sprintf(ftag, "%s/%s.gz", outf, rtag);
free(rtag);
if(overwrite == 0 && file_exists(ftag)){
fprintf(stderr, " ** ERROR: '%s' exists\n", ftag);
return 1;
}
free(ftag);
} else {
ftag = malloc(strlen(argv[c]) + 4);
sprintf(ftag, "%s.gz", argv[c]);
if(overwrite == 0 && file_exists(ftag)){
fprintf(stderr, " ** ERROR: '%s' exists\n", ftag);
return 1;
}
free(ftag);
}
}
}
do {
if(outf == NULL){
ftag = malloc(strlen(argv[optind]) + 4);
sprintf(ftag, "%s.gz", argv[optind]);
out = open_file_for_write(ftag, NULL, overwrite);
pz = open_pgzf_writer(out, bufsize, ncpu, level);
free(ftag);
} else if(is_dir){
char *rtag;
rtag = relative_filename(argv[optind]);
ftag = malloc(strlen(outf) + 1 + strlen(rtag) + 3 + 1);
sprintf(ftag, "%s/%s.gz", outf, rtag);
free(rtag);
out = open_file_for_write(ftag, NULL, overwrite);
pz = open_pgzf_writer(out, bufsize, ncpu, level);
free(ftag);
}
in = open_file_for_read(argv[optind], NULL);
while((nbyte = fread(buff, 1, bufsize, in))){
write_pgzf(pz, buff, nbyte);
}
if(in != stdin){
fclose(in);
if(del){
unlink(argv[optind]);
}
}
if(outf == NULL || is_dir){
close_pgzf(pz);
fclose(out);
}
optind ++;
} while(optind < argc);
if(outf && !is_dir){
close_pgzf(pz);
}
}
if(outf && !is_dir) fclose(out);
free(buff);
return 0;
}
|