--- Python-2.5.1/Python/import.c.atomic 2007-08-08 20:51:41.313394000 -0400
+++ Python-2.5.1/Python/import.c 2007-08-08 21:01:57.640451000 -0400
@@ -858,19 +858,28 @@
/* Write a compiled module to a file, placing the time of last
modification of its source into the header.
- Errors are ignored, if a write error occurs an attempt is made to
- remove the file. */
+ Write to a temporary file first so that creating the file is atomic.
+ Errors are ignored, if a write/unlink/rename error occurs an attempt
+ is made to remove the temporary file. */
static void
write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
{
FILE *fp;
+ char *tmppathname;
- fp = open_exclusive(cpathname);
+ /* the temporary file is called cpathname + ".tmp" */
+ if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1))
+ == NULL) {
+ return;
+ }
+ sprintf (tmppathname, "%s.tmp", cpathname);
+ fp = open_exclusive(tmppathname);
if (fp == NULL) {
if (Py_VerboseFlag)
PySys_WriteStderr(
- "# can't create %s\n", cpathname);
+ "# can't create %s\n", tmppathname);
+ PyMem_Free(tmppathname);
return;
}
PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
@@ -879,10 +888,11 @@
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
if (fflush(fp) != 0 || ferror(fp)) {
if (Py_VerboseFlag)
- PySys_WriteStderr("# can't write %s\n", cpathname);
+ PySys_WriteStderr("# can't write %s\n", tmppathname);
/* Don't keep partial file */
fclose(fp);
- (void) unlink(cpathname);
+ (void) unlink(tmppathname);
+ PyMem_Free(tmppathname);
return;
}
/* Now write the true mtime */
@@ -891,8 +901,30 @@
PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
fflush(fp);
fclose(fp);
+ /* Delete the old compiled file, if exists */
+ if (unlink (cpathname)) {
+ if ((errno != ENOENT)) {
+ /* the file exists but could not be deleted */
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# can't unlink %s\n", cpathname);
+ (void) unlink(tmppathname);
+ PyMem_Free(tmppathname);
+ return;
+ }
+ }
+ /* rename the tmp file to the real file name */
+ if (rename (tmppathname, cpathname)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# can't rename %s to %s\n", tmppathname, cpathname);
+ (void) unlink(tmppathname);
+ PyMem_Free(tmppathname);
+ return;
+ }
if (Py_VerboseFlag)
PySys_WriteStderr("# wrote %s\n", cpathname);
+ PyMem_Free(tmppathname);
}