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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.105 by root, Mon Sep 26 18:23:18 2011 UTC vs.
Revision 1.123 by root, Tue Oct 9 04:53:53 2012 UTC

1/* 1/*
2 * libeio implementation 2 * libeio implementation
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010,2011 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 *
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)
208 #endif 243 #endif
209 244
210 #define D_NAME(entp) entp->d_name 245 #define D_NAME(entp) entp->d_name
211 246
212 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 247 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
213 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 248 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
214 #define _DIRENT_HAVE_D_TYPE /* sigh */ 249 #define _DIRENT_HAVE_D_TYPE /* sigh */
215 #define D_INO(de) (de)->d_fileno 250 #define D_INO(de) (de)->d_fileno
216 #define D_NAMLEN(de) (de)->d_namlen 251 #define D_NAMLEN(de) (de)->d_namlen
217 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 252 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
218 #define D_INO(de) (de)->d_ino 253 #define D_INO(de) (de)->d_ino
312 } 347 }
313 348
314 return buf->ptr; 349 return buf->ptr;
315} 350}
316 351
352struct tmpbuf;
353
354#if _POSIX_VERSION >= 200809L
355 #define HAVE_AT 1
356 #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD)
357 #ifndef O_SEARCH
358 #define O_SEARCH O_RDONLY
359 #endif
360#else
361 #define HAVE_AT 0
362 static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path);
363#endif
364
365struct eio_pwd
366{
367#if HAVE_AT
368 int fd;
369#endif
370 int len;
371 char str[1]; /* actually, a 0-terminated canonical path */
372};
373
317/*****************************************************************************/ 374/*****************************************************************************/
318 375
319#define ETP_PRI_MIN EIO_PRI_MIN 376#define ETP_PRI_MIN EIO_PRI_MIN
320#define ETP_PRI_MAX EIO_PRI_MAX 377#define ETP_PRI_MAX EIO_PRI_MAX
321 378
357static xmutex_t wrklock; 414static xmutex_t wrklock;
358static xmutex_t reslock; 415static xmutex_t reslock;
359static xmutex_t reqlock; 416static xmutex_t reqlock;
360static xcond_t reqwait; 417static xcond_t reqwait;
361 418
362#if !HAVE_PREADWRITE
363/*
364 * make our pread/pwrite emulation safe against themselves, but not against
365 * normal read/write by using a mutex. slows down execution a lot,
366 * but that's your problem, not mine.
367 */
368static xmutex_t preadwritelock;
369#endif
370
371typedef struct etp_worker 419typedef struct etp_worker
372{ 420{
373 struct tmpbuf tmpbuf; 421 struct tmpbuf tmpbuf;
374 422
375 /* locked by wrklock */ 423 /* locked by wrklock */
387#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 435#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
388#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 436#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
389 437
390/* worker threads management */ 438/* worker threads management */
391 439
392static void ecb_cold 440static void
393etp_worker_clear (etp_worker *wrk) 441etp_worker_clear (etp_worker *wrk)
394{ 442{
395} 443}
396 444
397static void ecb_cold 445static void ecb_cold
921} 969}
922 970
923/*****************************************************************************/ 971/*****************************************************************************/
924/* work around various missing functions */ 972/* work around various missing functions */
925 973
926#if !HAVE_PREADWRITE
927# undef pread
928# undef pwrite
929# define pread eio__pread
930# define pwrite eio__pwrite
931
932static eio_ssize_t
933eio__pread (int fd, void *buf, size_t count, off_t offset)
934{
935 eio_ssize_t res;
936 off_t ooffset;
937
938 X_LOCK (preadwritelock);
939 ooffset = lseek (fd, 0, SEEK_CUR);
940 lseek (fd, offset, SEEK_SET);
941 res = read (fd, buf, count);
942 lseek (fd, ooffset, SEEK_SET);
943 X_UNLOCK (preadwritelock);
944
945 return res;
946}
947
948static eio_ssize_t
949eio__pwrite (int fd, void *buf, size_t count, off_t offset)
950{
951 eio_ssize_t res;
952 off_t ooffset;
953
954 X_LOCK (preadwritelock);
955 ooffset = lseek (fd, 0, SEEK_CUR);
956 lseek (fd, offset, SEEK_SET);
957 res = write (fd, buf, count);
958 lseek (fd, ooffset, SEEK_SET);
959 X_UNLOCK (preadwritelock);
960
961 return res;
962}
963#endif
964
965#ifndef HAVE_UTIMES 974#ifndef HAVE_UTIMES
966 975
967# undef utimes 976# undef utimes
968# define utimes(path,times) eio__utimes (path, times) 977# define utimes(path,times) eio__utimes (path, times)
969 978
1010 int res; 1019 int res;
1011 1020
1012#if HAVE_SYS_SYNCFS 1021#if HAVE_SYS_SYNCFS
1013 res = (int)syscall (__NR_syncfs, (int)(fd)); 1022 res = (int)syscall (__NR_syncfs, (int)(fd));
1014#else 1023#else
1015 res = -1; 1024 res = EIO_ENOSYS ();
1016 errno = ENOSYS;
1017#endif 1025#endif
1018 1026
1019 if (res < 0 && errno == ENOSYS && fd >= 0) 1027 if (res < 0 && errno == ENOSYS && fd >= 0)
1020 sync (); 1028 sync ();
1021 1029
1051} 1059}
1052 1060
1053static int 1061static int
1054eio__fallocate (int fd, int mode, off_t offset, size_t len) 1062eio__fallocate (int fd, int mode, off_t offset, size_t len)
1055{ 1063{
1056#if HAVE_FALLOCATE 1064#if HAVE_LINUX_FALLOCATE
1057 return fallocate (fd, mode, offset, len); 1065 return fallocate (fd, mode, offset, len);
1058#else 1066#else
1059 errno = ENOSYS; 1067 return EIO_ENOSYS ();
1060 return -1;
1061#endif 1068#endif
1062} 1069}
1063 1070
1064#if !HAVE_READAHEAD 1071#if !HAVE_READAHEAD
1065# undef readahead 1072# undef readahead
1080 todo -= len; 1087 todo -= len;
1081 } 1088 }
1082 1089
1083 FUBd; 1090 FUBd;
1084 1091
1085 errno = 0; 1092 /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */
1093 /* but not for e.g. EIO or eof, so we also never fail */
1086 return count; 1094 return 0;
1087} 1095}
1088 1096
1089#endif 1097#endif
1090 1098
1091/* sendfile always needs emulation */ 1099/* sendfile always needs emulation */
1126 1134
1127 /* according to source inspection, this is correct, and useful behaviour */ 1135 /* according to source inspection, this is correct, and useful behaviour */
1128 if (sbytes) 1136 if (sbytes)
1129 res = sbytes; 1137 res = sbytes;
1130 1138
1131# elif defined (__APPLE__) 1139# elif defined __APPLE__
1132 off_t sbytes = count; 1140 off_t sbytes = count;
1133 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1141 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
1134 1142
1135 /* according to the manpage, sbytes is always valid */ 1143 /* according to the manpage, sbytes is always valid */
1136 if (sbytes) 1144 if (sbytes)
1163 HANDLE h = TO_SOCKET (ifd); 1171 HANDLE h = TO_SOCKET (ifd);
1164 SetFilePointer (h, offset, 0, FILE_BEGIN); 1172 SetFilePointer (h, offset, 0, FILE_BEGIN);
1165 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1173 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1166 1174
1167#else 1175#else
1168 res = -1; 1176 res = EIO_ENOSYS ();
1169 errno = ENOSYS;
1170#endif 1177#endif
1171 1178
1172 /* we assume sendfile can copy at least 128mb in one go */ 1179 /* we assume sendfile can copy at least 128mb in one go */
1173 if (res <= 128 * 1024 * 1024) 1180 if (res <= 128 * 1024 * 1024)
1174 { 1181 {
1360} 1367}
1361 1368
1362/*****************************************************************************/ 1369/*****************************************************************************/
1363/* requests implemented outside eio_execute, because they are so large */ 1370/* requests implemented outside eio_execute, because they are so large */
1364 1371
1365/* copies some absolute path to tmpbuf */ 1372static void
1366static char * 1373eio__lseek (eio_req *req)
1367eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd)
1368{ 1374{
1369 if (wd == EIO_CWD) 1375 /* this usually gets optimised away completely, or your compiler sucks, */
1370 return getcwd (tmpbuf->ptr, PATH_MAX); 1376 /* or the whence constants really are not 0, 1, 2 */
1377 int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET
1378 : req->int2 == EIO_SEEK_CUR ? SEEK_CUR
1379 : req->int2 == EIO_SEEK_END ? SEEK_END
1380 : req->int2;
1371 1381
1372#if HAVE_AT 1382 req->offs = lseek (req->int1, req->offs, whence);
1373 abort (); /*TODO*/ 1383 req->result = req->offs == (off_t)-1 ? -1 : 0;
1374#else
1375 strcpy (tmpbuf->ptr, wd);
1376#endif
1377 return tmpbuf->ptr;
1378} 1384}
1379 1385
1380/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ 1386/* result will always end up in tmpbuf, there is always space for adding a 0-byte */
1381static int 1387static int
1382eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) 1388eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1388 int symlinks = SYMLOOP_MAX; 1394 int symlinks = SYMLOOP_MAX;
1389#else 1395#else
1390 int symlinks = 32; 1396 int symlinks = 32;
1391#endif 1397#endif
1392 1398
1393 /*D*/ /*TODO: wd ignored */
1394
1395 errno = EINVAL; 1399 errno = EINVAL;
1396 if (!rel) 1400 if (!rel)
1397 return -1; 1401 return -1;
1398 1402
1399 errno = ENOENT; 1403 errno = ENOENT;
1427#endif 1431#endif
1428#endif 1432#endif
1429 1433
1430 if (*rel != '/') 1434 if (*rel != '/')
1431 { 1435 {
1432 if (!eio__getwd (tmpbuf, wd)) 1436 int len;
1437
1438 errno = ENOENT;
1439 if (wd == EIO_INVALID_WD)
1433 return -1; 1440 return -1;
1441
1442 if (wd == EIO_CWD)
1443 {
1444 if (!getcwd (res, PATH_MAX))
1445 return -1;
1446
1447 len = strlen (res);
1448 }
1449 else
1450 memcpy (res, wd->str, len = wd->len);
1434 1451
1435 if (res [1]) /* only use if not / */ 1452 if (res [1]) /* only use if not / */
1436 res += strlen (res); 1453 res += len;
1437 } 1454 }
1438 1455
1439 while (*rel) 1456 while (*rel)
1440 { 1457 {
1441 eio_ssize_t len, linklen; 1458 eio_ssize_t len, linklen;
1469 } 1486 }
1470 } 1487 }
1471 1488
1472 errno = ENAMETOOLONG; 1489 errno = ENAMETOOLONG;
1473 if (res + 1 + len + 1 >= tmp1) 1490 if (res + 1 + len + 1 >= tmp1)
1474 return; 1491 return -1;
1475 1492
1476 /* copy one component */ 1493 /* copy one component */
1477 *res = '/'; 1494 *res = '/';
1478 memcpy (res + 1, beg, len); 1495 memcpy (res + 1, beg, len);
1479 1496
1688 eio_dent_insertion_sort (dents, size); 1705 eio_dent_insertion_sort (dents, size);
1689} 1706}
1690 1707
1691/* read a full directory */ 1708/* read a full directory */
1692static void 1709static void
1693eio__scandir (eio_req *req) 1710eio__scandir (eio_req *req, etp_worker *self)
1694{ 1711{
1695 char *name, *names; 1712 char *name, *names;
1696 int namesalloc = 4096 - sizeof (void *) * 4; 1713 int namesalloc = 4096 - sizeof (void *) * 4;
1697 int namesoffs = 0; 1714 int namesoffs = 0;
1698 int flags = req->int1; 1715 int flags = req->int1;
1716#ifdef _WIN32 1733#ifdef _WIN32
1717 { 1734 {
1718 int len = strlen ((const char *)req->ptr1); 1735 int len = strlen ((const char *)req->ptr1);
1719 char *path = malloc (MAX_PATH); 1736 char *path = malloc (MAX_PATH);
1720 const char *fmt; 1737 const char *fmt;
1738 const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1);
1721 1739
1722 if (!len) 1740 if (!len)
1723 fmt = "./*"; 1741 fmt = "./*";
1724 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') 1742 else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\')
1725 fmt = "%s*"; 1743 fmt = "%s*";
1726 else 1744 else
1727 fmt = "%s/*"; 1745 fmt = "%s/*";
1728 1746
1729 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); 1747 _snprintf (path, MAX_PATH, fmt, reqpath);
1730 dirp = FindFirstFile (path, &entp); 1748 dirp = FindFirstFile (path, &entp);
1731 free (path); 1749 free (path);
1732 1750
1733 if (dirp == INVALID_HANDLE_VALUE) 1751 if (dirp == INVALID_HANDLE_VALUE)
1734 { 1752 {
1756 1774
1757 return; 1775 return;
1758 } 1776 }
1759 } 1777 }
1760#else 1778#else
1779 #if HAVE_AT
1780 if (req->wd)
1781 {
1782 int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1783
1784 if (fd < 0)
1785 return;
1786
1787 dirp = fdopendir (fd);
1788
1789 if (!dirp)
1790 close (fd);
1791 }
1792 else
1761 dirp = opendir (req->ptr1); 1793 dirp = opendir (req->ptr1);
1794 #else
1795 dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1));
1796 #endif
1762 1797
1763 if (!dirp) 1798 if (!dirp)
1764 return; 1799 return;
1765#endif 1800#endif
1766 1801
1883 #ifdef DT_FIFO 1918 #ifdef DT_FIFO
1884 case DT_FIFO: ent->type = EIO_DT_FIFO; break; 1919 case DT_FIFO: ent->type = EIO_DT_FIFO; break;
1885 #endif 1920 #endif
1886 #ifdef DT_CHR 1921 #ifdef DT_CHR
1887 case DT_CHR: ent->type = EIO_DT_CHR; break; 1922 case DT_CHR: ent->type = EIO_DT_CHR; break;
1888 #endif 1923 #endif
1889 #ifdef DT_MPC 1924 #ifdef DT_MPC
1890 case DT_MPC: ent->type = EIO_DT_MPC; break; 1925 case DT_MPC: ent->type = EIO_DT_MPC; break;
1891 #endif 1926 #endif
1892 #ifdef DT_DIR 1927 #ifdef DT_DIR
1893 case DT_DIR: ent->type = EIO_DT_DIR; break; 1928 case DT_DIR: ent->type = EIO_DT_DIR; break;
1894 #endif 1929 #endif
1895 #ifdef DT_NAM 1930 #ifdef DT_NAM
1896 case DT_NAM: ent->type = EIO_DT_NAM; break; 1931 case DT_NAM: ent->type = EIO_DT_NAM; break;
1897 #endif 1932 #endif
1898 #ifdef DT_BLK 1933 #ifdef DT_BLK
1899 case DT_BLK: ent->type = EIO_DT_BLK; break; 1934 case DT_BLK: ent->type = EIO_DT_BLK; break;
1900 #endif 1935 #endif
1901 #ifdef DT_MPB 1936 #ifdef DT_MPB
1902 case DT_MPB: ent->type = EIO_DT_MPB; break; 1937 case DT_MPB: ent->type = EIO_DT_MPB; break;
1903 #endif 1938 #endif
1904 #ifdef DT_REG 1939 #ifdef DT_REG
1905 case DT_REG: ent->type = EIO_DT_REG; break; 1940 case DT_REG: ent->type = EIO_DT_REG; break;
1906 #endif 1941 #endif
1907 #ifdef DT_NWK 1942 #ifdef DT_NWK
1908 case DT_NWK: ent->type = EIO_DT_NWK; break; 1943 case DT_NWK: ent->type = EIO_DT_NWK; break;
1909 #endif 1944 #endif
1910 #ifdef DT_CMP 1945 #ifdef DT_CMP
1911 case DT_CMP: ent->type = EIO_DT_CMP; break; 1946 case DT_CMP: ent->type = EIO_DT_CMP; break;
1912 #endif 1947 #endif
1913 #ifdef DT_LNK 1948 #ifdef DT_LNK
1914 case DT_LNK: ent->type = EIO_DT_LNK; break; 1949 case DT_LNK: ent->type = EIO_DT_LNK; break;
1915 #endif 1950 #endif
1916 #ifdef DT_SOCK 1951 #ifdef DT_SOCK
1917 case DT_SOCK: ent->type = EIO_DT_SOCK; break; 1952 case DT_SOCK: ent->type = EIO_DT_SOCK; break;
1930 { 1965 {
1931 if (ent->type == EIO_DT_UNKNOWN) 1966 if (ent->type == EIO_DT_UNKNOWN)
1932 { 1967 {
1933 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1968 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1934 ent->score = 1; 1969 ent->score = 1;
1935 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1970 else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */
1936 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ 1971 ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */
1937 } 1972 }
1938 else if (ent->type == EIO_DT_DIR) 1973 else if (ent->type == EIO_DT_DIR)
1939 ent->score = 0; 1974 ent->score = 0;
1940 } 1975 }
1960 } 1995 }
1961} 1996}
1962 1997
1963/*****************************************************************************/ 1998/*****************************************************************************/
1964/* working directory stuff */ 1999/* working directory stuff */
2000/* various deficiencies in the posix 2008 api force us to */
2001/* keep the absolute path in string form at all times */
2002/* fuck yeah. */
2003
2004#if !HAVE_AT
2005
2006/* a bit like realpath, but usually faster because it doesn'T have to return */
2007/* an absolute or canonical path */
2008static const char *
2009wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2010{
2011 if (!wd || *path == '/')
2012 return path;
2013
2014 if (path [0] == '.' && !path [1])
2015 return wd->str;
2016
2017 {
2018 int l1 = wd->len;
2019 int l2 = strlen (path);
2020
2021 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
2022
2023 memcpy (res, wd->str, l1);
2024 res [l1] = '/';
2025 memcpy (res + l1 + 1, path, l2 + 1);
2026
2027 return res;
2028 }
2029}
2030
2031#endif
2032
2033static eio_wd
2034eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2035{
2036 int fd;
2037 eio_wd res;
2038 int len = eio__realpath (tmpbuf, wd, path);
2039
2040 if (len < 0)
2041 return EIO_INVALID_WD;
1965 2042
1966#if HAVE_AT 2043#if HAVE_AT
2044 fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY);
1967 2045
1968#define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) 2046 if (fd < 0)
1969 2047 return EIO_INVALID_WD;
1970#ifndef O_SEARCH
1971# define O_SEARCH O_RDONLY
1972#endif 2048#endif
2049
2050 res = malloc (sizeof (*res) + len); /* one extra 0-byte */
2051
2052#if HAVE_AT
2053 res->fd = fd;
2054#endif
2055
2056 res->len = len;
2057 memcpy (res->str, tmpbuf->ptr, len);
2058 res->str [len] = 0;
2059
2060 return res;
2061}
1973 2062
1974eio_wd 2063eio_wd
1975eio_wd_open_sync (eio_wd wd, const char *path) 2064eio_wd_open_sync (eio_wd wd, const char *path)
1976{ 2065{
1977 int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); 2066 struct tmpbuf tmpbuf = { 0 };
1978
1979 return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD;
1980}
1981
1982static eio_wd
1983eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
1984{
1985 return eio_wd_open_sync (wd, path); 2067 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2068 free (tmpbuf.ptr);
2069
2070 return wd;
1986} 2071}
1987 2072
1988void 2073void
1989eio_wd_close_sync (eio_wd wd) 2074eio_wd_close_sync (eio_wd wd)
1990{ 2075{
1991 int fd = WD2FD (wd); 2076 if (wd != EIO_INVALID_WD && wd != EIO_CWD)
1992 2077 {
1993 if (fd >= 0) 2078 #if HAVE_AT
1994 close (fd); 2079 close (wd->fd);
2080 #endif
2081 free (wd);
2082 }
1995} 2083}
2084
2085#if HAVE_AT
2086
2087/* they forgot these */
1996 2088
1997static int 2089static int
1998eio__truncateat (int dirfd, const char *path, off_t length) 2090eio__truncateat (int dirfd, const char *path, off_t length)
1999{ 2091{
2000 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); 2092 int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC);
2019 2111
2020 res = fstatvfs (fd, buf); 2112 res = fstatvfs (fd, buf);
2021 close (fd); 2113 close (fd);
2022 return res; 2114 return res;
2023 2115
2024}
2025
2026#else
2027
2028/* on legacy systems, we represent the working directories simply by their path strings */
2029
2030static const char *
2031wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2032{
2033 if (!wd || *path == '/')
2034 return path;
2035
2036 {
2037 int l1 = strlen ((const char *)wd);
2038 int l2 = strlen (path);
2039
2040 char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2);
2041
2042 memcpy (res, wd, l1);
2043 res [l1] = '/';
2044 memcpy (res + l1 + 1, path, l2 + 1);
2045
2046 return res;
2047 }
2048}
2049
2050static eio_wd
2051eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path)
2052{
2053 if (*path == '/') /* absolute paths ignore wd */
2054 path = strdup (path);
2055 else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */
2056 return wd;
2057 else
2058 {
2059 int len = eio__realpath (tmpbuf, wd, path);
2060
2061 path = EIO_INVALID_WD;
2062
2063 if (len >= 0)
2064 {
2065 ((char *)tmpbuf->ptr)[len] = 0;
2066 path = strdup (tmpbuf->ptr);
2067 }
2068 }
2069
2070 if (!path)
2071 path = EIO_INVALID_WD;
2072
2073 return (eio_wd)path;
2074}
2075
2076eio_wd
2077eio_wd_open_sync (eio_wd wd, const char *path)
2078{
2079 struct tmpbuf tmpbuf = { 0 };
2080 wd = eio__wd_open_sync (&tmpbuf, wd, path);
2081 free (tmpbuf.ptr);
2082
2083 return wd;
2084}
2085
2086void
2087eio_wd_close_sync (eio_wd wd)
2088{
2089 if (wd != EIO_INVALID_WD)
2090 free (wd);
2091} 2116}
2092 2117
2093#endif 2118#endif
2094 2119
2095/*****************************************************************************/ 2120/*****************************************************************************/
2107 req->result = -1; \ 2132 req->result = -1; \
2108 break; \ 2133 break; \
2109 } \ 2134 } \
2110 } 2135 }
2111 2136
2137static void ecb_noinline ecb_cold
2138etp_proc_init (void)
2139{
2140#if HAVE_PRCTL_SET_NAME
2141 /* provide a more sensible "thread name" */
2142 char name[16 + 1];
2143 const int namelen = sizeof (name) - 1;
2144 int len;
2145
2146 prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0);
2147 name [namelen] = 0;
2148 len = strlen (name);
2149 strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio");
2150 prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0);
2151#endif
2152}
2153
2112X_THREAD_PROC (etp_proc) 2154X_THREAD_PROC (etp_proc)
2113{ 2155{
2114 ETP_REQ *req; 2156 ETP_REQ *req;
2115 struct timespec ts; 2157 struct timespec ts;
2116 etp_worker *self = (etp_worker *)thr_arg; 2158 etp_worker *self = (etp_worker *)thr_arg;
2117 2159
2118#if HAVE_PRCTL_SET_NAME 2160 etp_proc_init ();
2119 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
2120#endif
2121 2161
2122 /* try to distribute timeouts somewhat evenly */ 2162 /* try to distribute timeouts somewhat evenly */
2123 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 2163 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
2124 2164
2125 for (;;) 2165 for (;;)
2196/*****************************************************************************/ 2236/*****************************************************************************/
2197 2237
2198int ecb_cold 2238int ecb_cold
2199eio_init (void (*want_poll)(void), void (*done_poll)(void)) 2239eio_init (void (*want_poll)(void), void (*done_poll)(void))
2200{ 2240{
2201#if !HAVE_PREADWRITE
2202 X_MUTEX_CREATE (preadwritelock);
2203#endif
2204
2205 return etp_init (want_poll, done_poll); 2241 return etp_init (want_poll, done_poll);
2206} 2242}
2207 2243
2208ecb_inline void 2244ecb_inline void
2209eio_api_destroy (eio_req *req) 2245eio_api_destroy (eio_req *req)
2210{ 2246{
2211 free (req); 2247 free (req);
2212} 2248}
2213 2249
2214#define REQ(rtype) \ 2250#define REQ(rtype) \
2215 eio_req *req; \ 2251 eio_req *req; \
2216 \ 2252 \
2217 req = (eio_req *)calloc (1, sizeof *req); \ 2253 req = (eio_req *)calloc (1, sizeof *req); \
2218 if (!req) \ 2254 if (!req) \
2219 return 0; \ 2255 return 0; \
2270 switch (req->type) 2306 switch (req->type)
2271 { 2307 {
2272 case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); 2308 case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1);
2273 req->result = req->wd == EIO_INVALID_WD ? -1 : 0; 2309 req->result = req->wd == EIO_INVALID_WD ? -1 : 0;
2274 break; 2310 break;
2311 case EIO_WD_CLOSE: req->result = 0;
2312 eio_wd_close_sync (req->wd); break;
2313
2275 case EIO_WD_CLOSE: eio_wd_close_sync (req->wd); break; 2314 case EIO_SEEK: eio__lseek (req); break;
2276
2277 case EIO_READ: ALLOC (req->size); 2315 case EIO_READ: ALLOC (req->size);
2278 req->result = req->offs >= 0 2316 req->result = req->offs >= 0
2279 ? pread (req->int1, req->ptr2, req->size, req->offs) 2317 ? pread (req->int1, req->ptr2, req->size, req->offs)
2280 : read (req->int1, req->ptr2, req->size); break; 2318 : read (req->int1, req->ptr2, req->size); break;
2281 case EIO_WRITE: req->result = req->offs >= 0 2319 case EIO_WRITE: req->result = req->offs >= 0
2284 2322
2285 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 2323 case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
2286 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; 2324 case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break;
2287 2325
2288#if HAVE_AT 2326#if HAVE_AT
2327
2289 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2328 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2290 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; 2329 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break;
2291 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2330 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2292 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; 2331 req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break;
2293 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; 2332 case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break;
2296 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; 2335 case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break;
2297 2336
2298 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; 2337 case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break;
2299 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; 2338 case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break;
2300 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; 2339 case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break;
2301 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2); break; 2340 case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break;
2302 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2, 0); break; 2341 case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break;
2303 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; 2342 case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break;
2304 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2343 case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
2305 case EIO_READLINK: ALLOC (PATH_MAX); 2344 case EIO_READLINK: ALLOC (PATH_MAX);
2306 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; 2345 req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break;
2307 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 2346 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2308 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; 2347 req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break;
2348 case EIO_UTIME:
2349 case EIO_FUTIME:
2350 {
2351 struct timespec ts[2];
2352 struct timespec *times;
2353
2354 if (req->nv1 != -1. || req->nv2 != -1.)
2355 {
2356 ts[0].tv_sec = req->nv1;
2357 ts[0].tv_nsec = (req->nv1 - ts[0].tv_sec) * 1e9;
2358 ts[1].tv_sec = req->nv2;
2359 ts[1].tv_nsec = (req->nv2 - ts[1].tv_sec) * 1e9;
2360
2361 times = ts;
2362 }
2363 else
2364 times = 0;
2365
2366 req->result = req->type == EIO_FUTIME
2367 ? futimens (req->int1, times)
2368 : utimensat (dirfd, req->ptr1, times, 0);
2369 }
2370 break;
2371
2309#else 2372#else
2373
2310 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2374 case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2311 req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 2375 req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2312 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); 2376 case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT));
2313 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; 2377 req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break;
2314 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; 2378 case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break;
2325 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; 2389 case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break;
2326 case EIO_READLINK: ALLOC (PATH_MAX); 2390 case EIO_READLINK: ALLOC (PATH_MAX);
2327 req->result = readlink (path, req->ptr2, PATH_MAX); break; 2391 req->result = readlink (path, req->ptr2, PATH_MAX); break;
2328 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); 2392 case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS));
2329 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; 2393 req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break;
2394
2395 case EIO_UTIME:
2396 case EIO_FUTIME:
2397 {
2398 struct timeval tv[2];
2399 struct timeval *times;
2400
2401 if (req->nv1 != -1. || req->nv2 != -1.)
2402 {
2403 tv[0].tv_sec = req->nv1;
2404 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1e6;
2405 tv[1].tv_sec = req->nv2;
2406 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1e6;
2407
2408 times = tv;
2409 }
2410 else
2411 times = 0;
2412
2413 req->result = req->type == EIO_FUTIME
2414 ? futimes (req->int1, times)
2415 : utimes (req->ptr1, times);
2416 }
2417 break;
2418
2330#endif 2419#endif
2331 2420
2332 case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) 2421 case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1)))
2333 { 2422 {
2334 ALLOC (req->result); 2423 ALLOC (req->result);
2357 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2446 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
2358 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2447 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
2359 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2448 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
2360 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; 2449 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
2361 2450
2362 case EIO_READDIR: eio__scandir (req); break; 2451 case EIO_READDIR: eio__scandir (req, self); break;
2363 2452
2364 case EIO_BUSY: 2453 case EIO_BUSY:
2365#ifdef _WIN32 2454#ifdef _WIN32
2366 Sleep (req->nv1 * 1e3); 2455 Sleep (req->nv1 * 1e3);
2367#else 2456#else
2374 req->result = select (0, 0, 0, 0, &tv); 2463 req->result = select (0, 0, 0, 0, &tv);
2375 } 2464 }
2376#endif 2465#endif
2377 break; 2466 break;
2378 2467
2379 case EIO_UTIME:
2380 case EIO_FUTIME:
2381 {
2382 struct timeval tv[2];
2383 struct timeval *times;
2384
2385 if (req->nv1 != -1. || req->nv2 != -1.)
2386 {
2387 tv[0].tv_sec = req->nv1;
2388 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
2389 tv[1].tv_sec = req->nv2;
2390 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
2391
2392 times = tv;
2393 }
2394 else
2395 times = 0;
2396
2397 req->result = req->type == EIO_FUTIME
2398 ? futimes (req->int1, times)
2399 : utimes (req->ptr1, times);
2400 }
2401 break;
2402
2403 case EIO_GROUP: 2468 case EIO_GROUP:
2404 abort (); /* handled in eio_request */ 2469 abort (); /* handled in eio_request */
2405 2470
2406 case EIO_NOP: 2471 case EIO_NOP:
2407 req->result = 0; 2472 req->result = 0;
2410 case EIO_CUSTOM: 2475 case EIO_CUSTOM:
2411 req->feed (req); 2476 req->feed (req);
2412 break; 2477 break;
2413 2478
2414 default: 2479 default:
2415 errno = ENOSYS;
2416 req->result = -1; 2480 req->result = EIO_ENOSYS ();
2417 break; 2481 break;
2418 } 2482 }
2419 2483
2420 req->errorno = errno; 2484 req->errorno = errno;
2421} 2485}
2422 2486
2423#ifndef EIO_NO_WRAPPERS 2487#ifndef EIO_NO_WRAPPERS
2424 2488
2489eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data)
2490{
2491 REQ (EIO_WD_OPEN); PATH; SEND;
2492}
2493
2494eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data)
2495{
2496 REQ (EIO_WD_CLOSE); req->wd = wd; SEND;
2497}
2498
2425eio_req *eio_nop (int pri, eio_cb cb, void *data) 2499eio_req *eio_nop (int pri, eio_cb cb, void *data)
2426{ 2500{
2427 REQ (EIO_NOP); SEND; 2501 REQ (EIO_NOP); SEND;
2428} 2502}
2429 2503
2488} 2562}
2489 2563
2490eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) 2564eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data)
2491{ 2565{
2492 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; 2566 REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND;
2567}
2568
2569eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data)
2570{
2571 REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND;
2493} 2572}
2494 2573
2495eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) 2574eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data)
2496{ 2575{
2497 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; 2576 REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines