From 2dfb82f13ea87e36c5df34024d3d2b054b0e72f0 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 17 Dec 2015 21:52:12 -0500 Subject: [PATCH] Handle rename() failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- plugin/gnupg.vim | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 06e1410..3b00366 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2015 Dec 04 +" Last Change: 2015 Dec 17 " Maintainer: James McCoy " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -739,7 +739,15 @@ function s:GPGEncrypt() endif let filename = resolve(expand('')) - 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