diff options
Diffstat (limited to 'src/nvim/memfile.c')
-rw-r--r-- | src/nvim/memfile.c | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/src/nvim/memfile.c b/src/nvim/memfile.c index a1713edb66..d032caa3be 100644 --- a/src/nvim/memfile.c +++ b/src/nvim/memfile.c @@ -46,6 +46,7 @@ #include "nvim/assert_defs.h" #include "nvim/buffer_defs.h" +#include "nvim/errors.h" #include "nvim/fileio.h" #include "nvim/gettext_defs.h" #include "nvim/globals.h" @@ -565,7 +566,8 @@ static int mf_write(memfile_T *mfp, bhdr_T *hp) bhdr_T *hp2; unsigned page_count; // number of pages written - if (mfp->mf_fd < 0) { // there is no file, can't write + if (mfp->mf_fd < 0 && !mfp->mf_reopen) { + // there is no file and there was no file, can't write return FAIL; } @@ -592,28 +594,48 @@ static int mf_write(memfile_T *mfp, bhdr_T *hp) // TODO(elmart): Check (page_size * nr) within off_T bounds. off_T offset = (off_T)(page_size * nr); // offset in the file - if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) { - PERROR(_("E296: Seek error in swap file write")); - return FAIL; - } if (hp2 == NULL) { // freed block, fill with dummy data page_count = 1; } else { page_count = hp2->bh_page_count; } unsigned size = page_size * page_count; // number of bytes written - void *data = (hp2 == NULL) ? hp->bh_data : hp2->bh_data; - if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size) { - /// Avoid repeating the error message, this mostly happens when the - /// disk is full. We give the message again only after a successful - /// write or when hitting a key. We keep on trying, in case some - /// space becomes available. - if (!did_swapwrite_msg) { - emsg(_("E297: Write error in swap file")); + + for (int attempt = 1; attempt <= 2; attempt++) { + if (mfp->mf_fd >= 0) { + if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) { + PERROR(_("E296: Seek error in swap file write")); + return FAIL; + } + void *data = (hp2 == NULL) ? hp->bh_data : hp2->bh_data; + if ((unsigned)write_eintr(mfp->mf_fd, data, size) == size) { + break; + } + } + + if (attempt == 1) { + // If the swap file is on a network drive, and the network + // gets disconnected and then re-connected, we can maybe fix it + // by closing and then re-opening the file. + if (mfp->mf_fd >= 0) { + close(mfp->mf_fd); + } + mfp->mf_fd = os_open(mfp->mf_fname, mfp->mf_flags, S_IREAD | S_IWRITE); + mfp->mf_reopen = (mfp->mf_fd < 0); + } + if (attempt == 2 || mfp->mf_fd < 0) { + // Avoid repeating the error message, this mostly happens when the + // disk is full. We give the message again only after a successful + // write or when hitting a key. We keep on trying, in case some + // space becomes available. + if (!did_swapwrite_msg) { + emsg(_("E297: Write error in swap file")); + } + did_swapwrite_msg = true; + return FAIL; } - did_swapwrite_msg = true; - return FAIL; } + did_swapwrite_msg = false; if (hp2 != NULL) { // written a non-dummy block hp2->bh_flags &= ~BH_DIRTY; @@ -750,7 +772,9 @@ static bool mf_do_open(memfile_T *mfp, char *fname, int flags) emsg(_("E300: Swap file already exists (symlink attack?)")); } else { // try to open the file - mfp->mf_fd = os_open(mfp->mf_fname, flags | O_NOFOLLOW, S_IREAD | S_IWRITE); + flags |= O_NOFOLLOW; + mfp->mf_flags = flags; + mfp->mf_fd = os_open(mfp->mf_fname, flags, S_IREAD | S_IWRITE); } // If the file cannot be opened, use memory only |