To: vim-dev@vim.org Subject: Patch 6.0.079 Fcc: outbox From: Bram Moolenaar MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.0.079 Problem: When "W" is in 'cpoptions' and 'backupcopy' is "no" or "auto", can still overwrite a read-only file, because it's renamed. (Gary Holloway) Solution: Add a check for a read-only file before renaming the file to become the backup. Files: src/fileio.c *** ../vim60.78/src/fileio.c Tue Oct 30 20:35:08 2001 --- src/fileio.c Mon Nov 5 19:37:58 2001 *************** *** 2204,2209 **** --- 2204,2211 ---- int device = FALSE; /* writing to a device */ struct stat st_old; int prev_got_int = got_int; + int file_readonly = FALSE; /* overwritten file is read-only */ + static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ int made_writable = FALSE; /* 'w' bit has been set */ #endif *************** *** 2540,2546 **** * Check if the file is really writable (when renaming the file to * make a backup we won't discover it later). */ ! if (!forceit && ( # ifdef USE_MCH_ACCESS # ifdef UNIX (perm & 0222) == 0 || --- 2542,2548 ---- * Check if the file is really writable (when renaming the file to * make a backup we won't discover it later). */ ! file_readonly = ( # ifdef USE_MCH_ACCESS # ifdef UNIX (perm & 0222) == 0 || *************** *** 2548,2558 **** mch_access((char *)fname, W_OK) # else (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 ! ? TRUE : (close(fd), FALSE) # endif ! )) { ! errmsg = (char_u *)_("is read-only (use ! to override)"); goto fail; } --- 2550,2564 ---- mch_access((char *)fname, W_OK) # else (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 ! ? TRUE : (close(fd), FALSE) # endif ! ); ! if (!forceit && file_readonly) { ! if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) ! errmsg = (char_u *)_(err_readonly); ! else ! errmsg = (char_u *)_("is read-only (use ! to override)"); goto fail; } *************** *** 2895,2900 **** --- 2901,2919 ---- /* * Make a backup by renaming the original file. + */ + /* + * If 'cpoptions' includes the "W" flag, we don't want to + * overwrite a read-only file. But rename may be possible + * anyway, thus we need an extra check here. + */ + if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) + { + errmsg = (char_u *)_(err_readonly); + goto fail; + } + + /* * * Form the backup file name - change path/fo.o.h to * path/fo.o.h.bak Try all directories in 'backupdir', first one *** ../vim60.78/src/version.c Mon Nov 5 13:16:21 2001 --- src/version.c Mon Nov 5 20:31:42 2001 *************** *** 608,609 **** --- 608,611 ---- { /* Add new patch number below this line */ + /**/ + 79, /**/ -- hundred-and-one symptoms of being an internet addict: 1. You actually wore a blue ribbon to protest the Communications Decency Act. /// 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 ///