To: vim-dev@vim.org Subject: Patch 6.0.040 Fcc: outbox From: Bram Moolenaar MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.0.040 Problem: When 'fileencoding' is invalid and writing fails because of this, the original file is gone. (Eric Carlier) Solution: Restore the original file from the backup. Files: src/fileio.c *** ../vim60.39/src/fileio.c Thu Sep 20 18:09:15 2001 --- src/fileio.c Tue Oct 30 19:59:18 2001 *************** *** 2183,2189 **** int backup_copy = FALSE; /* copy the original file? */ int dobackup; char_u *ffname; ! char_u *wfname; /* name of file to write to */ char_u *s; char_u *ptr; char_u c; --- 2183,2189 ---- int backup_copy = FALSE; /* copy the original file? */ int dobackup; char_u *ffname; ! char_u *wfname = NULL; /* name of file to write to */ char_u *s; char_u *ptr; char_u c; *************** *** 3016,3022 **** if (got_int) { errmsg = (char_u *)_(e_interr); ! goto fail; } } --- 3016,3022 ---- if (got_int) { errmsg = (char_u *)_(e_interr); ! goto restore_backup; } } *************** *** 3030,3036 **** if (mch_has_resource_fork(fname)) { errmsg = (char_u *)_("The resource fork will be lost (use ! to override)"); ! goto fail; } #endif --- 3030,3036 ---- if (mch_has_resource_fork(fname)) { errmsg = (char_u *)_("The resource fork will be lost (use ! to override)"); ! goto restore_backup; } #endif *************** *** 3119,3125 **** if (wfname == NULL) /* Can't write without a tempfile! */ { errmsg = (char_u *)_("E214: Can't find temp file for writing"); ! goto fail; } } # endif --- 3119,3125 ---- if (wfname == NULL) /* Can't write without a tempfile! */ { errmsg = (char_u *)_("E214: Can't find temp file for writing"); ! goto restore_backup; } } # endif *************** *** 3137,3143 **** if (!forceit) { errmsg = (char_u *)_("E213: Cannot convert (use ! to write without conversion)"); ! goto fail; } notconverted = TRUE; } --- 3137,3143 ---- if (!forceit) { errmsg = (char_u *)_("E213: Cannot convert (use ! to write without conversion)"); ! goto restore_backup; } notconverted = TRUE; } *************** *** 3156,3163 **** : (O_CREAT | O_TRUNC)) , 0666)) < 0) { - struct stat st; - /* * A forced write will try to create a new file if the old one is * still readonly. This may also happen when the directory is --- 3156,3161 ---- *************** *** 3196,3233 **** } } } ! /* ! * If we failed to open the file, we don't need a backup. Throw it ! * away. If we moved or removed the original file try to put the ! * backup in its place. ! */ ! if (backup != NULL && wfname == fname) { ! if (backup_copy) { ! /* ! * There is a small chance that we removed the original, try ! * to move the copy in its place. ! * This may not work if the vim_rename() fails. ! * In that case we leave the copy around. ! */ ! /* If file does not exist, put the copy in its place */ ! if (mch_stat((char *)fname, &st) < 0) vim_rename(backup, fname); ! /* if original file does exist throw away the copy */ ! if (mch_stat((char *)fname, &st) >= 0) ! mch_remove(backup); ! } ! else ! { ! /* try to put the original file back */ ! vim_rename(backup, fname); } - } ! /* if original file no longer exists give an extra warning */ ! if (!newfile && mch_stat((char *)fname, &st) < 0) ! end = 0; #ifdef FEAT_MBYTE if (wfname != fname) --- 3194,3237 ---- } } } ! ! restore_backup: { ! struct stat st; ! ! /* ! * If we failed to open the file, we don't need a backup. Throw it ! * away. If we moved or removed the original file try to put the ! * backup in its place. ! */ ! if (backup != NULL && wfname == fname) { ! if (backup_copy) ! { ! /* ! * There is a small chance that we removed the original, ! * try to move the copy in its place. ! * This may not work if the vim_rename() fails. ! * In that case we leave the copy around. ! */ ! /* If file does not exist, put the copy in its place */ ! if (mch_stat((char *)fname, &st) < 0) ! vim_rename(backup, fname); ! /* if original file does exist throw away the copy */ ! if (mch_stat((char *)fname, &st) >= 0) ! mch_remove(backup); ! } ! else ! { ! /* try to put the original file back */ vim_rename(backup, fname); ! } } ! /* if original file no longer exists give an extra warning */ ! if (!newfile && mch_stat((char *)fname, &st) < 0) ! end = 0; ! } #ifdef FEAT_MBYTE if (wfname != fname) *** ../vim60.39/src/version.c Tue Oct 30 14:52:27 2001 --- src/version.c Tue Oct 30 20:01:33 2001 *************** *** 608,609 **** --- 608,611 ---- { /* Add new patch number below this line */ + /**/ + 40, /**/ -- VOICE OVER: As the horrendous Black Beast lunged forward, escape for Arthur and his knights seemed hopeless, when, suddenly ... the animator suffered a fatal heart attack. ANIMATOR: Aaaaagh! VOICE OVER: The cartoon peril was no more ... The Quest for Holy Grail could continue. "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@moolenaar.net -- http://www.moolenaar.net \\\ ((( Creator of Vim -- http://vim.sf.net -- ftp://ftp.vim.org/pub/vim ))) \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///