From: Denis Vlasenko Date: Sat, 3 May 2008 11:35:59 +0000 (-0000) Subject: vi: do not truncate file to zero length. closes bug 2944. X-Git-Tag: 1_11_0~258 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8abae8841019fcc75852131b2d1d16bf5710b4b7;p=oweals%2Fbusybox.git vi: do not truncate file to zero length. closes bug 2944. function old new delta file_write 98 104 +6 --- diff --git a/editors/vi.c b/editors/vi.c index 454cfcee4..4e5a5ac4a 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -2437,11 +2437,16 @@ static int file_write(char * fn, char * first, char * last) return -2; } 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) return -1; cnt = last - first + 1; charcnt = full_write(fd, first, cnt); + ftruncate(fd, charcnt); if (charcnt == cnt) { // good write //file_modified = FALSE; // the file has not been modified