Skip to content

Commit cf0bfd9

Browse files
committed
patch 8.1.1349: if writing runs into conversion error backup file is deleted
Problem: If writing runs into a conversion error the backup file is deleted. (Arseny Nasokin) Solution: Don't delete the backup file is the file was overwritten and a conversion error occurred. (Christian Brabandt, closes #4387)
1 parent f8191c5 commit cf0bfd9

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/fileio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4985,10 +4985,10 @@ buf_write(
49854985
}
49864986
}
49874987

4988-
/*
4989-
* Remove the backup unless 'backup' option is set
4990-
*/
4991-
if (!p_bk && backup != NULL && mch_remove(backup) != 0)
4988+
// Remove the backup unless 'backup' option is set or there was a
4989+
// conversion error.
4990+
if (!p_bk && backup != NULL && !write_info.bw_conv_error
4991+
&& mch_remove(backup) != 0)
49924992
emsg(_("E207: Can't delete backup file"));
49934993

49944994
goto nofail;

src/testdir/test_writefile.vim

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,43 @@ func Test_writefile_fails_conversion()
3636
if !has('iconv') || has('sun')
3737
return
3838
endif
39+
" Without a backup file the write won't happen if there is a conversion
40+
" error.
3941
set nobackup nowritebackup
4042
new
4143
let contents = ["line one", "line two"]
4244
call writefile(contents, 'Xfile')
4345
edit Xfile
4446
call setline(1, ["first line", "cannot convert \u010b", "third line"])
45-
call assert_fails('write ++enc=cp932')
47+
call assert_fails('write ++enc=cp932', 'E513:')
4648
call assert_equal(contents, readfile('Xfile'))
4749

4850
call delete('Xfile')
4951
bwipe!
5052
set backup& writebackup&
5153
endfunc
5254

55+
func Test_writefile_fails_conversion2()
56+
if !has('iconv') || has('sun')
57+
return
58+
endif
59+
" With a backup file the write happens even if there is a conversion error,
60+
" but then the backup file must remain
61+
set nobackup writebackup
62+
let contents = ["line one", "line two"]
63+
call writefile(contents, 'Xfile_conversion_err')
64+
edit Xfile_conversion_err
65+
call setline(1, ["first line", "cannot convert \u010b", "third line"])
66+
set fileencoding=latin1
67+
let output = execute('write')
68+
call assert_match('CONVERSION ERROR', output)
69+
call assert_equal(contents, readfile('Xfile_conversion_err~'))
70+
71+
call delete('Xfile_conversion_err')
72+
call delete('Xfile_conversion_err~')
73+
bwipe!
74+
endfunc
75+
5376
func SetFlag(timer)
5477
let g:flag = 1
5578
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ static char *(features[]) =
767767

768768
static int included_patches[] =
769769
{ /* Add new patch number below this line */
770+
/**/
771+
1349,
770772
/**/
771773
1348,
772774
/**/

0 commit comments

Comments
 (0)