New PHP5 APC - version 3.0.19, using PHP5 5.2.0-8+etch11,
[php5-apc.git] / apc_fcntl_win32.c
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2008 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: George Schlossnagle <george@omniti.com>                     |
16   |          Edin Kadribasic <edink@php.net>                             |
17   +----------------------------------------------------------------------+
18
19    This software was contributed to PHP by Community Connect Inc. in 2002
20    and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
21    Future revisions and derivatives of this source code must acknowledge
22    Community Connect Inc. as the original contributor of this module by
23    leaving this note intact in the source code.
24
25    All other licensing and usage conditions are those of the PHP Group.
26
27  */
28
29 /* $Id: apc_fcntl_win32.c,v 3.6.2.1 2008/05/11 18:57:00 rasmus Exp $ */
30
31 #include "apc_fcntl.h"
32 #include "apc.h"
33 #include <php.h>
34 #include <win32/flock.h>
35 #include <io.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 int apc_fcntl_create(const char* pathname)
41 {
42         char *lock_file = emalloc(MAXPATHLEN);
43         HANDLE fd;
44         DWORD tmplen;
45         static int i=0;
46         
47         tmplen = GetTempPath(MAXPATHLEN, lock_file);
48         if (!tmplen) {
49                 efree(lock_file);
50                 return -1;
51         }
52
53         snprintf(lock_file + tmplen, MAXPATHLEN - tmplen - 1, "apc.lock.%d", i++);
54         
55         fd = CreateFile(lock_file,
56         GENERIC_READ | GENERIC_WRITE,
57         FILE_SHARE_READ | FILE_SHARE_WRITE,
58         NULL,
59         OPEN_ALWAYS,
60         FILE_ATTRIBUTE_NORMAL,
61         NULL);
62         
63
64         if (fd == INVALID_HANDLE_VALUE) {
65                 apc_eprint("apc_fcntl_create: could not open %s", lock_file);
66                 efree(lock_file);
67                 return -1;
68         }
69         
70         efree(lock_file);
71         return (int)fd;
72 }
73
74 void apc_fcntl_destroy(int fd)
75 {
76         CloseHandle((HANDLE)fd);
77 }
78
79 void apc_fcntl_lock(int fd)
80 {
81         OVERLAPPED offset =     {0, 0, 0, 0, NULL};
82         
83         if (!LockFileEx((HANDLE)fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset)) {
84                 apc_eprint("apc_fcntl_lock failed errno:%d", GetLastError());
85         }
86 }
87
88 void apc_fcntl_rdlock(int fd)
89 {
90         OVERLAPPED offset =     {0, 0, 0, 0, NULL};
91         
92         if (!LockFileEx((HANDLE)fd, 0, 0, 1, 0, &offset)) {
93                 apc_eprint("apc_fcntl_rdlock failed errno:%d", GetLastError());
94         }
95 }
96
97 void apc_fcntl_unlock(int fd)
98 {
99         OVERLAPPED offset =     {0, 0, 0, 0, NULL};
100
101         if (!UnlockFileEx((HANDLE)fd, 0, 1, 0, &offset)) {
102                 DWORD error_code = GetLastError();
103                 /* Ignore already unlocked error */
104                 if (error_code != ERROR_NOT_LOCKED) {
105                         apc_eprint("apc_fcntl_unlock failed errno:%d", error_code);
106                 }
107         }
108 }
109
110 /*
111  * Local variables:
112  * tab-width: 4
113  * c-basic-offset: 4
114  * End:
115  * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
116  * vim<600: expandtab sw=4 ts=4 sts=4
117  */