[Python-checkins] CVS: python/dist/src/Python import.c,2.150,2.151

Guido van Rossum python-dev@python.org
Wed, 20 Sep 2000 13:31:40 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv31808

Modified Files:
	import.c 
Log Message:
On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition

Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.150
retrieving revision 2.151
diff -C2 -r2.150 -r2.151
*** import.c	2000/09/01 23:29:28	2.150
--- import.c	2000/09/20 20:31:38	2.151
***************
*** 29,32 ****
--- 29,33 ----
  #include <sys/types.h>
  #endif
+ 
  #ifndef DONT_HAVE_SYS_STAT_H
  #include <sys/stat.h>
***************
*** 35,38 ****
--- 36,43 ----
  #endif
  
+ #ifdef HAVE_FCNTL_H
+ #include <fcntl.h>
+ #endif
+ 
  #if defined(PYCC_VACPP)
  /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
***************
*** 628,631 ****
--- 633,661 ----
  
  
+ /* Helper to open a bytecode file for writing in exclusive mode */
+ 
+ static FILE *
+ open_exclusive(char *filename)
+ {
+ #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
+ 	/* Use O_EXCL to avoid a race condition when another process tries to
+ 	   write the same file.  When that happens, our open() call fails,
+ 	   which is just fine (since it's only a cache).
+ 	   XXX If the file exists and is writable but the directory is not
+ 	   writable, the file will never be written.  Oh well.
+ 	*/
+ 	int fd;
+ 	(void) unlink(filename);
+ 	fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC, 0666);
+ 	if (fd < 0)
+ 		return NULL;
+ 	return fdopen(fd, "wb");
+ #else
+ 	/* Best we can do -- on Windows this can't happen anyway */
+ 	return fopen(filename, "wb");
+ #endif
+ }
+ 
+ 
  /* Write a compiled module to a file, placing the time of last
     modification of its source into the header.
***************
*** 638,642 ****
  	FILE *fp;
  
! 	fp = fopen(cpathname, "wb");
  	if (fp == NULL) {
  		if (Py_VerboseFlag)
--- 668,672 ----
  	FILE *fp;
  
! 	fp = open_exclusive(cpathname);
  	if (fp == NULL) {
  		if (Py_VerboseFlag)