Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_sendfile.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. import errno
  6. import os
  7. import sys
  8. try:
  9. import ctypes
  10. import ctypes.util
  11. except MemoryError:
  12. # selinux execmem denial
  13. # https://bugzilla.redhat.com/show_bug.cgi?id=488396
  14. raise ImportError
  15. SUPPORTED_PLATFORMS = (
  16. 'darwin',
  17. 'freebsd',
  18. 'dragonfly',
  19. 'linux2')
  20. if sys.version_info < (2, 6) or \
  21. sys.platform not in SUPPORTED_PLATFORMS:
  22. raise ImportError("sendfile isn't supported on this platform")
  23. _libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
  24. _sendfile = _libc.sendfile
  25. def sendfile(fdout, fdin, offset, nbytes):
  26. if sys.platform == 'darwin':
  27. _sendfile.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_uint64,
  28. ctypes.POINTER(ctypes.c_uint64), ctypes.c_voidp,
  29. ctypes.c_int]
  30. _nbytes = ctypes.c_uint64(nbytes)
  31. result = _sendfile(fdin, fdout, offset, _nbytes, None, 0)
  32. if result == -1:
  33. e = ctypes.get_errno()
  34. if e == errno.EAGAIN and _nbytes.value is not None:
  35. return _nbytes.value
  36. raise OSError(e, os.strerror(e))
  37. return _nbytes.value
  38. elif sys.platform in ('freebsd', 'dragonfly',):
  39. _sendfile.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_uint64,
  40. ctypes.c_uint64, ctypes.c_voidp,
  41. ctypes.POINTER(ctypes.c_uint64), ctypes.c_int]
  42. _sbytes = ctypes.c_uint64()
  43. result = _sendfile(fdin, fdout, offset, nbytes, None, _sbytes, 0)
  44. if result == -1:
  45. e = ctypes.get_errno()
  46. if e == errno.EAGAIN and _sbytes.value is not None:
  47. return _sbytes.value
  48. raise OSError(e, os.strerror(e))
  49. return _sbytes.value
  50. else:
  51. _sendfile.argtypes = [ctypes.c_int, ctypes.c_int,
  52. ctypes.POINTER(ctypes.c_uint64), ctypes.c_size_t]
  53. _offset = ctypes.c_uint64(offset)
  54. sent = _sendfile(fdout, fdin, _offset, nbytes)
  55. if sent == -1:
  56. e = ctypes.get_errno()
  57. raise OSError(e, os.strerror(e))
  58. return sent