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

Comparing libeio/eio.c (file contents):
Revision 1.67 by root, Sun Jun 5 20:25:00 2011 UTC vs.
Revision 1.71 by root, Fri Jun 10 12:35:18 2011 UTC

942 942
943/* sendfile always needs emulation */ 943/* sendfile always needs emulation */
944static ssize_t 944static ssize_t
945eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 945eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
946{ 946{
947 ssize_t written = 0;
947 ssize_t res; 948 ssize_t res;
948 949
949 if (!count) 950 if (!count)
950 return 0; 951 return 0;
951 952
953 for (;;)
954 {
952#if HAVE_SENDFILE 955#if HAVE_SENDFILE
953# if __linux 956# if __linux
957 off_t soffset = offset;
954 res = sendfile (ofd, ifd, &offset, count); 958 res = sendfile (ofd, ifd, &soffset, count);
955 959
956# elif __FreeBSD__ 960# elif __FreeBSD__
957 /* 961 /*
958 * Of course, the freebsd sendfile is a dire hack with no thoughts 962 * Of course, the freebsd sendfile is a dire hack with no thoughts
959 * wasted on making it similar to other I/O functions. 963 * wasted on making it similar to other I/O functions.
960 */ 964 */
961 {
962 off_t sbytes; 965 off_t sbytes;
963 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 966 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
964 967
965 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 968 #if 0 /* according to the manpage, this is correct, but broken behaviour */
966 /* freebsd' sendfile will return 0 on success */ 969 /* freebsd' sendfile will return 0 on success */
967 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 970 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
968 /* not on e.g. EIO or EPIPE - sounds broken */ 971 /* not on e.g. EIO or EPIPE - sounds broken */
969 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 972 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
970 res = sbytes; 973 res = sbytes;
971 #endif 974 #endif
972 975
973 /* according to source inspection, this is correct, and useful behaviour */ 976 /* according to source inspection, this is correct, and useful behaviour */
974 if (sbytes) 977 if (sbytes)
975 res = sbytes; 978 res = sbytes;
976 }
977 979
978# elif defined (__APPLE__) 980# elif defined (__APPLE__)
979
980 {
981 off_t sbytes = count; 981 off_t sbytes = count;
982 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 982 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
983 983
984 /* according to the manpage, sbytes is always valid */ 984 /* according to the manpage, sbytes is always valid */
985 if (sbytes) 985 if (sbytes)
986 res = sbytes; 986 res = sbytes;
987 }
988 987
989# elif __hpux 988# elif __hpux
990 res = sendfile (ofd, ifd, offset, count, 0, 0); 989 res = sendfile (ofd, ifd, offset, count, 0, 0);
991 990
992# elif __solaris 991# elif __solaris
993 {
994 struct sendfilevec vec; 992 struct sendfilevec vec;
995 size_t sbytes; 993 size_t sbytes;
996 994
997 vec.sfv_fd = ifd; 995 vec.sfv_fd = ifd;
998 vec.sfv_flag = 0; 996 vec.sfv_flag = 0;
999 vec.sfv_off = offset; 997 vec.sfv_off = offset;
1000 vec.sfv_len = count; 998 vec.sfv_len = count;
1001 999
1002 res = sendfilev (ofd, &vec, 1, &sbytes); 1000 res = sendfilev (ofd, &vec, 1, &sbytes);
1003 1001
1004 if (res < 0 && sbytes) 1002 if (res < 0 && sbytes)
1005 res = sbytes; 1003 res = sbytes;
1006 }
1007 1004
1008# endif 1005# endif
1009 1006
1010#elif defined (_WIN32) 1007#elif defined (_WIN32)
1011
1012 /* does not work, just for documentation of what would need to be done */ 1008 /* does not work, just for documentation of what would need to be done */
1013 { 1009 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1010 /* libeio guarantees that the file offset does not change, and windows */
1011 /* has no way to get an independent handle to the same file description */
1014 HANDLE h = TO_SOCKET (ifd); 1012 HANDLE h = TO_SOCKET (ifd);
1015 SetFilePointer (h, offset, 0, FILE_BEGIN); 1013 SetFilePointer (h, offset, 0, FILE_BEGIN);
1016 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1014 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1017 }
1018 1015
1019#else 1016#else
1020 res = -1; 1017 res = -1;
1021 errno = ENOSYS; 1018 errno = ENOSYS;
1022#endif 1019#endif
1023 1020
1021 /* we assume sendfile can copy at least 128mb in one go */
1022 if (res <= 128 * 1024 * 1024)
1023 {
1024 if (res > 0)
1025 written += res;
1026
1027 if (written)
1028 return written;
1029
1030 break;
1031 }
1032 else
1033 {
1034 /* if we requested more, then probably the kernel was lazy */
1035 written += res;
1036 offset += res;
1037 count -= res;
1038
1039 if (!count)
1040 return written;
1041 }
1042 }
1043
1024 if (res < 0 1044 if (res < 0
1025 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1045 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1026 /* BSDs */ 1046 /* BSDs */
1027#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1047#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1028 || errno == ENOTSUP 1048 || errno == ENOTSUP
1029#endif 1049#endif
1778 case EIO_NOP: 1798 case EIO_NOP:
1779 req->result = 0; 1799 req->result = 0;
1780 break; 1800 break;
1781 1801
1782 case EIO_CUSTOM: 1802 case EIO_CUSTOM:
1783 ((void (*)(eio_req *))req->feed) (req); 1803 req->feed (req);
1784 break; 1804 break;
1785 1805
1786 default: 1806 default:
1787 errno = ENOSYS; 1807 errno = ENOSYS;
1788 req->result = -1; 1808 req->result = -1;
2009eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2029eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2010{ 2030{
2011 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2031 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2012} 2032}
2013 2033
2014eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2034eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2015{ 2035{
2016 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2036 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2017} 2037}
2018 2038
2019#endif 2039#endif
2020 2040
2021eio_req *eio_grp (eio_cb cb, void *data) 2041eio_req *eio_grp (eio_cb cb, void *data)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines