diff mbox series

c++: Windows rename [PR 98412]

Message ID 7e54b0dc-c364-f050-a363-7015d66c1ac9@acm.org
State New
Headers show
Series c++: Windows rename [PR 98412] | expand

Commit Message

Nathan Sidwell Dec. 21, 2020, 5:50 p.m. UTC
Some system's rename(2) fails if the target already exists, so delete it
first.

	gcc/cp/
         * module.cc (create_dirs): Add logging.
	(finish_module_processing): Unlink before rename.
diff mbox series

Patch

diff --git i/gcc/cp/module.cc w/gcc/cp/module.cc
index fc918d296a2..7e38293545f 100644
--- i/gcc/cp/module.cc
+++ w/gcc/cp/module.cc
@@ -4693,6 +4693,7 @@  create_dirs (char *path)
 	char sep = *base;
 	*base = 0;
 	int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
+	dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
 	*base = sep;
 	if (failed
 	    /* Maybe racing with another creator (of a *different*
@@ -19744,8 +19745,17 @@  finish_module_processing (cpp_reader *reader)
 	      input_location = loc;
 	    }
 	  if (to.end ())
-	    if (rename (tmp_name, path))
-	      to.set_error (errno);
+	    {
+	      /* Some OS's do not replace NEWNAME if it already
+		 exists.  This'll have a race condition in erroneous
+		 concurrent builds.  */
+	      unlink (path);
+	      if (rename (tmp_name, path))
+		{
+		  dump () && dump ("Rename ('%s','%s') errno=%u", errno);
+		  to.set_error (errno);
+		}
+	    }
 
 	  if (to.get_error ())
 	    {