2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/assert.h"
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2022-03-24 07:05:59 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2020-10-11 04:18:53 +00:00
|
|
|
#include "libc/calls/struct/iovec.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/zipos/zipos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads from file at offset, thus avoiding superfluous lseek().
|
|
|
|
*
|
|
|
|
* @param fd is something open()'d earlier, noting pipes might not work
|
|
|
|
* @param buf is copied into, cf. copy_file_range(), sendfile(), etc.
|
|
|
|
* @param size in range [1..0x7ffff000] is reasonable
|
|
|
|
* @param offset is bytes from start of file at which read begins
|
|
|
|
* @return [1..size] bytes on success, 0 on EOF, or -1 w/ errno; with
|
|
|
|
* exception of size==0, in which case return zero means no error
|
|
|
|
* @see pwrite(), write()
|
2021-01-25 21:08:05 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
* @vforksafe
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
ssize_t pread(int fd, void *buf, size_t size, int64_t offset) {
|
|
|
|
ssize_t rc;
|
|
|
|
if (fd == -1 || offset < 0) return einval();
|
2020-11-28 20:01:51 +00:00
|
|
|
if (__isfdkind(fd, kFdZip)) {
|
2021-06-24 19:31:26 +00:00
|
|
|
rc =
|
|
|
|
weaken(__zipos_read)((struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle,
|
|
|
|
(struct iovec[]){{buf, size}}, 1, offset);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else if (!IsWindows()) {
|
2021-02-07 14:11:44 +00:00
|
|
|
rc = sys_pread(fd, buf, size, offset, offset);
|
2020-11-28 20:01:51 +00:00
|
|
|
} else if (__isfdkind(fd, kFdFile)) {
|
2021-02-04 03:35:29 +00:00
|
|
|
rc = sys_read_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, offset);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
|
|
|
rc = ebadf();
|
|
|
|
}
|
2022-03-24 07:05:59 +00:00
|
|
|
assert(rc == -1 || (size_t)rc <= size);
|
|
|
|
STRACE("pread(%d, [%#.*hhs%s], %'zu, %'zd) → %'zd% m", fd,
|
|
|
|
MAX(0, MIN(40, rc)), buf, rc > 40 ? "..." : "", size, offset, rc);
|
2020-06-15 14:18:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|