summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2012-01-03 19:23:25 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2012-01-03 19:23:25 +0000
commit40b92f2c4dce74477e0de4e0f073828a76bbc92c (patch)
treec59d51c511ea34b45350184dc6bb22d125e3a187
parent4b31d37b59949c2f375b05e39f75d33fc714a544 (diff)
downloadpysendfile-40b92f2c4dce74477e0de4e0f073828a76bbc92c.tar.gz
#11: get rid of header/trailer on Linux via TCP_CORK; thinking back it's not a good idea as we're messing with C types when returning
-rw-r--r--HISTORY1
-rw-r--r--sendfilemodule.c84
-rw-r--r--setup.py1
-rw-r--r--test/test_sendfile.py6
4 files changed, 11 insertions, 81 deletions
diff --git a/HISTORY b/HISTORY
index 1eb90f2..b9ed22f 100644
--- a/HISTORY
+++ b/HISTORY
@@ -13,7 +13,6 @@ Version 2.0.0 - XXXX-XX-XX
#6: exposed SF_NODISKIO, SF_MNOWAIT and SF_SYNC constants on FreeBSD
#8: benchmark script
#10: OSX support
-#11: Linux header and trailer support via TCP_CORK socket option
Version 1.2.4 - 2009-03-06
==========================
diff --git a/sendfilemodule.c b/sendfilemodule.c
index 4e347cc..c4d8798 100644
--- a/sendfilemodule.c
+++ b/sendfilemodule.c
@@ -235,107 +235,40 @@ method_sendfile(PyObject *self, PyObject *args)
#define SOL_TCP 6
#define TCP_CORK 3
+
static PyObject *
method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
{
int out_fd, in_fd;
off_t offset;
size_t nbytes;
- char * head = NULL;
- size_t head_len = 0;
- char * tail = NULL;
- size_t tail_len = 0;
- int orig_cork = 1;
- int orig_cork_len = sizeof(int);
- int ret;
- ssize_t sent_h = 0;
- ssize_t sent_f = 0;
- ssize_t sent_t = 0;
+ ssize_t sent = 0;
PyObject *offobj;
- static char *keywords[] = {"out", "in", "offset", "nbytes", "header",
- "trailer", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict,
- "iiOI|s#s#:sendfile",
- keywords, &out_fd, &in_fd, &offobj,
- &nbytes, &head, &head_len, &tail,
- &tail_len)) {
+ if (!PyArg_ParseTuple(args, "iiOI", &out_fd, &in_fd, &offobj, &nbytes)) {
return NULL;
}
- if (head_len != 0 || tail_len != 0) {
- int cork = 1;
- // first, fetch the original setting
- Py_BEGIN_ALLOW_THREADS
- ret = getsockopt(out_fd, SOL_TCP, TCP_CORK,
- (void*)&orig_cork, (socklen_t*)&orig_cork_len);
- Py_END_ALLOW_THREADS
- if (ret == -1)
- return PyErr_SetFromErrno(PyExc_OSError);
- Py_BEGIN_ALLOW_THREADS
- ret = setsockopt(out_fd, SOL_TCP, TCP_CORK, (void*)&cork, sizeof(cork));
- Py_END_ALLOW_THREADS
- if (ret == -1)
- return PyErr_SetFromErrno(PyExc_OSError);
- }
-
- // send header
- if (head_len != 0) {
- Py_BEGIN_ALLOW_THREADS
- sent_h = send(out_fd, head, head_len, 0);
- Py_END_ALLOW_THREADS
- if (sent_h < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
- else if (sent_h == 0) {
- // A return value of 0 is supposed to be interpreted as EOF
- // being reached. We do not want that and raise EAGAIN instead.
- errno = EAGAIN;
- return PyErr_SetFromErrno(PyExc_OSError);
- }
- else if (sent_h < head_len)
- goto done;
- }
-
- // send file
if (offobj == Py_None) {
Py_BEGIN_ALLOW_THREADS;
- sent_f = sendfile(out_fd, in_fd, NULL, nbytes);
+ sent = sendfile(out_fd, in_fd, NULL, nbytes);
Py_END_ALLOW_THREADS;
}
else {
if (!_parse_off_t(offobj, &offset))
return NULL;
Py_BEGIN_ALLOW_THREADS;
- sent_f = sendfile(out_fd, in_fd, &offset, nbytes);
+ sent = sendfile(out_fd, in_fd, &offset, nbytes);
Py_END_ALLOW_THREADS;
}
- if (sent_f == -1)
+ if (sent == -1)
return PyErr_SetFromErrno(PyExc_OSError);
- // send trailer
- if (tail_len != 0) {
- Py_BEGIN_ALLOW_THREADS
- sent_t = send(out_fd, tail, tail_len, 0);
- Py_END_ALLOW_THREADS
- if (sent_t < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
- else if (sent_t == 0) {
- // A return value of 0 is supposed to be interpreted as EOF
- // being reached. We do not want that and raise EAGAIN instead.
- errno = EAGAIN;
- return PyErr_SetFromErrno(PyExc_OSError);
- }
- }
-
- goto done;
-
-done:
#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 5)
- return Py_BuildValue("n", sent_h + sent_f + sent_t);
+ return Py_BuildValue("n", sent);
#else
- return Py_BuildValue("l", (long)sent_h + (long)sent_f + (long)sent_t);
+ return Py_BuildValue("l", (long)sent);
#endif
}
/* --- end Linux --- */
@@ -472,4 +405,3 @@ void initsendfile(void)
return module;
#endif
}
-
diff --git a/setup.py b/setup.py
index 39bfe6f..431c123 100644
--- a/setup.py
+++ b/setup.py
@@ -58,4 +58,3 @@ def main():
if __name__ == '__main__':
main()
-
diff --git a/test/test_sendfile.py b/test/test_sendfile.py
index 10af5df..f72b272 100644
--- a/test/test_sendfile.py
+++ b/test/test_sendfile.py
@@ -30,10 +30,10 @@ TESTFN3 = TESTFN + "3"
DATA = _bytes("12345abcde" * 1024 * 1024) # 10 Mb
HOST = '127.0.0.1'
BIGFILE_SIZE = 2500000000 # > 2GB file (2GB = 2147483648 bytes)
-if "sunos" not in sys.platform:
- SUPPORT_HEADER_TRAILER = True
-else:
+if "sunos" in sys.platform or "linux" in sys.platform:
SUPPORT_HEADER_TRAILER = False
+else:
+ SUPPORT_HEADER_TRAILER = True
class Handler(asynchat.async_chat):