Fossil SCM
Do not overwrite files on disk, and especially do not prompt the user for permission to overwrite, if there would ultimately be no change in the file content.
Commit
aa9ea7961afe45e2832cc9cabbba367041902a58
Parent
e9ffc4cdfba78b1…
2 files changed
+20
+23
-18
+20
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -559,5 +559,25 @@ | ||
| 559 | 559 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 560 | 560 | } |
| 561 | 561 | zBuf[j] = 0; |
| 562 | 562 | }while( access(zBuf,0)==0 ); |
| 563 | 563 | } |
| 564 | + | |
| 565 | + | |
| 566 | +/* | |
| 567 | +** Return true if a file named zName exists and has identical content | |
| 568 | +** to the blob pContent. If zName does not exist or if the content is | |
| 569 | +** different in any way, then return false. | |
| 570 | +*/ | |
| 571 | +int file_is_the_same(Blob *pContent, const char *zName){ | |
| 572 | + i64 iSize; | |
| 573 | + int rc; | |
| 574 | + Blob onDisk; | |
| 575 | + | |
| 576 | + iSize = file_size(zName); | |
| 577 | + if( iSize<0 ) return 0; | |
| 578 | + if( iSize!=blob_size(pContent) ) return 0; | |
| 579 | + blob_read_from_file(&onDisk, zName); | |
| 580 | + rc = blob_compare(&onDisk, pContent); | |
| 581 | + blob_reset(&onDisk); | |
| 582 | + return rc==0; | |
| 583 | +} | |
| 564 | 584 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -559,5 +559,25 @@ | |
| 559 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 560 | } |
| 561 | zBuf[j] = 0; |
| 562 | }while( access(zBuf,0)==0 ); |
| 563 | } |
| 564 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -559,5 +559,25 @@ | |
| 559 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 560 | } |
| 561 | zBuf[j] = 0; |
| 562 | }while( access(zBuf,0)==0 ); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /* |
| 567 | ** Return true if a file named zName exists and has identical content |
| 568 | ** to the blob pContent. If zName does not exist or if the content is |
| 569 | ** different in any way, then return false. |
| 570 | */ |
| 571 | int file_is_the_same(Blob *pContent, const char *zName){ |
| 572 | i64 iSize; |
| 573 | int rc; |
| 574 | Blob onDisk; |
| 575 | |
| 576 | iSize = file_size(zName); |
| 577 | if( iSize<0 ) return 0; |
| 578 | if( iSize!=blob_size(pContent) ) return 0; |
| 579 | blob_read_from_file(&onDisk, zName); |
| 580 | rc = blob_compare(&onDisk, pContent); |
| 581 | blob_reset(&onDisk); |
| 582 | return rc==0; |
| 583 | } |
| 584 |
+23
-18
| --- src/vfile.c | ||
| +++ src/vfile.c | ||
| @@ -245,28 +245,33 @@ | ||
| 245 | 245 | const char *zName; |
| 246 | 246 | |
| 247 | 247 | id = db_column_int(&q, 0); |
| 248 | 248 | zName = db_column_text(&q, 1); |
| 249 | 249 | rid = db_column_int(&q, 2); |
| 250 | - if( promptFlag ){ | |
| 251 | - if( file_size(zName)>=0 ){ | |
| 252 | - Blob ans; | |
| 253 | - char *zMsg; | |
| 254 | - char cReply; | |
| 255 | - zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName); | |
| 256 | - prompt_user(zMsg, &ans); | |
| 257 | - free(zMsg); | |
| 258 | - cReply = blob_str(&ans)[0]; | |
| 259 | - blob_reset(&ans); | |
| 260 | - if( cReply=='a' || cReply=='A' ){ | |
| 261 | - promptFlag = 0; | |
| 262 | - cReply = 'y'; | |
| 263 | - } | |
| 264 | - if( cReply=='n' || cReply=='N' ) continue; | |
| 265 | - } | |
| 266 | - } | |
| 267 | - content_get(rid, &content); | |
| 250 | + content_get(rid, &content); | |
| 251 | + if( file_is_the_same(&content, zName) ){ | |
| 252 | + blob_reset(&content); | |
| 253 | + continue; | |
| 254 | + } | |
| 255 | + if( promptFlag ){ | |
| 256 | + Blob ans; | |
| 257 | + char *zMsg; | |
| 258 | + char cReply; | |
| 259 | + zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName); | |
| 260 | + prompt_user(zMsg, &ans); | |
| 261 | + free(zMsg); | |
| 262 | + cReply = blob_str(&ans)[0]; | |
| 263 | + blob_reset(&ans); | |
| 264 | + if( cReply=='a' || cReply=='A' ){ | |
| 265 | + promptFlag = 0; | |
| 266 | + cReply = 'y'; | |
| 267 | + } | |
| 268 | + if( cReply=='n' || cReply=='N' ){ | |
| 269 | + blob_reset(&content); | |
| 270 | + continue; | |
| 271 | + } | |
| 272 | + } | |
| 268 | 273 | if( verbose ) printf("%s\n", &zName[nRepos]); |
| 269 | 274 | blob_write_to_file(&content, zName); |
| 270 | 275 | blob_reset(&content); |
| 271 | 276 | db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d", |
| 272 | 277 | file_mtime(zName), id); |
| 273 | 278 |
| --- src/vfile.c | |
| +++ src/vfile.c | |
| @@ -245,28 +245,33 @@ | |
| 245 | const char *zName; |
| 246 | |
| 247 | id = db_column_int(&q, 0); |
| 248 | zName = db_column_text(&q, 1); |
| 249 | rid = db_column_int(&q, 2); |
| 250 | if( promptFlag ){ |
| 251 | if( file_size(zName)>=0 ){ |
| 252 | Blob ans; |
| 253 | char *zMsg; |
| 254 | char cReply; |
| 255 | zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName); |
| 256 | prompt_user(zMsg, &ans); |
| 257 | free(zMsg); |
| 258 | cReply = blob_str(&ans)[0]; |
| 259 | blob_reset(&ans); |
| 260 | if( cReply=='a' || cReply=='A' ){ |
| 261 | promptFlag = 0; |
| 262 | cReply = 'y'; |
| 263 | } |
| 264 | if( cReply=='n' || cReply=='N' ) continue; |
| 265 | } |
| 266 | } |
| 267 | content_get(rid, &content); |
| 268 | if( verbose ) printf("%s\n", &zName[nRepos]); |
| 269 | blob_write_to_file(&content, zName); |
| 270 | blob_reset(&content); |
| 271 | db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d", |
| 272 | file_mtime(zName), id); |
| 273 |
| --- src/vfile.c | |
| +++ src/vfile.c | |
| @@ -245,28 +245,33 @@ | |
| 245 | const char *zName; |
| 246 | |
| 247 | id = db_column_int(&q, 0); |
| 248 | zName = db_column_text(&q, 1); |
| 249 | rid = db_column_int(&q, 2); |
| 250 | content_get(rid, &content); |
| 251 | if( file_is_the_same(&content, zName) ){ |
| 252 | blob_reset(&content); |
| 253 | continue; |
| 254 | } |
| 255 | if( promptFlag ){ |
| 256 | Blob ans; |
| 257 | char *zMsg; |
| 258 | char cReply; |
| 259 | zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName); |
| 260 | prompt_user(zMsg, &ans); |
| 261 | free(zMsg); |
| 262 | cReply = blob_str(&ans)[0]; |
| 263 | blob_reset(&ans); |
| 264 | if( cReply=='a' || cReply=='A' ){ |
| 265 | promptFlag = 0; |
| 266 | cReply = 'y'; |
| 267 | } |
| 268 | if( cReply=='n' || cReply=='N' ){ |
| 269 | blob_reset(&content); |
| 270 | continue; |
| 271 | } |
| 272 | } |
| 273 | if( verbose ) printf("%s\n", &zName[nRepos]); |
| 274 | blob_write_to_file(&content, zName); |
| 275 | blob_reset(&content); |
| 276 | db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d", |
| 277 | file_mtime(zName), id); |
| 278 |