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.

drh 2010-12-22 22:33 trunk
Commit aa9ea7961afe45e2832cc9cabbba367041902a58
2 files changed +20 +23 -18
+20
--- src/file.c
+++ src/file.c
@@ -559,5 +559,25 @@
559559
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
560560
}
561561
zBuf[j] = 0;
562562
}while( access(zBuf,0)==0 );
563563
}
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
+}
564584
--- 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 @@
245245
const char *zName;
246246
247247
id = db_column_int(&q, 0);
248248
zName = db_column_text(&q, 1);
249249
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
+ }
268273
if( verbose ) printf("%s\n", &zName[nRepos]);
269274
blob_write_to_file(&content, zName);
270275
blob_reset(&content);
271276
db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
272277
file_mtime(zName), id);
273278
--- 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

Keyboard Shortcuts

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