Handle rename() failures

Renaming the temporary, encrypted file to the intended filename (per the
buffer) may fail (e.g., typoed directory name).  In this case, remove
the temporary file and then give the typical error message for this
situation.

In order to be safe, the message is manually being generated instead of
trying to be clever and do something like “noautocmd write” expecting it
to hit the same error that rename() did.

Closes jamessan/vim-gnupg#56
Signed-off-by: James McCoy <vega.james@gmail.com>
This commit is contained in:
James McCoy 2015-12-17 21:52:12 -05:00
parent c2ddc05bc2
commit 2dfb82f13e
No known key found for this signature in database
GPG Key ID: DFE691AE331BA3DB

View File

@ -1,5 +1,5 @@
" Name: gnupg.vim
" Last Change: 2015 Dec 04
" Last Change: 2015 Dec 17
" Maintainer: James McCoy <vega.james@gmail.com>
" Original Author: Markus Braun <markus.braun@krawel.de>
" Summary: Vim plugin for transparent editing of gpg encrypted files.
@ -739,7 +739,15 @@ function s:GPGEncrypt()
endif
let filename = resolve(expand('<afile>'))
call rename(destfile, filename)
if rename(destfile, filename)
" Rename failed, so clean up the tempfile
call delete(destfile)
echohl GPGError
echom printf("\"%s\" E212: Can't open file for writing", filename)
echohl None
return
endif
if auType == 'BufWrite'
setl nomodified
let &readonly = filereadable(filename) && filewritable(filename) == 0