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 | * |
… | |
… | |
58 | #include <sys/stat.h> |
58 | #include <sys/stat.h> |
59 | #include <limits.h> |
59 | #include <limits.h> |
60 | #include <fcntl.h> |
60 | #include <fcntl.h> |
61 | #include <assert.h> |
61 | #include <assert.h> |
62 | |
62 | |
63 | #if _POSIX_VERSION >= 200809L |
|
|
64 | # define HAVE_AT 1 |
|
|
65 | #else |
|
|
66 | # define HAVE_AT 0 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
63 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
70 | /* intptr_t only comes from stdint.h, says idiot openbsd coder */ |
64 | /* intptr_t only comes from stdint.h, says idiot openbsd coder */ |
71 | #if HAVE_STDINT_H |
65 | #if HAVE_STDINT_H |
72 | # include <stdint.h> |
66 | # include <stdint.h> |
73 | #endif |
67 | #endif |
… | |
… | |
214 | #endif |
208 | #endif |
215 | |
209 | |
216 | #define D_NAME(entp) entp->d_name |
210 | #define D_NAME(entp) entp->d_name |
217 | |
211 | |
218 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
212 | /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ |
219 | #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ |
213 | #if __FreeBSD__ || __NetBSD__ || __OpenBSD__ |
220 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
214 | #define _DIRENT_HAVE_D_TYPE /* sigh */ |
221 | #define D_INO(de) (de)->d_fileno |
215 | #define D_INO(de) (de)->d_fileno |
222 | #define D_NAMLEN(de) (de)->d_namlen |
216 | #define D_NAMLEN(de) (de)->d_namlen |
223 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
217 | #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 |
224 | #define D_INO(de) (de)->d_ino |
218 | #define D_INO(de) (de)->d_ino |
… | |
… | |
318 | } |
312 | } |
319 | |
313 | |
320 | return buf->ptr; |
314 | return buf->ptr; |
321 | } |
315 | } |
322 | |
316 | |
|
|
317 | struct tmpbuf; |
|
|
318 | |
|
|
319 | #if _POSIX_VERSION >= 200809L |
|
|
320 | #define HAVE_AT 1 |
|
|
321 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
|
|
322 | #ifndef O_SEARCH |
|
|
323 | #define O_SEARCH O_RDONLY |
|
|
324 | #endif |
|
|
325 | #else |
|
|
326 | #define HAVE_AT 0 |
|
|
327 | static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); |
|
|
328 | #endif |
|
|
329 | |
|
|
330 | struct eio_pwd |
|
|
331 | { |
|
|
332 | #if HAVE_AT |
|
|
333 | int fd; |
|
|
334 | #endif |
|
|
335 | int len; |
|
|
336 | char str[1]; /* actually, a 0-terminated canonical path */ |
|
|
337 | }; |
|
|
338 | |
323 | /*****************************************************************************/ |
339 | /*****************************************************************************/ |
324 | |
340 | |
325 | #define ETP_PRI_MIN EIO_PRI_MIN |
341 | #define ETP_PRI_MIN EIO_PRI_MIN |
326 | #define ETP_PRI_MAX EIO_PRI_MAX |
342 | #define ETP_PRI_MAX EIO_PRI_MAX |
327 | |
343 | |
… | |
… | |
393 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
409 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
394 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
410 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
395 | |
411 | |
396 | /* worker threads management */ |
412 | /* worker threads management */ |
397 | |
413 | |
398 | static void ecb_cold |
414 | static void |
399 | etp_worker_clear (etp_worker *wrk) |
415 | etp_worker_clear (etp_worker *wrk) |
400 | { |
416 | { |
401 | } |
417 | } |
402 | |
418 | |
403 | static void ecb_cold |
419 | static void ecb_cold |
… | |
… | |
1016 | int res; |
1032 | int res; |
1017 | |
1033 | |
1018 | #if HAVE_SYS_SYNCFS |
1034 | #if HAVE_SYS_SYNCFS |
1019 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1035 | res = (int)syscall (__NR_syncfs, (int)(fd)); |
1020 | #else |
1036 | #else |
1021 | res = -1; |
1037 | res = EIO_ENOSYS (); |
1022 | errno = ENOSYS; |
|
|
1023 | #endif |
1038 | #endif |
1024 | |
1039 | |
1025 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1040 | if (res < 0 && errno == ENOSYS && fd >= 0) |
1026 | sync (); |
1041 | sync (); |
1027 | |
1042 | |
… | |
… | |
1057 | } |
1072 | } |
1058 | |
1073 | |
1059 | static int |
1074 | static int |
1060 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1075 | eio__fallocate (int fd, int mode, off_t offset, size_t len) |
1061 | { |
1076 | { |
1062 | #if HAVE_FALLOCATE |
1077 | #if HAVE_LINUX_FALLOCATE |
1063 | return fallocate (fd, mode, offset, len); |
1078 | return fallocate (fd, mode, offset, len); |
1064 | #else |
1079 | #else |
1065 | errno = ENOSYS; |
1080 | return EIO_ENOSYS (); |
1066 | return -1; |
|
|
1067 | #endif |
1081 | #endif |
1068 | } |
1082 | } |
1069 | |
1083 | |
1070 | #if !HAVE_READAHEAD |
1084 | #if !HAVE_READAHEAD |
1071 | # undef readahead |
1085 | # undef readahead |
… | |
… | |
1086 | todo -= len; |
1100 | todo -= len; |
1087 | } |
1101 | } |
1088 | |
1102 | |
1089 | FUBd; |
1103 | FUBd; |
1090 | |
1104 | |
1091 | errno = 0; |
1105 | /* linux's readahead basically only fails for EBADF or EINVAL (not mmappable) */ |
|
|
1106 | /* but not for e.g. EIO or eof, so we also never fail */ |
1092 | return count; |
1107 | return 0; |
1093 | } |
1108 | } |
1094 | |
1109 | |
1095 | #endif |
1110 | #endif |
1096 | |
1111 | |
1097 | /* sendfile always needs emulation */ |
1112 | /* sendfile always needs emulation */ |
… | |
… | |
1132 | |
1147 | |
1133 | /* according to source inspection, this is correct, and useful behaviour */ |
1148 | /* according to source inspection, this is correct, and useful behaviour */ |
1134 | if (sbytes) |
1149 | if (sbytes) |
1135 | res = sbytes; |
1150 | res = sbytes; |
1136 | |
1151 | |
1137 | # elif defined (__APPLE__) |
1152 | # elif defined __APPLE__ |
1138 | off_t sbytes = count; |
1153 | off_t sbytes = count; |
1139 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1154 | res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); |
1140 | |
1155 | |
1141 | /* according to the manpage, sbytes is always valid */ |
1156 | /* according to the manpage, sbytes is always valid */ |
1142 | if (sbytes) |
1157 | if (sbytes) |
… | |
… | |
1169 | HANDLE h = TO_SOCKET (ifd); |
1184 | HANDLE h = TO_SOCKET (ifd); |
1170 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1185 | SetFilePointer (h, offset, 0, FILE_BEGIN); |
1171 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1186 | res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); |
1172 | |
1187 | |
1173 | #else |
1188 | #else |
1174 | res = -1; |
1189 | res = EIO_ENOSYS (); |
1175 | errno = ENOSYS; |
|
|
1176 | #endif |
1190 | #endif |
1177 | |
1191 | |
1178 | /* we assume sendfile can copy at least 128mb in one go */ |
1192 | /* we assume sendfile can copy at least 128mb in one go */ |
1179 | if (res <= 128 * 1024 * 1024) |
1193 | if (res <= 128 * 1024 * 1024) |
1180 | { |
1194 | { |
… | |
… | |
1366 | } |
1380 | } |
1367 | |
1381 | |
1368 | /*****************************************************************************/ |
1382 | /*****************************************************************************/ |
1369 | /* requests implemented outside eio_execute, because they are so large */ |
1383 | /* requests implemented outside eio_execute, because they are so large */ |
1370 | |
1384 | |
1371 | /* copies some absolute path to tmpbuf */ |
1385 | static void |
1372 | static char * |
1386 | eio__lseek (eio_req *req) |
1373 | eio__getwd (struct tmpbuf *tmpbuf, eio_wd wd) |
|
|
1374 | { |
1387 | { |
1375 | if (wd == EIO_CWD) |
1388 | /* this usually gets optimised away completely, or your compiler sucks, */ |
1376 | return getcwd (tmpbuf->ptr, PATH_MAX); |
1389 | /* or the whence constants really are not 0, 1, 2 */ |
|
|
1390 | int whence = req->int2 == EIO_SEEK_SET ? SEEK_SET |
|
|
1391 | : req->int2 == EIO_SEEK_CUR ? SEEK_CUR |
|
|
1392 | : req->int2 == EIO_SEEK_END ? SEEK_END |
|
|
1393 | : req->int2; |
1377 | |
1394 | |
1378 | #if HAVE_AT |
1395 | req->offs = lseek (req->int1, req->offs, whence); |
1379 | abort (); /*TODO*/ |
1396 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
1380 | #else |
|
|
1381 | strcpy (tmpbuf->ptr, wd); |
|
|
1382 | #endif |
|
|
1383 | return tmpbuf->ptr; |
|
|
1384 | } |
1397 | } |
1385 | |
1398 | |
1386 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1399 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
1387 | static int |
1400 | static int |
1388 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1401 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
… | |
… | |
1394 | int symlinks = SYMLOOP_MAX; |
1407 | int symlinks = SYMLOOP_MAX; |
1395 | #else |
1408 | #else |
1396 | int symlinks = 32; |
1409 | int symlinks = 32; |
1397 | #endif |
1410 | #endif |
1398 | |
1411 | |
1399 | /*D*/ /*TODO: wd ignored */ |
|
|
1400 | |
|
|
1401 | errno = EINVAL; |
1412 | errno = EINVAL; |
1402 | if (!rel) |
1413 | if (!rel) |
1403 | return -1; |
1414 | return -1; |
1404 | |
1415 | |
1405 | errno = ENOENT; |
1416 | errno = ENOENT; |
… | |
… | |
1433 | #endif |
1444 | #endif |
1434 | #endif |
1445 | #endif |
1435 | |
1446 | |
1436 | if (*rel != '/') |
1447 | if (*rel != '/') |
1437 | { |
1448 | { |
1438 | if (!eio__getwd (tmpbuf, wd)) |
1449 | int len; |
|
|
1450 | |
|
|
1451 | errno = ENOENT; |
|
|
1452 | if (wd == EIO_INVALID_WD) |
1439 | return -1; |
1453 | return -1; |
|
|
1454 | |
|
|
1455 | if (wd == EIO_CWD) |
|
|
1456 | { |
|
|
1457 | if (!getcwd (res, PATH_MAX)) |
|
|
1458 | return -1; |
|
|
1459 | |
|
|
1460 | len = strlen (res); |
|
|
1461 | } |
|
|
1462 | else |
|
|
1463 | memcpy (res, wd->str, len = wd->len); |
1440 | |
1464 | |
1441 | if (res [1]) /* only use if not / */ |
1465 | if (res [1]) /* only use if not / */ |
1442 | res += strlen (res); |
1466 | res += len; |
1443 | } |
1467 | } |
1444 | |
1468 | |
1445 | while (*rel) |
1469 | while (*rel) |
1446 | { |
1470 | { |
1447 | eio_ssize_t len, linklen; |
1471 | eio_ssize_t len, linklen; |
… | |
… | |
1475 | } |
1499 | } |
1476 | } |
1500 | } |
1477 | |
1501 | |
1478 | errno = ENAMETOOLONG; |
1502 | errno = ENAMETOOLONG; |
1479 | if (res + 1 + len + 1 >= tmp1) |
1503 | if (res + 1 + len + 1 >= tmp1) |
1480 | return; |
1504 | return -1; |
1481 | |
1505 | |
1482 | /* copy one component */ |
1506 | /* copy one component */ |
1483 | *res = '/'; |
1507 | *res = '/'; |
1484 | memcpy (res + 1, beg, len); |
1508 | memcpy (res + 1, beg, len); |
1485 | |
1509 | |
… | |
… | |
1694 | eio_dent_insertion_sort (dents, size); |
1718 | eio_dent_insertion_sort (dents, size); |
1695 | } |
1719 | } |
1696 | |
1720 | |
1697 | /* read a full directory */ |
1721 | /* read a full directory */ |
1698 | static void |
1722 | static void |
1699 | eio__scandir (eio_req *req) |
1723 | eio__scandir (eio_req *req, etp_worker *self) |
1700 | { |
1724 | { |
1701 | char *name, *names; |
1725 | char *name, *names; |
1702 | int namesalloc = 4096 - sizeof (void *) * 4; |
1726 | int namesalloc = 4096 - sizeof (void *) * 4; |
1703 | int namesoffs = 0; |
1727 | int namesoffs = 0; |
1704 | int flags = req->int1; |
1728 | int flags = req->int1; |
… | |
… | |
1722 | #ifdef _WIN32 |
1746 | #ifdef _WIN32 |
1723 | { |
1747 | { |
1724 | int len = strlen ((const char *)req->ptr1); |
1748 | int len = strlen ((const char *)req->ptr1); |
1725 | char *path = malloc (MAX_PATH); |
1749 | char *path = malloc (MAX_PATH); |
1726 | const char *fmt; |
1750 | const char *fmt; |
|
|
1751 | const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1); |
1727 | |
1752 | |
1728 | if (!len) |
1753 | if (!len) |
1729 | fmt = "./*"; |
1754 | fmt = "./*"; |
1730 | else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\') |
1755 | else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\') |
1731 | fmt = "%s*"; |
1756 | fmt = "%s*"; |
1732 | else |
1757 | else |
1733 | fmt = "%s/*"; |
1758 | fmt = "%s/*"; |
1734 | |
1759 | |
1735 | _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1); |
1760 | _snprintf (path, MAX_PATH, fmt, reqpath); |
1736 | dirp = FindFirstFile (path, &entp); |
1761 | dirp = FindFirstFile (path, &entp); |
1737 | free (path); |
1762 | free (path); |
1738 | |
1763 | |
1739 | if (dirp == INVALID_HANDLE_VALUE) |
1764 | if (dirp == INVALID_HANDLE_VALUE) |
1740 | { |
1765 | { |
… | |
… | |
1762 | |
1787 | |
1763 | return; |
1788 | return; |
1764 | } |
1789 | } |
1765 | } |
1790 | } |
1766 | #else |
1791 | #else |
|
|
1792 | #if HAVE_AT |
|
|
1793 | if (req->wd) |
|
|
1794 | { |
|
|
1795 | int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
|
|
1796 | |
|
|
1797 | if (fd < 0) |
|
|
1798 | return; |
|
|
1799 | |
|
|
1800 | dirp = fdopendir (fd); |
|
|
1801 | |
|
|
1802 | if (!dirp) |
|
|
1803 | close (fd); |
|
|
1804 | } |
|
|
1805 | else |
1767 | dirp = opendir (req->ptr1); |
1806 | dirp = opendir (req->ptr1); |
|
|
1807 | #else |
|
|
1808 | dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); |
|
|
1809 | #endif |
1768 | |
1810 | |
1769 | if (!dirp) |
1811 | if (!dirp) |
1770 | return; |
1812 | return; |
1771 | #endif |
1813 | #endif |
1772 | |
1814 | |
… | |
… | |
1936 | { |
1978 | { |
1937 | if (ent->type == EIO_DT_UNKNOWN) |
1979 | if (ent->type == EIO_DT_UNKNOWN) |
1938 | { |
1980 | { |
1939 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1981 | if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ |
1940 | ent->score = 1; |
1982 | ent->score = 1; |
1941 | else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ |
1983 | else if (!strchr (name, '.')) /* absence of dots indicate likely dirs */ |
1942 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1984 | ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ |
1943 | } |
1985 | } |
1944 | else if (ent->type == EIO_DT_DIR) |
1986 | else if (ent->type == EIO_DT_DIR) |
1945 | ent->score = 0; |
1987 | ent->score = 0; |
1946 | } |
1988 | } |
… | |
… | |
1966 | } |
2008 | } |
1967 | } |
2009 | } |
1968 | |
2010 | |
1969 | /*****************************************************************************/ |
2011 | /*****************************************************************************/ |
1970 | /* working directory stuff */ |
2012 | /* working directory stuff */ |
|
|
2013 | /* various deficiencies in the posix 2008 api force us to */ |
|
|
2014 | /* keep the absolute path in string form at all times */ |
|
|
2015 | /* fuck yeah. */ |
|
|
2016 | |
|
|
2017 | #if !HAVE_AT |
|
|
2018 | |
|
|
2019 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
|
|
2020 | /* an absolute or canonical path */ |
|
|
2021 | static const char * |
|
|
2022 | wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2023 | { |
|
|
2024 | if (!wd || *path == '/') |
|
|
2025 | return path; |
|
|
2026 | |
|
|
2027 | if (path [0] == '.' && !path [1]) |
|
|
2028 | return wd->str; |
|
|
2029 | |
|
|
2030 | { |
|
|
2031 | int l1 = wd->len; |
|
|
2032 | int l2 = strlen (path); |
|
|
2033 | |
|
|
2034 | char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); |
|
|
2035 | |
|
|
2036 | memcpy (res, wd->str, l1); |
|
|
2037 | res [l1] = '/'; |
|
|
2038 | memcpy (res + l1 + 1, path, l2 + 1); |
|
|
2039 | |
|
|
2040 | return res; |
|
|
2041 | } |
|
|
2042 | } |
|
|
2043 | |
|
|
2044 | #endif |
|
|
2045 | |
|
|
2046 | static eio_wd |
|
|
2047 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2048 | { |
|
|
2049 | int fd; |
|
|
2050 | eio_wd res; |
|
|
2051 | int len = eio__realpath (tmpbuf, wd, path); |
|
|
2052 | |
|
|
2053 | if (len < 0) |
|
|
2054 | return EIO_INVALID_WD; |
1971 | |
2055 | |
1972 | #if HAVE_AT |
2056 | #if HAVE_AT |
|
|
2057 | fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
1973 | |
2058 | |
1974 | #define WD2FD(wd) ((wd) ? ((int)(long)(wd)) - 1 : AT_FDCWD) |
2059 | if (fd < 0) |
1975 | |
2060 | return EIO_INVALID_WD; |
1976 | #ifndef O_SEARCH |
|
|
1977 | # define O_SEARCH O_RDONLY |
|
|
1978 | #endif |
2061 | #endif |
|
|
2062 | |
|
|
2063 | res = malloc (sizeof (*res) + len); /* one extra 0-byte */ |
|
|
2064 | |
|
|
2065 | #if HAVE_AT |
|
|
2066 | res->fd = fd; |
|
|
2067 | #endif |
|
|
2068 | |
|
|
2069 | res->len = len; |
|
|
2070 | memcpy (res->str, tmpbuf->ptr, len); |
|
|
2071 | res->str [len] = 0; |
|
|
2072 | |
|
|
2073 | return res; |
|
|
2074 | } |
1979 | |
2075 | |
1980 | eio_wd |
2076 | eio_wd |
1981 | eio_wd_open_sync (eio_wd wd, const char *path) |
2077 | eio_wd_open_sync (eio_wd wd, const char *path) |
1982 | { |
2078 | { |
1983 | int fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); |
2079 | struct tmpbuf tmpbuf = { 0 }; |
1984 | |
|
|
1985 | return fd >= 0 ? (eio_wd)(long)(fd + 1) : EIO_INVALID_WD; |
|
|
1986 | } |
|
|
1987 | |
|
|
1988 | static eio_wd |
|
|
1989 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
1990 | { |
|
|
1991 | return eio_wd_open_sync (wd, path); |
2080 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
|
|
2081 | free (tmpbuf.ptr); |
|
|
2082 | |
|
|
2083 | return wd; |
1992 | } |
2084 | } |
1993 | |
2085 | |
1994 | void |
2086 | void |
1995 | eio_wd_close_sync (eio_wd wd) |
2087 | eio_wd_close_sync (eio_wd wd) |
1996 | { |
2088 | { |
1997 | int fd = WD2FD (wd); |
2089 | if (wd != EIO_INVALID_WD && wd != EIO_CWD) |
1998 | |
2090 | { |
1999 | if (fd >= 0) |
2091 | #if HAVE_AT |
2000 | close (fd); |
2092 | close (wd->fd); |
|
|
2093 | #endif |
|
|
2094 | free (wd); |
|
|
2095 | } |
2001 | } |
2096 | } |
|
|
2097 | |
|
|
2098 | #if HAVE_AT |
|
|
2099 | |
|
|
2100 | /* they forgot these */ |
2002 | |
2101 | |
2003 | static int |
2102 | static int |
2004 | eio__truncateat (int dirfd, const char *path, off_t length) |
2103 | eio__truncateat (int dirfd, const char *path, off_t length) |
2005 | { |
2104 | { |
2006 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); |
2105 | int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); |
… | |
… | |
2025 | |
2124 | |
2026 | res = fstatvfs (fd, buf); |
2125 | res = fstatvfs (fd, buf); |
2027 | close (fd); |
2126 | close (fd); |
2028 | return res; |
2127 | return res; |
2029 | |
2128 | |
2030 | } |
|
|
2031 | |
|
|
2032 | #else |
|
|
2033 | |
|
|
2034 | /* on legacy systems, we represent the working directories simply by their path strings */ |
|
|
2035 | |
|
|
2036 | static const char * |
|
|
2037 | wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2038 | { |
|
|
2039 | if (!wd || *path == '/') |
|
|
2040 | return path; |
|
|
2041 | |
|
|
2042 | { |
|
|
2043 | int l1 = strlen ((const char *)wd); |
|
|
2044 | int l2 = strlen (path); |
|
|
2045 | |
|
|
2046 | char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); |
|
|
2047 | |
|
|
2048 | memcpy (res, wd, l1); |
|
|
2049 | res [l1] = '/'; |
|
|
2050 | memcpy (res + l1 + 1, path, l2 + 1); |
|
|
2051 | |
|
|
2052 | return res; |
|
|
2053 | } |
|
|
2054 | } |
|
|
2055 | |
|
|
2056 | static eio_wd |
|
|
2057 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
|
|
2058 | { |
|
|
2059 | if (*path == '/') /* absolute paths ignore wd */ |
|
|
2060 | path = strdup (path); |
|
|
2061 | else if (path [0] == '.' && !path [1]) /* special case '.', as it is common */ |
|
|
2062 | return wd; |
|
|
2063 | else |
|
|
2064 | { |
|
|
2065 | int len = eio__realpath (tmpbuf, wd, path); |
|
|
2066 | |
|
|
2067 | path = EIO_INVALID_WD; |
|
|
2068 | |
|
|
2069 | if (len >= 0) |
|
|
2070 | { |
|
|
2071 | ((char *)tmpbuf->ptr)[len] = 0; |
|
|
2072 | path = strdup (tmpbuf->ptr); |
|
|
2073 | } |
|
|
2074 | } |
|
|
2075 | |
|
|
2076 | if (!path) |
|
|
2077 | path = EIO_INVALID_WD; |
|
|
2078 | |
|
|
2079 | return (eio_wd)path; |
|
|
2080 | } |
|
|
2081 | |
|
|
2082 | eio_wd |
|
|
2083 | eio_wd_open_sync (eio_wd wd, const char *path) |
|
|
2084 | { |
|
|
2085 | struct tmpbuf tmpbuf = { 0 }; |
|
|
2086 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
|
|
2087 | free (tmpbuf.ptr); |
|
|
2088 | |
|
|
2089 | return wd; |
|
|
2090 | } |
|
|
2091 | |
|
|
2092 | void |
|
|
2093 | eio_wd_close_sync (eio_wd wd) |
|
|
2094 | { |
|
|
2095 | if (wd != EIO_INVALID_WD) |
|
|
2096 | free (wd); |
|
|
2097 | } |
2129 | } |
2098 | |
2130 | |
2099 | #endif |
2131 | #endif |
2100 | |
2132 | |
2101 | /*****************************************************************************/ |
2133 | /*****************************************************************************/ |
… | |
… | |
2113 | req->result = -1; \ |
2145 | req->result = -1; \ |
2114 | break; \ |
2146 | break; \ |
2115 | } \ |
2147 | } \ |
2116 | } |
2148 | } |
2117 | |
2149 | |
|
|
2150 | static void ecb_noinline ecb_cold |
|
|
2151 | etp_proc_init (void) |
|
|
2152 | { |
|
|
2153 | #if HAVE_PRCTL_SET_NAME |
|
|
2154 | /* provide a more sensible "thread name" */ |
|
|
2155 | char name[16 + 1]; |
|
|
2156 | const int namelen = sizeof (name) - 1; |
|
|
2157 | int len; |
|
|
2158 | |
|
|
2159 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2160 | name [namelen] = 0; |
|
|
2161 | len = strlen (name); |
|
|
2162 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
2163 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
2164 | #endif |
|
|
2165 | } |
|
|
2166 | |
2118 | X_THREAD_PROC (etp_proc) |
2167 | X_THREAD_PROC (etp_proc) |
2119 | { |
2168 | { |
2120 | ETP_REQ *req; |
2169 | ETP_REQ *req; |
2121 | struct timespec ts; |
2170 | struct timespec ts; |
2122 | etp_worker *self = (etp_worker *)thr_arg; |
2171 | etp_worker *self = (etp_worker *)thr_arg; |
2123 | |
2172 | |
2124 | #if HAVE_PRCTL_SET_NAME |
2173 | etp_proc_init (); |
2125 | prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); |
|
|
2126 | #endif |
|
|
2127 | |
2174 | |
2128 | /* try to distribute timeouts somewhat evenly */ |
2175 | /* try to distribute timeouts somewhat evenly */ |
2129 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2176 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
2130 | |
2177 | |
2131 | for (;;) |
2178 | for (;;) |
… | |
… | |
2193 | free (req); |
2240 | free (req); |
2194 | |
2241 | |
2195 | X_LOCK (wrklock); |
2242 | X_LOCK (wrklock); |
2196 | etp_worker_free (self); |
2243 | etp_worker_free (self); |
2197 | X_UNLOCK (wrklock); |
2244 | X_UNLOCK (wrklock); |
2198 | |
|
|
2199 | return 0; |
|
|
2200 | } |
2245 | } |
2201 | |
2246 | |
2202 | /*****************************************************************************/ |
2247 | /*****************************************************************************/ |
2203 | |
2248 | |
2204 | int ecb_cold |
2249 | int ecb_cold |
… | |
… | |
2279 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2324 | req->result = req->wd == EIO_INVALID_WD ? -1 : 0; |
2280 | break; |
2325 | break; |
2281 | case EIO_WD_CLOSE: req->result = 0; |
2326 | case EIO_WD_CLOSE: req->result = 0; |
2282 | eio_wd_close_sync (req->wd); break; |
2327 | eio_wd_close_sync (req->wd); break; |
2283 | |
2328 | |
|
|
2329 | case EIO_SEEK: eio__lseek (req); break; |
2284 | case EIO_READ: ALLOC (req->size); |
2330 | case EIO_READ: ALLOC (req->size); |
2285 | req->result = req->offs >= 0 |
2331 | req->result = req->offs >= 0 |
2286 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2332 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
2287 | : read (req->int1, req->ptr2, req->size); break; |
2333 | : read (req->int1, req->ptr2, req->size); break; |
2288 | case EIO_WRITE: req->result = req->offs >= 0 |
2334 | case EIO_WRITE: req->result = req->offs >= 0 |
… | |
… | |
2304 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
2350 | case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; |
2305 | |
2351 | |
2306 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
2352 | case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; |
2307 | case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
2353 | case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; |
2308 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
2354 | case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; |
2309 | case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2); break; |
2355 | case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
2310 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD (req->int3), req->ptr2, 0); break; |
2356 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
2311 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
2357 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
2312 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
2358 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
2313 | case EIO_READLINK: ALLOC (PATH_MAX); |
2359 | case EIO_READLINK: ALLOC (PATH_MAX); |
2314 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
2360 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
2315 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
2361 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
… | |
… | |
2415 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2461 | case EIO_MTOUCH: req->result = eio__mtouch (req); break; |
2416 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
2462 | case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; |
2417 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2463 | case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; |
2418 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
2464 | case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; |
2419 | |
2465 | |
2420 | case EIO_READDIR: eio__scandir (req); break; |
2466 | case EIO_READDIR: eio__scandir (req, self); break; |
2421 | |
2467 | |
2422 | case EIO_BUSY: |
2468 | case EIO_BUSY: |
2423 | #ifdef _WIN32 |
2469 | #ifdef _WIN32 |
2424 | Sleep (req->nv1 * 1e3); |
2470 | Sleep (req->nv1 * 1e3); |
2425 | #else |
2471 | #else |
… | |
… | |
2444 | case EIO_CUSTOM: |
2490 | case EIO_CUSTOM: |
2445 | req->feed (req); |
2491 | req->feed (req); |
2446 | break; |
2492 | break; |
2447 | |
2493 | |
2448 | default: |
2494 | default: |
2449 | errno = ENOSYS; |
|
|
2450 | req->result = -1; |
2495 | req->result = EIO_ENOSYS (); |
2451 | break; |
2496 | break; |
2452 | } |
2497 | } |
2453 | |
2498 | |
2454 | req->errorno = errno; |
2499 | req->errorno = errno; |
2455 | } |
2500 | } |
2456 | |
2501 | |
2457 | #ifndef EIO_NO_WRAPPERS |
2502 | #ifndef EIO_NO_WRAPPERS |
2458 | |
2503 | |
|
|
2504 | eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) |
|
|
2505 | { |
|
|
2506 | REQ (EIO_WD_OPEN); PATH; SEND; |
|
|
2507 | } |
|
|
2508 | |
|
|
2509 | eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) |
|
|
2510 | { |
|
|
2511 | REQ (EIO_WD_CLOSE); req->wd = wd; SEND; |
|
|
2512 | } |
|
|
2513 | |
2459 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2514 | eio_req *eio_nop (int pri, eio_cb cb, void *data) |
2460 | { |
2515 | { |
2461 | REQ (EIO_NOP); SEND; |
2516 | REQ (EIO_NOP); SEND; |
2462 | } |
2517 | } |
2463 | |
2518 | |
… | |
… | |
2522 | } |
2577 | } |
2523 | |
2578 | |
2524 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2579 | eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) |
2525 | { |
2580 | { |
2526 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
2581 | REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; |
|
|
2582 | } |
|
|
2583 | |
|
|
2584 | eio_req *eio_seek (int fd, off_t offset, int whence, int pri, eio_cb cb, void *data) |
|
|
2585 | { |
|
|
2586 | REQ (EIO_SEEK); req->int1 = fd; req->offs = offset; req->int2 = whence; SEND; |
2527 | } |
2587 | } |
2528 | |
2588 | |
2529 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2589 | eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2530 | { |
2590 | { |
2531 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2591 | REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |