2018-05-16 08:22:34 +00:00
|
|
|
#
|
|
|
|
# Asyncdispatch2 SendFile
|
|
|
|
# (c) Copyright 2018
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
## This module provides cross-platform wrapper for ``sendfile()`` syscall.
|
|
|
|
|
|
|
|
when defined(nimdoc):
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
|
|
|
## Copies data between file descriptor ``infd`` and ``outfd``. Because this
|
|
|
|
## copying is done within the kernel, ``sendfile()`` is more efficient than
|
|
|
|
## the combination of ``read(2)`` and ``write(2)``, which would require
|
|
|
|
## transferring data to and from user space.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``infd`` should be a file descriptor opened for reading and
|
|
|
|
## ``outfd`` should be a descriptor opened for writing.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## The ``infd`` argument must correspond to a file which supports
|
|
|
|
## ``mmap(2)``-like operations (i.e., it cannot be a socket).
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``offset`` the file offset from which ``sendfile()`` will start reading
|
|
|
|
## data from ``infd``.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## ``count`` is the number of bytes to copy between the file descriptors.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## If the transfer was successful, the number of bytes written to ``outfd``
|
|
|
|
## is returned. Note that a successful call to ``sendfile()`` may write
|
|
|
|
## fewer bytes than requested; the caller should be prepared to retry the
|
|
|
|
## call if there were unsent bytes.
|
2018-05-29 09:59:39 +00:00
|
|
|
##
|
2018-05-16 08:22:34 +00:00
|
|
|
## On error, ``-1`` is returned.
|
|
|
|
|
|
|
|
when defined(linux) or defined(android):
|
|
|
|
|
|
|
|
proc osSendFile*(outfd, infd: cint, offset: ptr int, count: int): int
|
|
|
|
{.importc: "sendfile", header: "<sys/sendfile.h>".}
|
|
|
|
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
|
|
|
var o = offset
|
2018-05-21 21:52:57 +00:00
|
|
|
result = osSendFile(cint(outfd), cint(infd), addr o, count)
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
|
|
|
defined(dragonflybsd):
|
|
|
|
|
|
|
|
type
|
2018-05-21 21:52:57 +00:00
|
|
|
SendfileHeader* = object {.importc: "sf_hdtr",
|
2018-05-16 08:22:34 +00:00
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""",
|
|
|
|
pure, final.}
|
|
|
|
|
2018-05-21 21:52:57 +00:00
|
|
|
proc osSendFile*(outfd, infd: cint, offset: uint, size: uint,
|
|
|
|
hdtr: ptr SendfileHeader, sbytes: ptr uint,
|
2018-05-16 08:22:34 +00:00
|
|
|
flags: int): int {.importc: "sendfile",
|
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""".}
|
|
|
|
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
2018-05-21 21:52:57 +00:00
|
|
|
var o = 0'u
|
|
|
|
if osSendFile(cint(infd), cint(outfd), uint(offset), uint(count), nil,
|
|
|
|
addr o, 0) == 0:
|
|
|
|
result = int(o)
|
|
|
|
else:
|
|
|
|
result = -1
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(macosx):
|
2018-05-21 21:52:57 +00:00
|
|
|
import posix
|
2018-05-16 08:22:34 +00:00
|
|
|
type
|
2018-05-21 21:52:57 +00:00
|
|
|
SendfileHeader* = object {.importc: "sf_hdtr",
|
2018-05-16 08:22:34 +00:00
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""",
|
|
|
|
pure, final.}
|
|
|
|
|
|
|
|
proc osSendFile*(fd, s: cint, offset: int, size: ptr int,
|
2018-05-21 21:52:57 +00:00
|
|
|
hdtr: ptr SendfileHeader,
|
2018-05-16 08:22:34 +00:00
|
|
|
flags: int): int {.importc: "sendfile",
|
|
|
|
header: """#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>""".}
|
|
|
|
|
|
|
|
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
2018-05-21 21:52:57 +00:00
|
|
|
var o = count
|
|
|
|
if osSendFile(cint(infd), cint(outfd), offset, addr o, nil, 0) == 0:
|
|
|
|
result = o
|
|
|
|
else:
|
|
|
|
result = -1
|