]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tokyocabinet/compat.c
Correct typo in new features
[bacula/bacula] / bacula / src / lib / tokyocabinet / compat.c
1 /*************************************************************************************************
2  * The compat WIN32 API of Tokyo Cabinet
3  *                                                      Copyright (C) 2006-2008 Mikio Hirabayashi
4  * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
5  * the GNU Lesser General Public License as published by the Free Software Foundation; either
6  * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope
7  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
9  * License for more details.
10  * You should have received a copy of the GNU Lesser General Public License along with Tokyo
11  * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
12  * Boston, MA 02111-1307 USA.
13  *************************************************************************************************/
14
15 #ifdef HAVE_WIN32
16
17 #include "tcutil.h"
18 #include "myconf.h"
19 #include <windef.h>
20 #include <winbase.h>
21
22 int msync(void *start, size_t length, int flags)
23 {
24    return 0;
25 }
26 int fsync(int fd)
27 {
28    return 0;
29 }
30
31 /*
32  * Keep track of HANDLE for CreateView
33  */
34
35 struct mmap_priv
36 {
37    HANDLE h;
38    HANDLE mv;
39 };
40
41 static TCLIST *mmap_list=NULL;
42
43 static void mmap_register(HANDLE h, HANDLE mv)
44 {
45    struct mmap_priv *priv;
46 /*
47    if (!tcglobalmutexlock()) {
48       return;
49    }
50 */
51    if (!mmap_list) {
52       mmap_list = tclistnew();
53    }
54
55    priv = tcmalloc(sizeof(struct mmap_priv));
56    priv->h = h;
57    priv->mv = mv;
58    tclistpush(mmap_list, priv, sizeof(priv));
59 /*
60    tcglobalmutexunlock();
61 */
62 }
63
64 static HANDLE mmap_unregister(HANDLE mv)
65 {
66    HANDLE h=NULL;
67    struct mmap_priv *priv;
68    int i, max, size;
69    int nok=1;
70 /*
71    if (!tcglobalmutexlock()) {
72       return NULL;
73    }
74 */ 
75    max = tclistnum(mmap_list);
76    for(i=0; nok && i<max; i++) {
77       priv = (struct mmap_priv *)tclistval(mmap_list, i, &size);
78       if (priv && (priv->mv == mv)) {
79          tclistremove(mmap_list, i, &size);
80          h = priv->h;
81          tcfree(mmap_list);
82          nok=0;
83       }
84    }
85 /*
86    tcglobalmutexunlock();
87 */
88    return h;
89 }
90
91
92 /*
93  * Emulation of mmap and unmmap for tokyo dbm
94  */
95 void *mmap(void *start, size_t length, int prot, int flags,
96            int fd, off_t offset)
97 {
98    DWORD fm_access = 0;
99    DWORD mv_access = 0;
100    HANDLE h;
101    HANDLE mv;
102
103    if (length == 0) {
104       return MAP_FAILED;
105    }
106    if (!fd) {
107       return MAP_FAILED;
108    }
109
110    if (flags & PROT_WRITE) {
111       fm_access |= PAGE_READWRITE;
112    } else if (flags & PROT_READ) {
113       fm_access |= PAGE_READONLY;
114    }
115
116    if (flags & PROT_READ) {
117       mv_access |= FILE_MAP_READ;
118    }
119    if (flags & PROT_WRITE) {
120       mv_access |= FILE_MAP_WRITE;
121    }
122
123    h = CreateFileMapping((HANDLE)_get_osfhandle (fd),
124                          NULL /* security */,
125                          fm_access,
126                          0 /* MaximumSizeHigh */,
127                          0 /* MaximumSizeLow */,
128                          NULL /* name of the file mapping object */);
129
130    if (!h || h == INVALID_HANDLE_VALUE) {
131       return MAP_FAILED;
132    }
133
134    mv = MapViewOfFile(h, mv_access,
135                       0 /* offset hi */,
136                       0 /* offset lo */,
137                       length);
138    if (!mv || mv == INVALID_HANDLE_VALUE) {
139       CloseHandle(h);
140       return MAP_FAILED;
141    }
142
143    mmap_register(h, mv);
144    return (void *) mv;
145 }
146
147 int munmap(void *start, size_t length)
148 {
149    HANDLE h;
150    if (!start) {
151       return -1;
152    }
153    h = mmap_unregister(start);
154    UnmapViewOfFile(start);
155    CloseHandle(h);
156    return 0;
157 }
158
159 int regcomp(regex_t *preg, const char *regex, int cflags)
160 {
161    return 0;
162 }
163
164 int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
165 {
166    return 0;
167 }
168
169 size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
170 {
171    *errbuf = 0;
172    return 0;
173 }
174 /*
175 int gettimeofday(struct timeval *tv, struct timezone *tz)
176 {
177    return -1;
178 }
179 */
180 void regfree(regex_t *preg)
181 {
182 }
183
184 int glob(const char *pattern, int flags,
185          int (*errfunc) (const char *epath, int eerrno),
186          glob_t *pglob)
187 {
188    return GLOB_NOMATCH;
189 }
190 void globfree(glob_t *pglob)
191 {
192 }
193
194 char *realpath(const char *path, char *resolved_path)
195 {
196    strcpy(resolved_path, path);
197    return resolved_path;
198 }
199
200 /*
201 int rand_r(unsigned int *seedp)
202 {
203    *seedp = *seedp * 1103515245 + 12345;
204    return((unsigned)(*seedp/65536) % 32768);
205 }
206 */
207
208 /* read from a file descriptor at a given offset */
209 ssize_t pread(int fd, void *buf, size_t count, off_t offset)
210 {
211    __int64 cur_pos;
212    ssize_t num_read;
213
214    if ((cur_pos = _lseeki64(fd, 0, SEEK_CUR)) == (off_t)-1)
215       return -1;
216
217    if (_lseeki64(fd, offset, SEEK_SET) == (off_t)-1)
218       return -1;
219
220    num_read = read(fd, buf, count);
221
222    if (_lseeki64(fd, cur_pos, SEEK_SET) == (off_t)-1)
223       return -1;
224
225    return num_read;
226 }
227
228 /* write to a file descriptor at a given offset */
229 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
230 {
231    __int64 cur_pos;
232    ssize_t num_write;
233
234    if ((cur_pos = _lseeki64(fd, 0, SEEK_CUR)) == (off_t)-1)
235       return -1;
236
237    if (_lseeki64(fd, offset, SEEK_SET) == (off_t)-1)
238       return -1;
239
240    num_write = write(fd, buf, count);
241
242    if (_lseeki64(fd, cur_pos, SEEK_SET) == (off_t)-1)
243       return -1;
244
245    return num_write;
246 }
247
248 #endif