Fossil SCM
Preserve the basis-file suffix when generating temporary file names.
Commit
a072be1eafe487c650d416b013752efd4be4d92a46be5244253a4980ceffccf1
Parent
63febeb7f4229af…
2 files changed
+1
-6
+28
-3
+1
-6
| --- src/diffcmd.c | ||
| +++ src/diffcmd.c | ||
| @@ -213,11 +213,10 @@ | ||
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /* Release memory resources */ |
| 216 | 216 | blob_reset(&file2); |
| 217 | 217 | }else{ |
| 218 | - int cnt = 0; | |
| 219 | 218 | Blob nameFile1; /* Name of temporary file to old pFile1 content */ |
| 220 | 219 | Blob cmd; /* Text of command to run */ |
| 221 | 220 | |
| 222 | 221 | if( !fIncludeBinary ){ |
| 223 | 222 | Blob file2; |
| @@ -246,15 +245,11 @@ | ||
| 246 | 245 | blob_reset(&file2); |
| 247 | 246 | } |
| 248 | 247 | |
| 249 | 248 | /* Construct a temporary file to hold pFile1 based on the name of |
| 250 | 249 | ** zFile2 */ |
| 251 | - blob_zero(&nameFile1); | |
| 252 | - do{ | |
| 253 | - blob_reset(&nameFile1); | |
| 254 | - blob_appendf(&nameFile1, "%s~%d", zFile2, cnt++); | |
| 255 | - }while( file_access(blob_str(&nameFile1),F_OK)==0 ); | |
| 250 | + file_tempname(&nameFile1, zFile2); | |
| 256 | 251 | blob_write_to_file(pFile1, blob_str(&nameFile1)); |
| 257 | 252 | |
| 258 | 253 | /* Construct the external diff command */ |
| 259 | 254 | blob_zero(&cmd); |
| 260 | 255 | blob_append(&cmd, zDiffCmd, -1); |
| 261 | 256 |
| --- src/diffcmd.c | |
| +++ src/diffcmd.c | |
| @@ -213,11 +213,10 @@ | |
| 213 | } |
| 214 | |
| 215 | /* Release memory resources */ |
| 216 | blob_reset(&file2); |
| 217 | }else{ |
| 218 | int cnt = 0; |
| 219 | Blob nameFile1; /* Name of temporary file to old pFile1 content */ |
| 220 | Blob cmd; /* Text of command to run */ |
| 221 | |
| 222 | if( !fIncludeBinary ){ |
| 223 | Blob file2; |
| @@ -246,15 +245,11 @@ | |
| 246 | blob_reset(&file2); |
| 247 | } |
| 248 | |
| 249 | /* Construct a temporary file to hold pFile1 based on the name of |
| 250 | ** zFile2 */ |
| 251 | blob_zero(&nameFile1); |
| 252 | do{ |
| 253 | blob_reset(&nameFile1); |
| 254 | blob_appendf(&nameFile1, "%s~%d", zFile2, cnt++); |
| 255 | }while( file_access(blob_str(&nameFile1),F_OK)==0 ); |
| 256 | blob_write_to_file(pFile1, blob_str(&nameFile1)); |
| 257 | |
| 258 | /* Construct the external diff command */ |
| 259 | blob_zero(&cmd); |
| 260 | blob_append(&cmd, zDiffCmd, -1); |
| 261 |
| --- src/diffcmd.c | |
| +++ src/diffcmd.c | |
| @@ -213,11 +213,10 @@ | |
| 213 | } |
| 214 | |
| 215 | /* Release memory resources */ |
| 216 | blob_reset(&file2); |
| 217 | }else{ |
| 218 | Blob nameFile1; /* Name of temporary file to old pFile1 content */ |
| 219 | Blob cmd; /* Text of command to run */ |
| 220 | |
| 221 | if( !fIncludeBinary ){ |
| 222 | Blob file2; |
| @@ -246,15 +245,11 @@ | |
| 245 | blob_reset(&file2); |
| 246 | } |
| 247 | |
| 248 | /* Construct a temporary file to hold pFile1 based on the name of |
| 249 | ** zFile2 */ |
| 250 | file_tempname(&nameFile1, zFile2); |
| 251 | blob_write_to_file(pFile1, blob_str(&nameFile1)); |
| 252 | |
| 253 | /* Construct the external diff command */ |
| 254 | blob_zero(&cmd); |
| 255 | blob_append(&cmd, zDiffCmd, -1); |
| 256 |
+28
-3
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -1459,13 +1459,16 @@ | ||
| 1459 | 1459 | blob_set(pPath, "/"); |
| 1460 | 1460 | } |
| 1461 | 1461 | } |
| 1462 | 1462 | |
| 1463 | 1463 | /* |
| 1464 | -** Construct a random temporary filename into pBuf starting with zPrefix. | |
| 1464 | +** Construct a random temporary filename into pBuf where the name of | |
| 1465 | +** the temporary file is derived from zBasis. The suffix on the temp | |
| 1466 | +** file is the same as the suffix on zBasis, and the temp file has | |
| 1467 | +** the root of zBasis in its name. | |
| 1465 | 1468 | */ |
| 1466 | -void file_tempname(Blob *pBuf, const char *zPrefix){ | |
| 1469 | +void file_tempname(Blob *pBuf, const char *zBasis){ | |
| 1467 | 1470 | #if defined(_WIN32) |
| 1468 | 1471 | const char *azDirs[] = { |
| 1469 | 1472 | 0, /* GetTempPath */ |
| 1470 | 1473 | 0, /* TEMP */ |
| 1471 | 1474 | 0, /* TMP */ |
| @@ -1488,10 +1491,12 @@ | ||
| 1488 | 1491 | "0123456789"; |
| 1489 | 1492 | unsigned int i; |
| 1490 | 1493 | const char *zDir = "."; |
| 1491 | 1494 | int cnt = 0; |
| 1492 | 1495 | char zRand[16]; |
| 1496 | + int nBasis; | |
| 1497 | + const char *zSuffix; | |
| 1493 | 1498 | |
| 1494 | 1499 | #if defined(_WIN32) |
| 1495 | 1500 | wchar_t zTmpPath[MAX_PATH]; |
| 1496 | 1501 | |
| 1497 | 1502 | if( GetTempPathW(MAX_PATH, zTmpPath) ){ |
| @@ -1513,19 +1518,39 @@ | ||
| 1513 | 1518 | if( !file_isdir(azDirs[i], ExtFILE) ) continue; |
| 1514 | 1519 | zDir = azDirs[i]; |
| 1515 | 1520 | break; |
| 1516 | 1521 | } |
| 1517 | 1522 | |
| 1523 | + assert( zBasis!=0 ); | |
| 1524 | + zSuffix = 0; | |
| 1525 | + for(i=0; zBasis[i]; i++){ | |
| 1526 | + if( zBasis[i]=='/' || zBasis[i]=='\\' ){ | |
| 1527 | + zBasis += i+1; | |
| 1528 | + i = -1; | |
| 1529 | + }else if( zBasis[i]=='.' ){ | |
| 1530 | + zSuffix = zBasis + i; | |
| 1531 | + } | |
| 1532 | + } | |
| 1533 | + if( zSuffix==0 || zSuffix<=zBasis ){ | |
| 1534 | + zSuffix = ""; | |
| 1535 | + nBasis = i; | |
| 1536 | + }else{ | |
| 1537 | + nBasis = (int)(zSuffix - zBasis); | |
| 1538 | + } | |
| 1539 | + if( nBasis==0 ){ | |
| 1540 | + nBasis = 6; | |
| 1541 | + zBasis = "fossil"; | |
| 1542 | + } | |
| 1518 | 1543 | do{ |
| 1519 | 1544 | blob_zero(pBuf); |
| 1520 | 1545 | if( cnt++>20 ) fossil_panic("cannot generate a temporary filename"); |
| 1521 | 1546 | sqlite3_randomness(15, zRand); |
| 1522 | 1547 | for(i=0; i<15; i++){ |
| 1523 | 1548 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1524 | 1549 | } |
| 1525 | 1550 | zRand[15] = 0; |
| 1526 | - blob_appendf(pBuf, "%s/%s-%s.txt", zDir, zPrefix ? zPrefix : "", zRand); | |
| 1551 | + blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zRand, zSuffix); | |
| 1527 | 1552 | }while( file_size(blob_str(pBuf), ExtFILE)>=0 ); |
| 1528 | 1553 | |
| 1529 | 1554 | #if defined(_WIN32) |
| 1530 | 1555 | fossil_path_free((char *)azDirs[0]); |
| 1531 | 1556 | fossil_path_free((char *)azDirs[1]); |
| 1532 | 1557 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1459,13 +1459,16 @@ | |
| 1459 | blob_set(pPath, "/"); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | ** Construct a random temporary filename into pBuf starting with zPrefix. |
| 1465 | */ |
| 1466 | void file_tempname(Blob *pBuf, const char *zPrefix){ |
| 1467 | #if defined(_WIN32) |
| 1468 | const char *azDirs[] = { |
| 1469 | 0, /* GetTempPath */ |
| 1470 | 0, /* TEMP */ |
| 1471 | 0, /* TMP */ |
| @@ -1488,10 +1491,12 @@ | |
| 1488 | "0123456789"; |
| 1489 | unsigned int i; |
| 1490 | const char *zDir = "."; |
| 1491 | int cnt = 0; |
| 1492 | char zRand[16]; |
| 1493 | |
| 1494 | #if defined(_WIN32) |
| 1495 | wchar_t zTmpPath[MAX_PATH]; |
| 1496 | |
| 1497 | if( GetTempPathW(MAX_PATH, zTmpPath) ){ |
| @@ -1513,19 +1518,39 @@ | |
| 1513 | if( !file_isdir(azDirs[i], ExtFILE) ) continue; |
| 1514 | zDir = azDirs[i]; |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | do{ |
| 1519 | blob_zero(pBuf); |
| 1520 | if( cnt++>20 ) fossil_panic("cannot generate a temporary filename"); |
| 1521 | sqlite3_randomness(15, zRand); |
| 1522 | for(i=0; i<15; i++){ |
| 1523 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1524 | } |
| 1525 | zRand[15] = 0; |
| 1526 | blob_appendf(pBuf, "%s/%s-%s.txt", zDir, zPrefix ? zPrefix : "", zRand); |
| 1527 | }while( file_size(blob_str(pBuf), ExtFILE)>=0 ); |
| 1528 | |
| 1529 | #if defined(_WIN32) |
| 1530 | fossil_path_free((char *)azDirs[0]); |
| 1531 | fossil_path_free((char *)azDirs[1]); |
| 1532 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1459,13 +1459,16 @@ | |
| 1459 | blob_set(pPath, "/"); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | ** Construct a random temporary filename into pBuf where the name of |
| 1465 | ** the temporary file is derived from zBasis. The suffix on the temp |
| 1466 | ** file is the same as the suffix on zBasis, and the temp file has |
| 1467 | ** the root of zBasis in its name. |
| 1468 | */ |
| 1469 | void file_tempname(Blob *pBuf, const char *zBasis){ |
| 1470 | #if defined(_WIN32) |
| 1471 | const char *azDirs[] = { |
| 1472 | 0, /* GetTempPath */ |
| 1473 | 0, /* TEMP */ |
| 1474 | 0, /* TMP */ |
| @@ -1488,10 +1491,12 @@ | |
| 1491 | "0123456789"; |
| 1492 | unsigned int i; |
| 1493 | const char *zDir = "."; |
| 1494 | int cnt = 0; |
| 1495 | char zRand[16]; |
| 1496 | int nBasis; |
| 1497 | const char *zSuffix; |
| 1498 | |
| 1499 | #if defined(_WIN32) |
| 1500 | wchar_t zTmpPath[MAX_PATH]; |
| 1501 | |
| 1502 | if( GetTempPathW(MAX_PATH, zTmpPath) ){ |
| @@ -1513,19 +1518,39 @@ | |
| 1518 | if( !file_isdir(azDirs[i], ExtFILE) ) continue; |
| 1519 | zDir = azDirs[i]; |
| 1520 | break; |
| 1521 | } |
| 1522 | |
| 1523 | assert( zBasis!=0 ); |
| 1524 | zSuffix = 0; |
| 1525 | for(i=0; zBasis[i]; i++){ |
| 1526 | if( zBasis[i]=='/' || zBasis[i]=='\\' ){ |
| 1527 | zBasis += i+1; |
| 1528 | i = -1; |
| 1529 | }else if( zBasis[i]=='.' ){ |
| 1530 | zSuffix = zBasis + i; |
| 1531 | } |
| 1532 | } |
| 1533 | if( zSuffix==0 || zSuffix<=zBasis ){ |
| 1534 | zSuffix = ""; |
| 1535 | nBasis = i; |
| 1536 | }else{ |
| 1537 | nBasis = (int)(zSuffix - zBasis); |
| 1538 | } |
| 1539 | if( nBasis==0 ){ |
| 1540 | nBasis = 6; |
| 1541 | zBasis = "fossil"; |
| 1542 | } |
| 1543 | do{ |
| 1544 | blob_zero(pBuf); |
| 1545 | if( cnt++>20 ) fossil_panic("cannot generate a temporary filename"); |
| 1546 | sqlite3_randomness(15, zRand); |
| 1547 | for(i=0; i<15; i++){ |
| 1548 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1549 | } |
| 1550 | zRand[15] = 0; |
| 1551 | blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zRand, zSuffix); |
| 1552 | }while( file_size(blob_str(pBuf), ExtFILE)>=0 ); |
| 1553 | |
| 1554 | #if defined(_WIN32) |
| 1555 | fossil_path_free((char *)azDirs[0]); |
| 1556 | fossil_path_free((char *)azDirs[1]); |
| 1557 |