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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF. The full HDF copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
** FILE
** hdfpack.c
** USAGE
** hdfpack [options] <infile> <outfile>
** DESCRIPTION
** This program will compact an HDF file by reading in all the data
** elements in the file and writing them out to a new file.
** Options are:
** -b Don't coalesce linked-block elements
** -i Interactive mode; prompt for each linked-block element
** -d# Force the output file to have # DDs per DD block
** -t# Force output file to have # linked blocks per table entry
** -x Keep external objects external
** -r <from> <to> rename external objects
** Only one of options b and i can be specified.
** COMMENTS, BUGS, ASSUMPTIONS
** Both arguments must be supplied to the program and they cannot be
** identical.
** You must have enough additional disk space for the compacted file.
*/
#include <stdio.h>
#include <string.h>
#include "hdf.h"
#include "herr.h"
#include "hfile_priv.h"
typedef struct mydd_t {
uint16 tag;
uint16 ref;
int32 offset;
int32 length;
int16 special;
} mydd_t;
/* Static function prototypes */
int promptblocks(mydd_t *dd);
void copy_blocks(mydd_t *dd, int32 infile, int32 outfile);
void merge_blocks(mydd_t *dd, int32 infile, int32 outfile);
int main(int, char *a[]);
static void usage(char *);
static void hdferror(void);
static void error(const char *);
int desc_comp(const void *d1, const void *d2);
unsigned char *data;
char invoke[81];
int32 data_size;
int32 nblk = 0;
char *from_file = NULL;
char *to_file = NULL;
int
main(int argc, char *argv[])
{
int i, num_desc, fnum, merge;
int32 infile, outfile, aid, ret;
mydd_t *dlist;
int32 oldoff, oldlen;
int blocks = 1;
int intr = 0;
int16 ndds = 0;
int optset = 0;
intn external = FALSE;
char *tmp, fname[2][80];
/*
** Get invocation name of program
*/
tmp = (char *)NULL;
strcpy(invoke, strtok(argv[0], "/]\\\0"));
for (;;) {
if (tmp != NULL)
strcpy(invoke, tmp);
if ((tmp = strtok((char *)NULL, "/]\\\0")) == NULL)
break;
}
if (argc < 3) {
usage(invoke);
exit(1);
}
/*
** Get option flags
*/
fnum = 0;
i = 1;
while (i < argc) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'b':
if (optset == 0) {
blocks = 0;
optset = 1;
}
else {
error("incompatible options: -i and -b");
}
break;
case 'i':
if (optset == 0) {
intr = 1;
blocks = 0;
optset = 1;
}
else {
error("incompatible options: -i and -b");
}
break;
case 'd':
ndds = (int16)atoi(&argv[i][2]);
break;
case 't':
nblk = atoi(&argv[i][2]);
break;
case 'x':
external = TRUE;
break;
case 'r':
from_file = argv[++i];
to_file = argv[++i];
break;
default:
fprintf(stderr, "Unknown option -%c ignored\n", argv[i][1]);
break;
}
}
else {
if (fnum < 2) {
strcpy(fname[fnum], argv[i]);
fnum++;
}
}
i++;
}
/*
** Enough [unique] file arguments?
*/
if ((fnum != 2) || (strcmp(fname[0], fname[1]) == 0)) {
error("need 2 unique file names");
}
if (from_file && to_file)
printf("Going to rename external file %s to %s\n", from_file, to_file);
/*
** Check to make sure input file is HDF
*/
ret = (int)Hishdf(fname[0]);
if (ret == FALSE)
hdferror();
/*
** Open input and output files
*/
infile = Hopen(fname[0], DFACC_READ, 0);
if (infile == FAIL)
hdferror();
outfile = Hopen(fname[1], DFACC_CREATE, ndds);
if (outfile == FAIL)
hdferror();
/*
** See how many data elements there
** are and make room for their DD's
*/
num_desc = (int)Hnumber(infile, DFTAG_WILDCARD);
if (num_desc == FAIL)
hdferror();
dlist = (mydd_t *)malloc(num_desc * sizeof(*dlist));
if (dlist == NULL)
error("\tWow! That file must be HUGE!\n\tThere isn't enough memory to hold the DD's.\n");
/*
** Allocate data buffer - try 1 Meg first, work down
*/
data_size = 1048576; /* 1 MB */
data = NULL;
while ((data = (unsigned char *)malloc(data_size)) == NULL)
data_size /= 2; /* okay then, cut request by half */
/*
** Get all DD's for data elements
*/
aid = Hstartread(infile, DFTAG_WILDCARD, DFREF_WILDCARD);
if (aid == FAIL) {
printf("MAJOR PROBLEM: Hstartread for DD's; line %d\n", __LINE__);
hdferror();
}
for (i = 0; i < num_desc; i++) {
/*
* Get data about the current one
*/
Hinquire(aid, NULL, &dlist[i].tag, &dlist[i].ref, &dlist[i].length, &dlist[i].offset, NULL, NULL,
&dlist[i].special);
/*
* Move to the next one
*/
ret = Hnextread(aid, DFTAG_WILDCARD, DFREF_WILDCARD, DF_CURRENT);
/*
* Fail if there are none left and we expect more
*/
if ((ret == FAIL) && (i + 1 < num_desc)) {
printf("MAJOR PROBLEM: DDs; only got %d of %d; line %d\n", i, num_desc, __LINE__);
hdferror();
}
}
/*
* Close the access element
*/
ret = Hendaccess(aid);
if (ret == FAIL)
hdferror();
/*
** Sort DD's by offset to make it easy to
** find DD's that point to the same data
*/
qsort((char *)dlist, (size_t)num_desc, sizeof(*dlist), desc_comp);
oldoff = -2;
oldlen = -2;
for (i = 0; i < num_desc; i++) {
if (((dlist[i].tag != DFTAG_NULL) && (dlist[i].tag != DFTAG_VERSION)) &&
(dlist[i].tag != DFTAG_LINKED)) {
if ((dlist[i].offset != oldoff) || (dlist[i].length != oldlen)) {
/*
** if this DD points to different data, read the data
** from the old file and write it to the new file
*/
switch (dlist[i].special) {
case SPECIAL_LINKED:
if (intr == 1)
merge = promptblocks(&dlist[i]);
else
merge = blocks;
if (merge == 0) {
copy_blocks(&dlist[i], infile, outfile);
}
else {
merge_blocks(&dlist[i], infile, outfile);
}
break;
case SPECIAL_EXT:
if (external) {
sp_info_block_t info;
int32 _aid, new_aid;
char *name;
/* get file name and offset */
_aid = Hstartread(infile, dlist[i].tag, dlist[i].ref);
if (_aid == FAIL)
continue;
ret = HDget_special_info(_aid, &info);
if ((ret == FAIL) || (info.key != SPECIAL_EXT))
continue;
/* see if should be renamed */
if (from_file && !strcmp(info.path, from_file))
name = to_file;
else
name = info.path;
/* create the new one */
new_aid = HXcreate(outfile, BASETAG(dlist[i].tag), dlist[i].ref, name,
info.offset, dlist[i].length);
/* close the elements */
Hendaccess(_aid);
Hendaccess(new_aid);
}
else {
/* pull into the file */
merge_blocks(&dlist[i], infile, outfile);
}
break;
case SPECIAL_COMP: /* This code assumes that you'd like to leave the compressed data that
way and not expand it */
{
int32 _aid, len;
void **buf;
/* Read in old compressed data description */
if ((_aid = Hstartaccess(infile, dlist[i].tag, dlist[i].ref, DFACC_READ)) == FAIL)
continue;
HQuerylength(aid, &len);
buf = malloc(len);
Hread(_aid, len, buf);
Hendaccess(_aid);
/* Write compressed data description into new file */
if ((_aid = Hstartaccess(outfile, dlist[i].tag, dlist[i].ref, DFACC_WRITE)) == FAIL)
continue;
Hwrite(_aid, len, buf);
Hendaccess(_aid);
free(buf);
} break;
default:
merge_blocks(&dlist[i], infile, outfile);
break;
} /* switch (special) */
}
else {
/*
** otherwise, just make a new DD for same data
*/
ret = Hdupdd(outfile, dlist[i].tag, dlist[i].ref, dlist[i - 1].tag, dlist[i - 1].ref);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
}
}
/*
** save offset of data to check against next DD
*/
oldoff = dlist[i].offset;
}
free(data);
free(dlist);
/*
** done; close files
*/
Hclose(infile);
Hclose(outfile);
return (0);
}
/*
** NAME
** promptblocks
*/
int
promptblocks(mydd_t *dd)
{
char ans[80];
printf("Would you like to coalesce the following linked-block element:\n");
printf("\ttag = %d\n\tref = %d\n(y/n): ", dd->tag, dd->ref);
fgets(ans, 80, stdin);
if ((ans[0] == 'y') || (ans[0] == 'Y'))
return (1);
else
return (0);
}
/*
** NAME
** copy_blocks -- move a linked-block element; preserve blocking
*/
void
copy_blocks(mydd_t *dd, int32 infile, int32 outfile)
{
int32 inaid, ret, rdret, outaid;
sp_info_block_t info;
inaid = Hstartread(infile, dd->tag, dd->ref);
ret = HDget_special_info(inaid, &info);
if ((ret != SUCCEED) || (info.key != SPECIAL_LINKED)) {
HERROR(DFE_GENAPP);
hdferror();
}
/*
** copy first block
*/
outaid = Hstartwrite(outfile, BASETAG(dd->tag), dd->ref, info.first_len);
if (outaid == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
rdret = 0;
while (rdret < info.first_len) {
ret = Hread(inaid, (data_size < info.first_len) ? data_size : info.first_len, data);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
rdret += ret;
ret = Hwrite(outaid, ret, data);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
}
ret = Hendaccess(outaid);
/*
** promote to linked-block element
*/
if (nblk > 0)
info.nblocks = nblk;
outaid = HLcreate(outfile, BASETAG(dd->tag), dd->ref, info.block_len, info.nblocks);
if (outaid == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
/*
** copy remaining blocks
*/
rdret = data_size;
while (rdret == data_size) {
rdret = Hread(inaid, data_size, data);
if (rdret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
if (rdret != 0) {
ret = Hwrite(outaid, rdret, data);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
}
}
Hendaccess(outaid);
Hendaccess(inaid);
}
/*
** NAME
** merge_blocks
*/
void
merge_blocks(mydd_t *dd, int32 infile, int32 outfile)
{
int32 inaid, outaid, ret, len;
inaid = Hstartread(infile, dd->tag, dd->ref);
if (inaid == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
outaid = Hstartaccess(outfile, BASETAG(dd->tag), dd->ref, DFACC_WRITE);
if (outaid == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
while (dd->length > 0) {
dd->length -= (len = Hread(inaid, data_size, data));
if (len == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
ret = Hwrite(outaid, len, data);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
}
ret = Hendaccess(inaid);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
ret = Hendaccess(outaid);
if (ret == FAIL) {
HERROR(DFE_GENAPP);
hdferror();
}
}
/*
** NAME
** usage -- print out usage template
** USAGE
** int usage()
** RETURNS
** none
** DESCRIPTION
** Print hdfpack's usage template to stdout.
** GLOBAL VARIABLES
** COMMENTS, BUGS, ASSUMPTIONS
** EXAMPLES
*/
static void
usage(char *name)
{
fprintf(stderr, "Usage: %s [-i | -b] [-d#] [-t#] [-x] [-r <from> <to>] <infile> <outfile>\n", name);
fprintf(stderr, "\t-t# : reset the linked block sizing\n");
fprintf(stderr, "\t-d# : reset the number of DDs per DD block\n");
fprintf(stderr, "\t-i : interactively merge linked blocks\n");
fprintf(stderr, "\t-b : maintain linked blocking sizes\n");
fprintf(stderr, "\t-x : keep external elements external\n");
fprintf(stderr, "\t-r <from> <to> :\n");
fprintf(stderr, "\t change external element names matching <from> to <to>\n");
}
/*
** NAME
** hdferror -- print out HDF error number
** USAGE
** int hdferror()
** RETURNS
** none
** DESCRIPTION
** Print an HDF error number to stderr.
** GLOBAL VARIABLES
** COMMENTS, BUGS, ASSUMPTIONS
** This routine terminates the program with code 1.
** EXAMPLES
*/
static void
hdferror(void)
{
HEprint(stderr, 0);
exit(1);
}
/*
** NAME
** error -- print error to stderr
** USAGE
** int error(string);
** char *string; IN: pointer to error description string
** RETURNS
** none
** DESCRIPTION
** Print an HDF error number to stderr.
** GLOBAL VARIABLES
** COMMENTS, BUGS, ASSUMPTIONS
** This routine terminates the program with code 1.
** EXAMPLES
*/
static void
error(const char *string)
{
fprintf(stderr, "%s: %s\n", invoke, string);
exit(1);
}
/*
** NAME
** desc_comp -- compare two DD's by offset
** USAGE
** int desc_cmp(d1, d2)
** mydd_t *d1 IN: pointer to a DD
** mydd_t *d2 IN: pointer to a DD
** RETURNS
** A number less than, greater than, or equal to 0, according to
** whether d1's offset is less than, greater than, or equal to
** d2's offset.
** DESCRIPTION
** A pointer to this routine is passed to qsort() as the comparison
** function for the sort.
** GLOBAL VARIABLES
** COMMENTS, BUGS, ASSUMPTIONS
** EXAMPLES
*/
int
desc_comp(const void *d1, const void *d2)
{
return ((int)(((const mydd_t *)d1)->offset - ((const mydd_t *)d2)->offset));
}
|