Karsten Hopp cb6e30
To: vim-dev@vim.org
Karsten Hopp cb6e30
Subject: Patch 7.2.076
Karsten Hopp cb6e30
Fcc: outbox
Karsten Hopp cb6e30
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp cb6e30
Mime-Version: 1.0
Karsten Hopp cb6e30
Content-Type: text/plain; charset=ISO-8859-1
Karsten Hopp cb6e30
Content-Transfer-Encoding: 8bit
Karsten Hopp cb6e30
------------
Karsten Hopp cb6e30
Karsten Hopp cb6e30
Patch 7.2.076
Karsten Hopp cb6e30
Problem:    rename(from, to) deletes the file if "from" and "to" are not equal
Karsten Hopp cb6e30
	    but still refer to the same file.  E.g., on a FAT32 filesystem
Karsten Hopp cb6e30
	    under Unix.
Karsten Hopp cb6e30
Solution:   Go through another file name.
Karsten Hopp cb6e30
Files:	    src/fileio.c
Karsten Hopp cb6e30
Karsten Hopp cb6e30
Karsten Hopp cb6e30
*** ../vim-7.2.075/src/fileio.c	Fri Nov 28 21:26:50 2008
Karsten Hopp cb6e30
--- src/fileio.c	Tue Dec 30 16:04:44 2008
Karsten Hopp cb6e30
***************
Karsten Hopp cb6e30
*** 6119,6124 ****
Karsten Hopp cb6e30
--- 6119,6165 ----
Karsten Hopp cb6e30
      if (mch_stat((char *)from, &st) < 0)
Karsten Hopp cb6e30
  	return -1;
Karsten Hopp cb6e30
  
Karsten Hopp cb6e30
+ #ifdef UNIX
Karsten Hopp cb6e30
+     {
Karsten Hopp cb6e30
+ 	struct stat	st_to;
Karsten Hopp cb6e30
+ 	char		tempname[MAXPATHL + 1];
Karsten Hopp cb6e30
+ 
Karsten Hopp cb6e30
+ 	/* It's possible for the source and destination to be the same file.
Karsten Hopp cb6e30
+ 	 * This happens when "from" and "to" differ in case and are on a FAT32
Karsten Hopp cb6e30
+ 	 * filesystem.  In that case go through a temp file name. */
Karsten Hopp cb6e30
+ 	if (mch_stat((char *)to, &st_to) >= 0
Karsten Hopp cb6e30
+ 		&& st.st_dev == st_to.st_dev
Karsten Hopp cb6e30
+ 		&& st.st_ino == st_to.st_ino)
Karsten Hopp cb6e30
+ 	{
Karsten Hopp cb6e30
+ 	    /* Find a name that doesn't exist and is in the same directory.
Karsten Hopp cb6e30
+ 	     * Move "from" to "tempname" and then to "to". */
Karsten Hopp cb6e30
+ 	    if (STRLEN(from) >= MAXPATHL - 5)
Karsten Hopp cb6e30
+ 		return -1;
Karsten Hopp cb6e30
+ 	    STRCPY(tempname, from);
Karsten Hopp cb6e30
+ 	    for (n = 123; n < 99999; ++n)
Karsten Hopp cb6e30
+ 	    {
Karsten Hopp cb6e30
+ 		sprintf(gettail(tempname), "%d", n);
Karsten Hopp cb6e30
+ 		if (mch_stat(tempname, &st_to) < 0)
Karsten Hopp cb6e30
+ 		{
Karsten Hopp cb6e30
+ 		    if (mch_rename((char *)from, tempname) == 0)
Karsten Hopp cb6e30
+ 		    {
Karsten Hopp cb6e30
+ 			if (mch_rename(tempname, (char *)to) == 0)
Karsten Hopp cb6e30
+ 			    return 0;
Karsten Hopp cb6e30
+ 			/* Strange, the second step failed.  Try moving the
Karsten Hopp cb6e30
+ 			 * file back and return failure. */
Karsten Hopp cb6e30
+ 			mch_rename(tempname, (char *)from);
Karsten Hopp cb6e30
+ 			return -1;
Karsten Hopp cb6e30
+ 		    }
Karsten Hopp cb6e30
+ 		    /* If it fails for one temp name it will most likely fail
Karsten Hopp cb6e30
+ 		     * for any temp name, give up. */
Karsten Hopp cb6e30
+ 		    return -1;
Karsten Hopp cb6e30
+ 		}
Karsten Hopp cb6e30
+ 	    }
Karsten Hopp cb6e30
+ 	    return -1;
Karsten Hopp cb6e30
+ 	}
Karsten Hopp cb6e30
+     }
Karsten Hopp cb6e30
+ #endif
Karsten Hopp cb6e30
+ 
Karsten Hopp cb6e30
      /*
Karsten Hopp cb6e30
       * Delete the "to" file, this is required on some systems to make the
Karsten Hopp cb6e30
       * mch_rename() work, on other systems it makes sure that we don't have
Karsten Hopp cb6e30
*** ../vim-7.2.075/src/version.c	Wed Dec 24 14:24:41 2008
Karsten Hopp cb6e30
--- src/version.c	Tue Dec 30 16:09:51 2008
Karsten Hopp cb6e30
***************
Karsten Hopp cb6e30
*** 678,679 ****
Karsten Hopp cb6e30
--- 678,681 ----
Karsten Hopp cb6e30
  {   /* Add new patch number below this line */
Karsten Hopp cb6e30
+ /**/
Karsten Hopp cb6e30
+     76,
Karsten Hopp cb6e30
  /**/
Karsten Hopp cb6e30
Karsten Hopp cb6e30
-- 
Karsten Hopp cb6e30
FATAL ERROR! SYSTEM HALTED! - Press any key to continue doing nothing.
Karsten Hopp cb6e30
Karsten Hopp cb6e30
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp cb6e30
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp cb6e30
\\\        download, build and distribute -- http://www.A-A-P.org        ///
Karsten Hopp cb6e30
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///