Fossil SCM

Preserve the basis-file suffix when generating temporary file names.

drh 2019-04-24 12:59 trunk
Commit a072be1eafe487c650d416b013752efd4be4d92a46be5244253a4980ceffccf1
2 files changed +1 -6 +28 -3
+1 -6
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -213,11 +213,10 @@
213213
}
214214
215215
/* Release memory resources */
216216
blob_reset(&file2);
217217
}else{
218
- int cnt = 0;
219218
Blob nameFile1; /* Name of temporary file to old pFile1 content */
220219
Blob cmd; /* Text of command to run */
221220
222221
if( !fIncludeBinary ){
223222
Blob file2;
@@ -246,15 +245,11 @@
246245
blob_reset(&file2);
247246
}
248247
249248
/* Construct a temporary file to hold pFile1 based on the name of
250249
** 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);
256251
blob_write_to_file(pFile1, blob_str(&nameFile1));
257252
258253
/* Construct the external diff command */
259254
blob_zero(&cmd);
260255
blob_append(&cmd, zDiffCmd, -1);
261256
--- 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 @@
14591459
blob_set(pPath, "/");
14601460
}
14611461
}
14621462
14631463
/*
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.
14651468
*/
1466
-void file_tempname(Blob *pBuf, const char *zPrefix){
1469
+void file_tempname(Blob *pBuf, const char *zBasis){
14671470
#if defined(_WIN32)
14681471
const char *azDirs[] = {
14691472
0, /* GetTempPath */
14701473
0, /* TEMP */
14711474
0, /* TMP */
@@ -1488,10 +1491,12 @@
14881491
"0123456789";
14891492
unsigned int i;
14901493
const char *zDir = ".";
14911494
int cnt = 0;
14921495
char zRand[16];
1496
+ int nBasis;
1497
+ const char *zSuffix;
14931498
14941499
#if defined(_WIN32)
14951500
wchar_t zTmpPath[MAX_PATH];
14961501
14971502
if( GetTempPathW(MAX_PATH, zTmpPath) ){
@@ -1513,19 +1518,39 @@
15131518
if( !file_isdir(azDirs[i], ExtFILE) ) continue;
15141519
zDir = azDirs[i];
15151520
break;
15161521
}
15171522
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
+ }
15181543
do{
15191544
blob_zero(pBuf);
15201545
if( cnt++>20 ) fossil_panic("cannot generate a temporary filename");
15211546
sqlite3_randomness(15, zRand);
15221547
for(i=0; i<15; i++){
15231548
zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ];
15241549
}
15251550
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);
15271552
}while( file_size(blob_str(pBuf), ExtFILE)>=0 );
15281553
15291554
#if defined(_WIN32)
15301555
fossil_path_free((char *)azDirs[0]);
15311556
fossil_path_free((char *)azDirs[1]);
15321557
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button