ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.120 by root, Tue Apr 24 18:47:50 2012 UTC vs.
Revision 1.126 by root, Fri Dec 28 07:33:41 2012 UTC

4 * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libeio@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright notice, 10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 11 * this list of conditions and the following disclaimer.
12 * 12 *
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
132 #define sync() EIO_ENOSYS () 132 #define sync() EIO_ENOSYS ()
133 #define readlink(path,buf,s) EIO_ENOSYS () 133 #define readlink(path,buf,s) EIO_ENOSYS ()
134 #define statvfs(path,buf) EIO_ENOSYS () 134 #define statvfs(path,buf) EIO_ENOSYS ()
135 #define fstatvfs(fd,buf) EIO_ENOSYS () 135 #define fstatvfs(fd,buf) EIO_ENOSYS ()
136 136
137 #define pread(fd,buf,count,offset) eio__pread (fd, buf, count, offset)
138 #define pwrite(fd,buf,count,offset) eio__pwrite (fd, buf, count, offset)
139
140 #if __GNUC__
141 typedef long long eio_off_t; /* signed for compatibility to msvc */
142 #else
143 typedef __int64 eio_off_t; /* unsigned not supported by msvc */
144 #endif
145
146 static eio_ssize_t
147 eio__pread (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
148 {
149 OVERLAPPED o = { 0 };
150 DWORD got;
151
152 o.Offset = offset;
153 o.OffsetHigh = offset >> 32;
154
155 return ReadFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
156 ? got : -1;
157 }
158
159 static eio_ssize_t
160 eio__pwrite (int fd, void *buf, eio_ssize_t count, eio_off_t offset)
161 {
162 OVERLAPPED o = { 0 };
163 DWORD got;
164
165 o.Offset = offset;
166 o.OffsetHigh = offset >> 32;
167
168 return WriteFile ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd), buf, count, &got, &o)
169 ? got : -1;
170 }
171
137 /* rename() uses MoveFile, which fails to overwrite */ 172 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu) 173 #define rename(old,neu) eio__rename (old, neu)
139 174
140 static int 175 static int
141 eio__rename (const char *old, const char *neu) 176 eio__rename (const char *old, const char *neu)
281#endif 316#endif
282 317
283/* buffer size for various temporary buffers */ 318/* buffer size for various temporary buffers */
284#define EIO_BUFSIZE 65536 319#define EIO_BUFSIZE 65536
285 320
286#define dBUF \ 321#define dBUF \
287 char *eio_buf = malloc (EIO_BUFSIZE); \ 322 char *eio_buf = malloc (EIO_BUFSIZE); \
288 errno = ENOMEM; \ 323 errno = ENOMEM; \
289 if (!eio_buf) \ 324 if (!eio_buf) \
290 return -1 325 return -1
291 326
379static xmutex_t wrklock; 414static xmutex_t wrklock;
380static xmutex_t reslock; 415static xmutex_t reslock;
381static xmutex_t reqlock; 416static xmutex_t reqlock;
382static xcond_t reqwait; 417static xcond_t reqwait;
383 418
384#if !HAVE_PREADWRITE
385/*
386 * make our pread/pwrite emulation safe against themselves, but not against
387 * normal read/write by using a mutex. slows down execution a lot,
388 * but that's your problem, not mine.
389 */
390static xmutex_t preadwritelock;
391#endif
392
393typedef struct etp_worker 419typedef struct etp_worker
394{ 420{
395 struct tmpbuf tmpbuf; 421 struct tmpbuf tmpbuf;
396 422
397 /* locked by wrklock */ 423 /* locked by wrklock */
576 /*TODO*/ 602 /*TODO*/
577 assert (("unable to allocate worker thread data", wrk)); 603 assert (("unable to allocate worker thread data", wrk));
578 604
579 X_LOCK (wrklock); 605 X_LOCK (wrklock);
580 606
581 if (thread_create (&wrk->tid, etp_proc, (void *)wrk)) 607 if (xthread_create (&wrk->tid, etp_proc, (void *)wrk))
582 { 608 {
583 wrk->prev = &wrk_first; 609 wrk->prev = &wrk_first;
584 wrk->next = wrk_first.next; 610 wrk->next = wrk_first.next;
585 wrk_first.next->prev = wrk; 611 wrk_first.next->prev = wrk;
586 wrk_first.next = wrk; 612 wrk_first.next = wrk;
943} 969}
944 970
945/*****************************************************************************/ 971/*****************************************************************************/
946/* work around various missing functions */ 972/* work around various missing functions */
947 973
948#if !HAVE_PREADWRITE
949# undef pread
950# undef pwrite
951# define pread eio__pread
952# define pwrite eio__pwrite
953
954static eio_ssize_t
955eio__pread (int fd, void *buf, size_t count, off_t offset)
956{
957 eio_ssize_t res;
958 off_t ooffset;
959
960 X_LOCK (preadwritelock);
961 ooffset = lseek (fd, 0, SEEK_CUR);
962 lseek (fd, offset, SEEK_SET);
963 res = read (fd, buf, count);
964 lseek (fd, ooffset, SEEK_SET);
965 X_UNLOCK (preadwritelock);
966
967 return res;
968}
969
970static eio_ssize_t
971eio__pwrite (int fd, void *buf, size_t count, off_t offset)
972{
973 eio_ssize_t res;
974 off_t ooffset;
975
976 X_LOCK (preadwritelock);
977 ooffset = lseek (fd, 0, SEEK_CUR);
978 lseek (fd, offset, SEEK_SET);
979 res = write (fd, buf, count);
980 lseek (fd, ooffset, SEEK_SET);
981 X_UNLOCK (preadwritelock);
982
983 return res;
984}
985#endif
986
987#ifndef HAVE_UTIMES 974#ifndef HAVE_UTIMES
988 975
989# undef utimes 976# undef utimes
990# define utimes(path,times) eio__utimes (path, times) 977# define utimes(path,times) eio__utimes (path, times)
991 978
1429 1416
1430 if (fd >= 0) 1417 if (fd >= 0)
1431 { 1418 {
1432 sprintf (tmp1, "/proc/self/fd/%d", fd); 1419 sprintf (tmp1, "/proc/self/fd/%d", fd);
1433 req->result = readlink (tmp1, res, PATH_MAX); 1420 req->result = readlink (tmp1, res, PATH_MAX);
1421 /* here we should probably stat the open file and the disk file, to make sure they still match */
1434 close (fd); 1422 close (fd);
1435
1436 /* here we should probably stat the open file and the disk file, to make sure they still match */
1437 1423
1438 if (req->result > 0) 1424 if (req->result > 0)
1439 goto done; 1425 goto done;
1440 } 1426 }
1441 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) 1427 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1442 return; 1428 return -1;
1443 } 1429 }
1444#endif 1430#endif
1445#endif 1431#endif
1446 1432
1447 if (*rel != '/') 1433 if (*rel != '/')
1931 #ifdef DT_FIFO 1917 #ifdef DT_FIFO
1932 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1918 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1933 #endif 1919 #endif
1934 #ifdef DT_CHR 1920 #ifdef DT_CHR
1935 case DT_CHR: ent->type = EIO_DT_CHR; break; 1921 case DT_CHR: ent->type = EIO_DT_CHR; break;
1936 #endif 1922 #endif
1937 #ifdef DT_MPC 1923 #ifdef DT_MPC
1938 case DT_MPC: ent->type = EIO_DT_MPC; break; 1924 case DT_MPC: ent->type = EIO_DT_MPC; break;
1939 #endif 1925 #endif
1940 #ifdef DT_DIR 1926 #ifdef DT_DIR
1941 case DT_DIR: ent->type = EIO_DT_DIR; break; 1927 case DT_DIR: ent->type = EIO_DT_DIR; break;
1942 #endif 1928 #endif
1943 #ifdef DT_NAM 1929 #ifdef DT_NAM
1944 case DT_NAM: ent->type = EIO_DT_NAM; break; 1930 case DT_NAM: ent->type = EIO_DT_NAM; break;
1945 #endif 1931 #endif
1946 #ifdef DT_BLK 1932 #ifdef DT_BLK
1947 case DT_BLK: ent->type = EIO_DT_BLK; break; 1933 case DT_BLK: ent->type = EIO_DT_BLK; break;
1948 #endif 1934 #endif
1949 #ifdef DT_MPB 1935 #ifdef DT_MPB
1950 case DT_MPB: ent->type = EIO_DT_MPB; break; 1936 case DT_MPB: ent->type = EIO_DT_MPB; break;
1951 #endif 1937 #endif
1952 #ifdef DT_REG 1938 #ifdef DT_REG
1953 case DT_REG: ent->type = EIO_DT_REG; break; 1939 case DT_REG: ent->type = EIO_DT_REG; break;
1954 #endif 1940 #endif
1955 #ifdef DT_NWK 1941 #ifdef DT_NWK
1956 case DT_NWK: ent->type = EIO_DT_NWK; break; 1942 case DT_NWK: ent->type = EIO_DT_NWK; break;
1957 #endif 1943 #endif
1958 #ifdef DT_CMP 1944 #ifdef DT_CMP
1959 case DT_CMP: ent->type = EIO_DT_CMP; break; 1945 case DT_CMP: ent->type = EIO_DT_CMP; break;
1960 #endif 1946 #endif
1961 #ifdef DT_LNK 1947 #ifdef DT_LNK
1962 case DT_LNK: ent->type = EIO_DT_LNK; break; 1948 case DT_LNK: ent->type = EIO_DT_LNK; break;
1963 #endif 1949 #endif
1964 #ifdef DT_SOCK 1950 #ifdef DT_SOCK
1965 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1951 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
2240 free (req); 2226 free (req);
2241 2227
2242 X_LOCK (wrklock); 2228 X_LOCK (wrklock);
2243 etp_worker_free (self); 2229 etp_worker_free (self);
2244 X_UNLOCK (wrklock); 2230 X_UNLOCK (wrklock);
2231
2232 return 0;
2245} 2233}
2246 2234
2247/*****************************************************************************/ 2235/*****************************************************************************/
2248 2236
2249int ecb_cold 2237int ecb_cold
2250eio_init (void (*want_poll)(void), void (*done_poll)(void)) 2238eio_init (void (*want_poll)(void), void (*done_poll)(void))
2251{ 2239{
2252#if !HAVE_PREADWRITE
2253 X_MUTEX_CREATE (preadwritelock);
2254#endif
2255
2256 return etp_init (want_poll, done_poll); 2240 return etp_init (want_poll, done_poll);
2257} 2241}
2258 2242
2259ecb_inline void 2243ecb_inline void
2260eio_api_destroy (eio_req *req) 2244eio_api_destroy (eio_req *req)
2261{ 2245{
2262 free (req); 2246 free (req);
2263} 2247}
2264 2248
2265#define REQ(rtype) \ 2249#define REQ(rtype) \
2266 eio_req *req; \ 2250 eio_req *req; \
2267 \ 2251 \
2268 req = (eio_req *)calloc (1, sizeof *req); \ 2252 req = (eio_req *)calloc (1, sizeof *req); \
2269 if (!req) \ 2253 if (!req) \
2270 return 0; \ 2254 return 0; \
2284 { \ 2268 { \
2285 eio_api_destroy (req); \ 2269 eio_api_destroy (req); \
2286 return 0; \ 2270 return 0; \
2287 } 2271 }
2288 2272
2273#define SINGLEDOT(ptr) (0[(char *)(ptr)] == '.' && !1[(char *)(ptr)])
2274
2289static void 2275static void
2290eio_execute (etp_worker *self, eio_req *req) 2276eio_execute (etp_worker *self, eio_req *req)
2291{ 2277{
2292#if HAVE_AT 2278#if HAVE_AT
2293 int dirfd; 2279 int dirfd;
2348 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; 2334 case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break;
2349 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; 2335 case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break;
2350 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 2336 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2351 2337
2352 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 2338 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2353 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 2339 case EIO_RMDIR: /* complications arise because "." cannot be removed, so we might have to expand */
2340 req->result = req->wd && SINGLEDOT (req->ptr1)
2341 ? rmdir (req->wd->str)
2342 : unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2354 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 2343 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2355 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; 2344 case EIO_RENAME: /* complications arise because "." cannot be renamed, so we might have to expand */
2345 req->result = req->wd && SINGLEDOT (req->ptr1)
2346 ? rename (req->wd->str, req->ptr2)
2347 : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2356 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; 2348 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2357 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 2349 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2358 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2350 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2359 case EIO_READLINK: ALLOC (PATH_MAX); 2351 case EIO_READLINK: ALLOC (PATH_MAX);
2360 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 2352 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines