13290
|
1 |
--- Python-2.5.1/Python/import.c.atomic 2007-08-08 20:51:41.313394000 -0400
|
|
2 |
+++ Python-2.5.1/Python/import.c 2007-08-08 21:01:57.640451000 -0400
|
|
3 |
@@ -858,19 +858,28 @@
|
|
4 |
|
|
5 |
/* Write a compiled module to a file, placing the time of last
|
|
6 |
modification of its source into the header.
|
|
7 |
- Errors are ignored, if a write error occurs an attempt is made to
|
|
8 |
- remove the file. */
|
|
9 |
+ Write to a temporary file first so that creating the file is atomic.
|
|
10 |
+ Errors are ignored, if a write/unlink/rename error occurs an attempt
|
|
11 |
+ is made to remove the temporary file. */
|
|
12 |
|
|
13 |
static void
|
|
14 |
write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
|
|
15 |
{
|
|
16 |
FILE *fp;
|
|
17 |
+ char *tmppathname;
|
|
18 |
|
|
19 |
- fp = open_exclusive(cpathname);
|
|
20 |
+ /* the temporary file is called cpathname + ".tmp" */
|
|
21 |
+ if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1))
|
|
22 |
+ == NULL) {
|
|
23 |
+ return;
|
|
24 |
+ }
|
|
25 |
+ sprintf (tmppathname, "%s.tmp", cpathname);
|
|
26 |
+ fp = open_exclusive(tmppathname);
|
|
27 |
if (fp == NULL) {
|
|
28 |
if (Py_VerboseFlag)
|
|
29 |
PySys_WriteStderr(
|
|
30 |
- "# can't create %s\n", cpathname);
|
|
31 |
+ "# can't create %s\n", tmppathname);
|
|
32 |
+ PyMem_Free(tmppathname);
|
|
33 |
return;
|
|
34 |
}
|
|
35 |
PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
|
|
36 |
@@ -879,10 +888,11 @@
|
|
37 |
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
|
|
38 |
if (fflush(fp) != 0 || ferror(fp)) {
|
|
39 |
if (Py_VerboseFlag)
|
|
40 |
- PySys_WriteStderr("# can't write %s\n", cpathname);
|
|
41 |
+ PySys_WriteStderr("# can't write %s\n", tmppathname);
|
|
42 |
/* Don't keep partial file */
|
|
43 |
fclose(fp);
|
|
44 |
- (void) unlink(cpathname);
|
|
45 |
+ (void) unlink(tmppathname);
|
|
46 |
+ PyMem_Free(tmppathname);
|
|
47 |
return;
|
|
48 |
}
|
|
49 |
/* Now write the true mtime */
|
|
50 |
@@ -891,8 +901,30 @@
|
|
51 |
PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
|
|
52 |
fflush(fp);
|
|
53 |
fclose(fp);
|
|
54 |
+ /* Delete the old compiled file, if exists */
|
|
55 |
+ if (unlink (cpathname)) {
|
|
56 |
+ if ((errno != ENOENT)) {
|
|
57 |
+ /* the file exists but could not be deleted */
|
|
58 |
+ if (Py_VerboseFlag)
|
|
59 |
+ PySys_WriteStderr(
|
|
60 |
+ "# can't unlink %s\n", cpathname);
|
|
61 |
+ (void) unlink(tmppathname);
|
|
62 |
+ PyMem_Free(tmppathname);
|
|
63 |
+ return;
|
|
64 |
+ }
|
|
65 |
+ }
|
|
66 |
+ /* rename the tmp file to the real file name */
|
|
67 |
+ if (rename (tmppathname, cpathname)) {
|
|
68 |
+ if (Py_VerboseFlag)
|
|
69 |
+ PySys_WriteStderr(
|
|
70 |
+ "# can't rename %s to %s\n", tmppathname, cpathname);
|
|
71 |
+ (void) unlink(tmppathname);
|
|
72 |
+ PyMem_Free(tmppathname);
|
|
73 |
+ return;
|
|
74 |
+ }
|
|
75 |
if (Py_VerboseFlag)
|
|
76 |
PySys_WriteStderr("# wrote %s\n", cpathname);
|
|
77 |
+ PyMem_Free(tmppathname);
|
|
78 |
}
|
|
79 |
|
|
80 |
|