11 + Errors are ignored, if a write/unlink/rename error occurs an attempt |
11 + Errors are ignored, if a write/unlink/rename error occurs an attempt |
12 + is made to remove the temporary file. */ |
12 + is made to remove the temporary file. */ |
13 |
13 |
14 static void |
14 static void |
15 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) |
15 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) |
16 @@ -879,12 +880,21 @@ |
16 @@ -882,11 +883,21 @@ |
17 #else |
17 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; |
18 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; |
18 #endif |
19 #endif |
|
20 + char *tmppathname; |
|
21 + |
|
22 + /* the temporary file is called cpathname + ".tmp" */ |
|
23 + if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1)) |
|
24 + == NULL) { |
|
25 + return; |
|
26 + } |
|
27 + sprintf (tmppathname, "%s.tmp", cpathname); |
|
28 + fp = open_exclusive(tmppathname, mode); |
|
29 |
19 |
30 - fp = open_exclusive(cpathname, mode); |
20 - fp = open_exclusive(cpathname, mode); |
31 if (fp == NULL) { |
21 + char *tmppathname; |
32 if (Py_VerboseFlag) |
22 + |
33 PySys_WriteStderr( |
23 + /* the temporary file is called cpathname + ".tmp" */ |
34 - "# can't create %s\n", cpathname); |
24 + if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1)) |
35 + "# can't create %s\n", tmppathname); |
25 + == NULL) { |
36 + PyMem_Free(tmppathname); |
26 + return; |
37 return; |
27 + } |
38 } |
28 + sprintf (tmppathname, "%s.tmp", cpathname); |
39 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION); |
29 + fp = open_exclusive(tmppathname, mode); |
40 @@ -893,10 +903,11 @@ |
30 + |
41 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION); |
31 if (fp == NULL) { |
42 if (fflush(fp) != 0 || ferror(fp)) { |
32 if (Py_VerboseFlag) |
43 if (Py_VerboseFlag) |
33 PySys_WriteStderr( |
44 - PySys_WriteStderr("# can't write %s\n", cpathname); |
34 - "# can't create %s\n", cpathname); |
45 + PySys_WriteStderr("# can't write %s\n", tmppathname); |
35 + "# can't create %s\n", tmppathname); |
46 /* Don't keep partial file */ |
36 + PyMem_Free(tmppathname); |
47 fclose(fp); |
37 return; |
48 - (void) unlink(cpathname); |
38 } |
49 + (void) unlink(tmppathname); |
39 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION); |
50 + PyMem_Free(tmppathname); |
40 @@ -895,10 +906,11 @@ |
51 return; |
41 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION); |
52 } |
42 if (fflush(fp) != 0 || ferror(fp)) { |
53 /* Now write the true mtime */ |
43 if (Py_VerboseFlag) |
54 @@ -905,8 +916,30 @@ |
44 - PySys_WriteStderr("# can't write %s\n", cpathname); |
55 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION); |
45 + PySys_WriteStderr("# can't write %s\n", tmppathname); |
56 fflush(fp); |
46 /* Don't keep partial file */ |
57 fclose(fp); |
47 fclose(fp); |
58 + /* Delete the old compiled file, if exists */ |
48 - (void) unlink(cpathname); |
59 + if (unlink (cpathname)) { |
49 + (void) unlink(tmppathname); |
60 + if ((errno != ENOENT)) { |
50 + PyMem_Free(tmppathname); |
61 + /* the file exists but could not be deleted */ |
51 return; |
62 + if (Py_VerboseFlag) |
52 } |
63 + PySys_WriteStderr( |
53 /* Now write the true mtime */ |
64 + "# can't unlink %s\n", cpathname); |
54 @@ -907,8 +919,29 @@ |
65 + (void) unlink(tmppathname); |
55 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION); |
66 + PyMem_Free(tmppathname); |
56 fflush(fp); |
67 + return; |
57 fclose(fp); |
68 + } |
58 + /* Delete the old compiled file, if exists */ |
69 + } |
59 + if (unlink (cpathname)) { |
70 + /* rename the tmp file to the real file name */ |
60 + if ((errno != ENOENT)) { |
71 + if (rename (tmppathname, cpathname)) { |
61 + /* the file exists but could not be deleted */ |
72 + if (Py_VerboseFlag) |
62 + if (Py_VerboseFlag) |
73 + PySys_WriteStderr( |
63 + PySys_WriteStderr("# can't unlink %s\n", cpathname); |
74 + "# can't rename %s to %s\n", tmppathname, cpathname); |
64 + (void) unlink(tmppathname); |
75 + (void) unlink(tmppathname); |
65 + PyMem_Free(tmppathname); |
76 + PyMem_Free(tmppathname); |
66 + return; |
77 + return; |
67 + } |
78 + } |
68 + } |
79 if (Py_VerboseFlag) |
69 + /* rename the tmp file to the real file name */ |
80 PySys_WriteStderr("# wrote %s\n", cpathname); |
70 + if (rename (tmppathname, cpathname)) { |
81 + PyMem_Free(tmppathname); |
71 + if (Py_VerboseFlag) |
|
72 + PySys_WriteStderr( |
|
73 + "# can't rename %s to %s\n", tmppathname, cpathname); |
|
74 + (void) unlink(tmppathname); |
|
75 + PyMem_Free(tmppathname); |
|
76 + return; |
|
77 + } |
|
78 if (Py_VerboseFlag) |
|
79 PySys_WriteStderr("# wrote %s\n", cpathname); |
|
80 + PyMem_Free(tmppathname); |
82 } |
81 } |
83 |
82 |
84 static void |
83 static void |