vi: do not truncate file to zero length. closes bug 2944.

function                                             old     new   delta
file_write                                            98     104      +6
This commit is contained in:
Denis Vlasenko 2008-05-03 11:35:59 +00:00
parent 96b99b860c
commit 8abae88410

View File

@ -2437,11 +2437,16 @@ static int file_write(char * fn, char * first, char * last)
return -2; return -2;
} }
charcnt = 0; charcnt = 0;
fd = open(fn, (O_WRONLY | O_CREAT | O_TRUNC), 0666); /* By popular request we do not open file with O_TRUNC,
* but instead ftruncate() it _after_ successful write.
* Might reduce amount of data lost on power fail etc.
*/
fd = open(fn, (O_WRONLY | O_CREAT), 0666);
if (fd < 0) if (fd < 0)
return -1; return -1;
cnt = last - first + 1; cnt = last - first + 1;
charcnt = full_write(fd, first, cnt); charcnt = full_write(fd, first, cnt);
ftruncate(fd, charcnt);
if (charcnt == cnt) { if (charcnt == cnt) {
// good write // good write
//file_modified = FALSE; // the file has not been modified //file_modified = FALSE; // the file has not been modified