Changeset 1003 in kBuild
- Timestamp:
- Jun 2, 2007 11:54:36 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kObjCache/kObjCache.c
r1002 r1003 57 57 #include "md5.h" 58 58 59 /******************************************************************************* 60 * Defined Constants And Macros * 61 *******************************************************************************/ 62 /** The max line length in a cache file. */ 63 #define KOBJCACHE_MAX_LINE_LEN 16384 64 59 65 60 66 /******************************************************************************* … … 108 114 size_t cbOldCppMapping; 109 115 110 /** The raw cache file buffer.111 * The const members below points into this. */112 char *pszRawFile;113 116 /** The head of the checksum list. */ 114 117 KOCSUM SumHead; 115 118 /** The object filename (relative to the cache file). */ 116 c onst char *pszObjName;119 char *pszObjName; 117 120 /** The compile argument vector used to build the object. */ 118 c onst char **papszArgvCompile;121 char **papszArgvCompile; 119 122 /** The size of the compile */ 120 123 unsigned cArgvCompile; … … 148 151 149 152 150 /* crc.c */151 extern uint32_t crc32(uint32_t, const void *, size_t);152 153 154 153 /** 155 154 * Compares two check sum entries. … … 164 163 if (pSum1 == pSum2) 165 164 return 1; 166 if ( pSum1 || !pSum2)165 if (!pSum1 || !pSum2) 167 166 return 0; 168 167 if (pSum1->crc32 != pSum2->crc32) … … 273 272 static void kObjCacheRead(PKOBJCACHE pEntry) 274 273 { 275 kObjCacheVerbose(pEntry, "reading cache file...\n"); 276 pEntry->fNeedCompiling = 1; 274 static char s_szLine[KOBJCACHE_MAX_LINE_LEN + 16]; 275 FILE *pFile; 276 pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "rb"); 277 if (pFile) 278 { 279 kObjCacheVerbose(pEntry, "reading cache file...\n"); 280 281 /* 282 * Check the magic. 283 */ 284 if ( !fgets(s_szLine, sizeof(s_szLine), pFile) 285 || strcmp(s_szLine, "magic=kObjCache-1\n")) 286 { 287 kObjCacheVerbose(pEntry, "bad cache file (magic)\n"); 288 pEntry->fNeedCompiling = 1; 289 } 290 else 291 { 292 /* 293 * Parse the rest of the file (relaxed order). 294 */ 295 unsigned i; 296 int fBad = 0; 297 int fBadBeforeMissing; 298 int fFirstSum = 1; 299 while (fgets(s_szLine, sizeof(s_szLine), pFile)) 300 { 301 /* Split the line and drop the trailing newline. */ 302 char *pszNl = strchr(s_szLine, '\n'); 303 char *pszVal = strchr(s_szLine, '='); 304 if ((fBad = pszVal == NULL)) 305 break; 306 if (pszNl) 307 *pszNl = '\0'; 308 *pszVal++ = '\0'; 309 310 /* string case on variable name */ 311 if (!strcmp(s_szLine, "obj")) 312 { 313 if ((fBad = pEntry->pszObjName != NULL)) 314 break; 315 pEntry->pszObjName = xstrdup(pszVal); 316 } 317 else if (!strcmp(s_szLine, "cpp")) 318 { 319 if ((fBad = pEntry->pszOldCppName != NULL)) 320 break; 321 pEntry->pszOldCppName = xstrdup(pszVal); 322 } 323 else if (!strcmp(s_szLine, "cc-argc")) 324 { 325 if ((fBad = pEntry->papszArgvCompile != NULL)) 326 break; 327 pEntry->cArgvCompile = atoi(pszVal); /* if wrong, we'll fail below. */ 328 pEntry->papszArgvCompile = xmalloc((pEntry->cArgvCompile + 1) * sizeof(pEntry->papszArgvCompile[0])); 329 memset(pEntry->papszArgvCompile, 0, (pEntry->cArgvCompile + 1) * sizeof(pEntry->papszArgvCompile[0])); 330 } 331 else if (!strncmp(s_szLine, "cc-argv-#", sizeof("cc-argv-#") - 1)) 332 { 333 char *pszNext; 334 unsigned i = strtoul(&s_szLine[sizeof("cc-argv-#") - 1], &pszNext, 0); 335 if ((fBad = i >= pEntry->cArgvCompile || pEntry->papszArgvCompile[i] || (pszNext && *pszNext))) 336 break; 337 pEntry->papszArgvCompile[i] = xstrdup(pszVal); 338 } 339 else if (!strcmp(s_szLine, "sum")) 340 { 341 KOCSUM Sum; 342 unsigned i; 343 char *pszNext; 344 char *pszMD5 = strchr(pszVal, ':'); 345 if ((fBad = pszMD5 == NULL)) 346 break; 347 *pszMD5++ = '\0'; 348 349 /* crc32 */ 350 Sum.crc32 = (uint32_t)strtoul(pszVal, &pszNext, 16); 351 if ((fBad = (pszNext && *pszNext))) 352 break; 353 354 /* md5 */ 355 for (i = 0; i < sizeof(Sum.md5) * 2; i++) 356 { 357 unsigned char ch = pszMD5[i]; 358 int x; 359 if (ch - '0' <= 9) 360 x = ch - '0'; 361 else if (ch - 'a' <= 5) 362 x = ch - 'a' + 10; 363 else if (ch - 'A' <= 5) 364 x = ch - 'A' + 10; 365 else 366 { 367 fBad = 1; 368 break; 369 } 370 if (!(i & 1)) 371 Sum.md5[i >> 1] = x << 4; 372 else 373 Sum.md5[i >> 1] |= x; 374 } 375 if (fBad) 376 break; 377 378 if (fFirstSum) 379 { 380 pEntry->SumHead = Sum; 381 pEntry->SumHead.pNext = NULL; 382 fFirstSum = 0; 383 } 384 else 385 { 386 Sum.pNext = pEntry->SumHead.pNext; 387 pEntry->SumHead.pNext = xmalloc(sizeof(Sum)); 388 *pEntry->SumHead.pNext = Sum; 389 } 390 } 391 else 392 { 393 fBad = 1; 394 break; 395 } 396 } /* parse loop */ 397 398 /* 399 * Did we find everything? 400 */ 401 fBadBeforeMissing = fBad; 402 if ( !fBad 403 && ( !pEntry->papszArgvCompile 404 || !pEntry->pszObjName 405 || !pEntry->pszOldCppName 406 || fFirstSum)) 407 fBad = 1; 408 if (!fBad) 409 for (i = 0; i < pEntry->cArgvCompile; i++) 410 if ((fBad = !pEntry->papszArgvCompile[i])) 411 break; 412 if (fBad) 413 kObjCacheVerbose(pEntry, "bad cache file (%s)\n", fBadBeforeMissing ? s_szLine : "missing stuff"); 414 else if (ferror(pFile)) 415 kObjCacheVerbose(pEntry, "cache file read error\n"); 416 pEntry->fNeedCompiling = fBad; 417 } 418 fclose(pFile); 419 } 420 else 421 { 422 kObjCacheVerbose(pEntry, "no cache file\n"); 423 pEntry->fNeedCompiling = 1; 424 } 277 425 } 278 426 … … 285 433 static void kObjCacheWrite(PKOBJCACHE pEntry) 286 434 { 435 FILE *pFile; 436 PCKOCSUM pSum; 437 unsigned i; 438 287 439 kObjCacheVerbose(pEntry, "writing cache file...\n"); 440 pFile = FOpenFileInDir(pEntry->pszName, pEntry->pszDir, "wb"); 441 if (!pFile) 442 kObjCacheFatal(pEntry, "Failed to open '%s' in '%s': %s\n", 443 pEntry->pszName, pEntry->pszDir, strerror(errno)); 444 445 #define CHECK_LEN(expr) \ 446 do { int cch = expr; if (cch >= KOBJCACHE_MAX_LINE_LEN) kObjCacheFatal(pEntry, "Line too long: %d (max %d)\nexpr: %s\n", cch, KOBJCACHE_MAX_LINE_LEN, #expr); } while (0) 447 448 fprintf(pFile, "magic=kObjCache-1\n"); 449 CHECK_LEN(fprintf(pFile, "obj=%s\n", pEntry->pszNewObjName ? pEntry->pszNewObjName : pEntry->pszObjName)); 450 CHECK_LEN(fprintf(pFile, "cpp=%s\n", pEntry->pszNewCppName ? pEntry->pszNewCppName : pEntry->pszOldCppName)); 451 CHECK_LEN(fprintf(pFile, "cc-argc=%u\n", pEntry->cArgvCompile)); 452 for (i = 0; i < pEntry->cArgvCompile; i++) 453 CHECK_LEN(fprintf(pFile, "cc-argv-#%u=%s\n", i, pEntry->papszArgvCompile[i])); 454 for (pSum = pEntry->fNeedCompiling ? &pEntry->NewSum : &pEntry->SumHead; 455 pSum; 456 pSum = pSum->pNext) 457 fprintf(pFile, "sum=%#x:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", 458 pSum->crc32, 459 pSum->md5[0], pSum->md5[1], pSum->md5[2], pSum->md5[3], 460 pSum->md5[4], pSum->md5[5], pSum->md5[6], pSum->md5[7], 461 pSum->md5[8], pSum->md5[9], pSum->md5[10], pSum->md5[11], 462 pSum->md5[12], pSum->md5[13], pSum->md5[14], pSum->md5[15]); 288 463 464 if ( fflush(pFile) < 0 465 || ferror(pFile)) 466 { 467 int iErr = errno; 468 fclose(pFile); 469 UnlinkFileInDir(pEntry->pszName, pEntry->pszDir); 470 kObjCacheFatal(pEntry, "Stream error occured while writing '%s' in '%s': %d (?)\n", 471 pEntry->pszName, pEntry->pszDir, strerror(iErr)); 472 } 473 fclose(pFile); 289 474 } 290 475 … … 397 582 MD5Update(&MD5Ctx, pEntry->pszNewCppMapping, pEntry->cbNewCppMapping); 398 583 MD5Final(&pEntry->NewSum.md5[0], &MD5Ctx); 399 kObjCacheVerbose(pEntry, "crc32=%#lx md5=08x%08x%08x%08x\n", pEntry->NewSum.crc32, 400 ((uint32_t *)&pEntry->NewSum.md5[0])[0], 401 ((uint32_t *)&pEntry->NewSum.md5[0])[1], 402 ((uint32_t *)&pEntry->NewSum.md5[0])[2], 403 ((uint32_t *)&pEntry->NewSum.md5[0])[3]); 584 kObjCacheVerbose(pEntry, "crc32=%#lx md5=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", 585 pEntry->NewSum.crc32, 586 pEntry->NewSum.md5[0], pEntry->NewSum.md5[1], pEntry->NewSum.md5[2], pEntry->NewSum.md5[3], 587 pEntry->NewSum.md5[4], pEntry->NewSum.md5[5], pEntry->NewSum.md5[6], pEntry->NewSum.md5[7], 588 pEntry->NewSum.md5[8], pEntry->NewSum.md5[9], pEntry->NewSum.md5[10], pEntry->NewSum.md5[11], 589 pEntry->NewSum.md5[12], pEntry->NewSum.md5[13], pEntry->NewSum.md5[14], pEntry->NewSum.md5[15]); 590 404 591 } 405 592 … … 502 689 */ 503 690 kObjCacheVerbose(pEntry, "compiling -> '%s'...\n", pEntry->pszNewObjName); 504 pEntry->papszArgvCompile = papszArgvCompile;691 pEntry->papszArgvCompile = (char **)papszArgvCompile; /* LEAK */ 505 692 pEntry->cArgvCompile = cArgvCompile; 506 693 kObjCacheSpawn(pEntry, papszArgvCompile, cArgvCompile, "compile", NULL);
Note:
See TracChangeset
for help on using the changeset viewer.