components/python/python26/patches/Python26-06-write_compiled_module-atomic.patch
author Rich Burridge <rich.burridge@oracle.com>
Wed, 30 May 2012 12:40:14 -0700
changeset 841 1a62cefa636d
parent 115 c360825c3a3f
permissions -rw-r--r--
7121707 python 2.6.4 should be updated to latest 2.6.X release
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
99
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     1
diff --git Python-2.6.4/Python/import.c Python-2.6.4/Python/import.c
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     2
--- Python-2.6.4/Python/import.c
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     3
+++ Python-2.6.4/Python/import.c
841
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
     4
@@ -868,8 +868,9 @@
99
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     5
 
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     6
 /* Write a compiled module to a file, placing the time of last
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     7
    modification of its source into the header.
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     8
-   Errors are ignored, if a write error occurs an attempt is made to
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
     9
-   remove the file. */
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    10
+   Write to a temporary file first so that creating the file is atomic.
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    11
+   Errors are ignored, if a write/unlink/rename error occurs an attempt
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    12
+   is made to remove the temporary file. */
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    13
 
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    14
 static void
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    15
 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
841
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    16
@@ -882,11 +883,21 @@
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    17
     mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    18
 #endif
99
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    19
 
841
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    20
-    fp = open_exclusive(cpathname, mode);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    21
+    char *tmppathname;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    22
+
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    23
+    /* the temporary file is called cpathname + ".tmp" */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    24
+    if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1))
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    25
+        == NULL) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    26
+        return;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    27
+    }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    28
+    sprintf (tmppathname, "%s.tmp", cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    29
+    fp = open_exclusive(tmppathname, mode);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    30
+
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    31
     if (fp == NULL) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    32
         if (Py_VerboseFlag)
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    33
             PySys_WriteStderr(
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    34
-                "# can't create %s\n", cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    35
+                "# can't create %s\n", tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    36
+        PyMem_Free(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    37
         return;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    38
     }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    39
     PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    40
@@ -895,10 +906,11 @@
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    41
     PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    42
     if (fflush(fp) != 0 || ferror(fp)) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    43
         if (Py_VerboseFlag)
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    44
-            PySys_WriteStderr("# can't write %s\n", cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    45
+            PySys_WriteStderr("# can't write %s\n", tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    46
         /* Don't keep partial file */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    47
         fclose(fp);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    48
-        (void) unlink(cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    49
+        (void) unlink(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    50
+        PyMem_Free(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    51
         return;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    52
     }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    53
     /* Now write the true mtime */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    54
@@ -907,8 +919,29 @@
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    55
     PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    56
     fflush(fp);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    57
     fclose(fp);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    58
+    /* Delete the old compiled file, if exists */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    59
+    if (unlink (cpathname)) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    60
+        if ((errno != ENOENT)) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    61
+            /* the file exists but could not be deleted */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    62
+            if (Py_VerboseFlag)
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    63
+                PySys_WriteStderr("# can't unlink %s\n", cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    64
+            (void) unlink(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    65
+            PyMem_Free(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    66
+            return;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    67
+        }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    68
+    }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    69
+    /* rename the tmp file to the real file name */
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    70
+    if (rename (tmppathname, cpathname)) {
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    71
+        if (Py_VerboseFlag)
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    72
+            PySys_WriteStderr(
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    73
+                "# can't rename %s to %s\n", tmppathname, cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    74
+        (void) unlink(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    75
+        PyMem_Free(tmppathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    76
+        return;
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    77
+    }
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    78
     if (Py_VerboseFlag)
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    79
         PySys_WriteStderr("# wrote %s\n", cpathname);
1a62cefa636d 7121707 python 2.6.4 should be updated to latest 2.6.X release
Rich Burridge <rich.burridge@oracle.com>
parents: 115
diff changeset
    80
+    PyMem_Free(tmppathname);
99
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    81
 }
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    82
 
c15c9099bb44 6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff changeset
    83
 static void