Changeset 30607
- Timestamp:
- Feb 13, 2011, 11:16:23 AM (15 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/tools/src/roc.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/tools/src/roc.c
r30306 r30607 4 4 # include <regex.h> 5 5 6 // # define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); abort(); } } 7 # define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); exit(1); } } 6 # define myAssert(LOGIC,...) { if (!(LOGIC)) { fprintf (stderr, __VA_ARGS__); abort(); } } 8 7 9 8 # define ROC_HEADER_SIZE 0x1000 … … 15 14 int roc_insert (int argc, char **argv); 16 15 int roc_delete (int argc, char **argv); 16 int roc_validate (int argc, char **argv); 17 17 18 int usage (void); 18 19 int print_block (char *header, int *header_size, int *Noff, char *format,...); … … 29 30 if (!strcasecmp(argv[1], "-insert")) roc_insert (argc, argv); 30 31 if (!strcasecmp(argv[1], "-delete")) roc_delete (argc, argv); 32 if (!strcasecmp(argv[1], "-validate")) roc_validate (argc, argv); 31 33 32 34 usage(); … … 302 304 } 303 305 306 int roc_validate (int argc, char **argv) { 307 308 int i, j, n, Ninput, Nblocks, header_size, Nbytes, Nread; 309 off_t *sizes, *bytes_read; 310 char value; 311 char *header, *line, *ptr; 312 char **inputName, *targetName; 313 char **inputData; 314 char *targetData; 315 FILE **input, *target; 316 317 if (argc != 3) usage(); 318 319 320 /* find the md5 sum for each input file (ADD LATER) 321 * find the size of the input files 322 * define the output file size = MAX(sizes) 323 * create the target file header 324 */ 325 326 // the output file 327 targetName = argv[2]; 328 329 target = fopen(targetName, "r"); 330 myAssert (target, "failed to open roc datafile %s\n",targetName); 331 332 ALLOCATE (line, char, 1024); 333 334 scan_line (target, line); 335 myAssert (!strncmp(line, "ROC Version 0", strlen("ROC Verion 0")), "invalid ROC file or format in %s",targetName); 336 337 scan_line (target, line); 338 sscanf (line, "HEADER SIZE %x", &header_size); 339 340 ALLOCATE (header, char, header_size); 341 342 // read the full header 343 fseeko (target, 0, SEEK_SET); 344 Nbytes = fread (header, 1, header_size, target); 345 myAssert (Nbytes == header_size, "failed to read header data\n"); 346 347 ptr = header; 348 ptr = strchr (ptr, '\n'); ptr ++; // ROC Version 349 ptr = strchr (ptr, '\n'); ptr ++; // HEADER SIZE 350 351 sscanf (ptr, "N_FILES %d", &Ninput); ptr = strchr (ptr, '\n'); ptr ++; 352 sscanf (ptr, "N_BLOCKS %d", &Nblocks); ptr = strchr (ptr, '\n'); ptr ++; 353 354 // the input files 355 ALLOCATE (inputName, char *, Ninput); 356 ALLOCATE (sizes, off_t, Ninput); 357 ALLOCATE (bytes_read, off_t, Ninput); 358 for (i = 0; i < Ninput; i++) { 359 ALLOCATE (inputName[i], char, 1024); 360 sscanf (ptr, "%*s : %s", inputName[i]); ptr = strchr (ptr, '\n'); ptr ++; 361 ptr = strchr (ptr, '\n'); ptr ++; // BASE 362 sscanf (ptr, "%*s : "OFF_T_FMT, &sizes[i]); ptr = strchr (ptr, '\n'); ptr ++; 363 ptr = strchr (ptr, '\n'); ptr ++; // MD5 364 bytes_read[i] = 0; 365 } 366 367 ALLOCATE (input, FILE *, Ninput); 368 for (i = 0; i < Ninput; i++) { 369 input[i] = fopen(inputName[i], "r"); 370 myAssert (input[i], "failed to open file %s in %s\n",inputName[i],targetName); 371 } 372 373 ALLOCATE (inputData, char *, Ninput); 374 ALLOCATE (targetData, char, ROC_BLOCKSIZE); 375 for (i = 0; i < Ninput; i++) { 376 ALLOCATE (inputData[i], char, ROC_BLOCKSIZE); 377 } 378 379 for (n = 0; n < Nblocks; n++) { 380 for (i = 0; i < Ninput; i++) { 381 Nread = MIN (ROC_BLOCKSIZE, sizes[i] - bytes_read[i]); 382 Nbytes = fread (inputData[i], 1, Nread, input[i]); 383 myAssert (Nbytes == Nread, "failed to read data file: %s read: %d expect: %d prev_read: %d block: %d in %s\n",inputName[i],Nbytes,Nread,(int) bytes_read[i],n,targetName); 384 if (Nread < ROC_BLOCKSIZE) { 385 // if we have reached the end of the file, fill in the rest with NULLs 386 memset (&inputData[i][Nread], 0, ROC_BLOCKSIZE - Nread); 387 } 388 bytes_read[i] += Nread; 389 } 390 Nbytes = fread (targetData, 1, ROC_BLOCKSIZE, target); 391 myAssert (Nbytes == ROC_BLOCKSIZE, "failed to read roc file: %s read: %d expect %d\n", targetName, Nbytes, ROC_BLOCKSIZE); 392 393 for (j = 0; j < ROC_BLOCKSIZE; j++) { 394 value = targetData[j]; 395 for (i = 0; i < Ninput; i++) { 396 value = value ^ inputData[i][j]; 397 } 398 myAssert(value == 0, "validation failed on block %d and byte %d in %s\n",n,j,targetName); 399 } 400 401 } 402 403 for (i = 0; i < Ninput; i++) { 404 fclose(input[i]); 405 } 406 407 fclose (target); 408 409 fprintf (stderr, "validation: %s appears correct.\n",targetName); 410 exit (0); 411 } 412 304 413 int roc_insert (int argc, char **argv) { 305 414 … … 334 443 fprintf (stderr, " -create (target) (input) (input) (input) ... [options]\n"); 335 444 fprintf (stderr, " -repair (target) (file #) (output\n"); 445 fprintf (stderr, " -validate (target)\n"); 336 446 fprintf (stderr, " -insert (target) (input)\n"); 337 447 fprintf (stderr, " -delete (target) (file)\n"); 448 338 449 exit (2); 339 450 }
Note:
See TracChangeset
for help on using the changeset viewer.
