]> git.decadent.org.uk Git - dak.git/blob - contrib/python_1.5.2-fcntl_lockf.diff
fix -r/--regex to actually work
[dak.git] / contrib / python_1.5.2-fcntl_lockf.diff
1 --- python-1.5.2/Modules/fcntlmodule.c.orig     Wed Jan  6 13:44:23 1999
2 +++ python-1.5.2/Modules/fcntlmodule.c  Sun Apr  1 07:42:54 2001
3 @@ -233,30 +233,12 @@
4  {
5         int fd, code, ret, whence = 0;
6         PyObject *lenobj = NULL, *startobj = NULL;
7 +       struct flock l;
8  
9         if (!PyArg_ParseTuple(args, "ii|OOi", &fd, &code,
10                               &lenobj, &startobj, &whence))
11             return NULL;
12  
13 -#ifndef LOCK_SH
14 -#define LOCK_SH                1       /* shared lock */
15 -#define LOCK_EX                2       /* exclusive lock */
16 -#define LOCK_NB                4       /* don't block when locking */
17 -#define LOCK_UN                8       /* unlock */
18 -#endif
19 -       {
20 -               struct flock l;
21 -               if (code == LOCK_UN)
22 -                       l.l_type = F_UNLCK;
23 -               else if (code & LOCK_SH)
24 -                       l.l_type = F_RDLCK;
25 -               else if (code & LOCK_EX)
26 -                       l.l_type = F_WRLCK;
27 -               else {
28 -                       PyErr_SetString(PyExc_ValueError,
29 -                                       "unrecognized flock argument");
30 -                       return NULL;
31 -               }
32                 l.l_start = l.l_len = 0;
33                 if (startobj != NULL) {
34  #if !defined(HAVE_LARGEFILE_SUPPORT)
35 @@ -281,10 +263,45 @@
36                                 return NULL;
37                 }
38                 l.l_whence = whence;
39 +       switch (code)
40 +         {
41 +         case F_TEST:
42 +           /* Test the lock: return 0 if FD is unlocked or locked by this process;
43 +              return -1, set errno to EACCES, if another process holds the lock.  */
44 +           if (fcntl (fd, F_GETLK, &l) < 0) {
45 +             PyErr_SetFromErrno(PyExc_IOError);
46 +             return NULL;
47 +           }
48 +           if (l.l_type == F_UNLCK || l.l_pid == getpid ()) {
49 +             Py_INCREF(Py_None);
50 +             return Py_None;
51 +           }
52 +           errno = EACCES;
53 +           PyErr_SetFromErrno(PyExc_IOError);
54 +           return NULL;
55 +
56 +         case F_ULOCK:
57 +           l.l_type = F_UNLCK;
58 +           code = F_SETLK;
59 +           break;
60 +         case F_LOCK:
61 +           l.l_type = F_WRLCK;
62 +           code = F_SETLKW;
63 +           break;
64 +         case F_TLOCK:
65 +           l.l_type = F_WRLCK;
66 +           code = F_SETLK;
67 +           break;
68 +
69 +         default:
70 +           PyErr_SetString(PyExc_ValueError,
71 +                           "unrecognized flock argument");
72 +           return NULL;
73 +         }
74                 Py_BEGIN_ALLOW_THREADS
75 -               ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
76 +         ret = fcntl(fd, code, &l);
77                 Py_END_ALLOW_THREADS
78 -       }
79 +
80         if (ret < 0) {
81                 PyErr_SetFromErrno(PyExc_IOError);
82                 return NULL;