cosmopolitan/libc/sock/sendfile.c
Justine Tunney b420ed8248 Undiamond Python headers
This change gets the Python codebase into a state where it conforms to
the conventions of this codebase. It's now possible to include headers
from Python, without worrying about ordering. Python has traditionally
solved that problem by "diamonding" everything in Python.h, but that's
problematic since it means any change to any Python header invalidates
all the build artifacts. Lastly it makes tooling not work. Since it is
hard to explain to Emacs when I press C-c C-h to add an import line it
shouldn't add the header that actually defines the symbol, and instead
do follow the nonstandard Python convention.

Progress has been made on letting Python load source code from the zip
executable structure via the standard C library APIs. System calss now
recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME
since Python uses colon as its delimiter.

Some progress has been made on embedding the notice license terms into
the Python object code. This is easier said than done since Python has
an extremely complicated ownership story.

- Some termios APIs have been added
- Implement rewinddir() dirstream API
- GetCpuCount() API added to Cosmopolitan Libc
- More bugs in Cosmopolitan Libc have been fixed
- zipobj.com now has flags for mangling the path
- Fixed bug a priori with sendfile() on certain BSDs
- Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms
- FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes
- APE now supports a hybrid solution to no-self-modify for builds
- Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 14:07:40 -07:00

101 lines
4.7 KiB
C

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
static textwindows ssize_t sendfile_linux2nt(int outfd, int infd,
int64_t *inout_opt_inoffset,
size_t uptobytes) {
struct NtOverlapped Overlapped;
struct NtOverlapped *lpOverlapped;
if (!__isfdkind(outfd, kFdSocket) || !__isfdkind(outfd, kFdFile))
return ebadf();
if (inout_opt_inoffset) {
memset(&Overlapped, 0, sizeof(Overlapped));
Overlapped.Pointer = (void *)(intptr_t)(*inout_opt_inoffset);
lpOverlapped = &Overlapped;
} else {
lpOverlapped = NULL;
}
/* TODO(jart): Fetch this on a per-socket basis via GUID. */
if (TransmitFile(g_fds.p[outfd].handle, g_fds.p[infd].handle, uptobytes, 0,
lpOverlapped, NULL, 0)) {
return uptobytes;
} else {
return __winsockerr();
}
}
static ssize_t sendfile_linux2bsd(int outfd, int infd,
int64_t *inout_opt_inoffset,
size_t uptobytes) {
int rc;
int64_t offset, sbytes;
if (inout_opt_inoffset) {
offset = *inout_opt_inoffset;
} else if ((offset = lseek(infd, 0, SEEK_CUR)) == -1) {
return -1;
}
if (IsFreebsd()) {
rc = sys_sendfile_freebsd(infd, outfd, offset, uptobytes, 0, &sbytes, 0);
} else {
rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0);
}
if (rc != -1) {
if (inout_opt_inoffset) *inout_opt_inoffset += sbytes;
return sbytes;
} else {
return -1;
}
}
/**
* Transfers data from file to network.
*
* @param outfd needs to be a socket
* @param infd needs to be a file
* @param inout_opt_inoffset may be specified for pread()-like behavior
* @param uptobytes is usually the number of bytes remaining in file; it
* can't exceed INT_MAX-1; some platforms block until everything's
* sent, whereas others won't; zero isn't allowed
* @return number of bytes transmitted which may be fewer than requested
* @see copy_file_range() for file ↔ file
* @see splice() for fd ↔ pipe
*/
ssize_t sendfile(int outfd, int infd, int64_t *inout_opt_inoffset,
size_t uptobytes) {
if (!uptobytes) return einval();
if (uptobytes > 0x7ffffffe /* Microsoft's off-by-one */) return eoverflow();
if (IsModeDbg() && uptobytes > 1) uptobytes >>= 1;
if (IsLinux()) {
return sys_sendfile(outfd, infd, inout_opt_inoffset, uptobytes);
} else if (IsFreebsd() || IsXnu()) {
return sendfile_linux2bsd(outfd, infd, inout_opt_inoffset, uptobytes);
} else if (IsWindows()) {
return sendfile_linux2nt(outfd, infd, inout_opt_inoffset, uptobytes);
} else {
return copyfd(infd, inout_opt_inoffset, outfd, NULL, uptobytes, 0);
}
}