cosmopolitan/libc/stdio/dirstream.c

678 lines
19 KiB
C
Raw Normal View History

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
*/
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
#include "libc/assert.h"
#include "libc/calls/calls.h"
2020-06-15 14:18:57 +00:00
#include "libc/calls/internal.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/syscall-sysv.internal.h"
2022-05-23 22:06:11 +00:00
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/mem/critbit0.h"
2020-06-15 14:18:57 +00:00
#include "libc/mem/mem.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filetype.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/byhandlefileinformation.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/struct/win32finddata.h"
#include "libc/runtime/zipos.internal.h"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
#include "libc/sysv/consts/dt.h"
#include "libc/sysv/consts/f.h"
2020-06-15 14:18:57 +00:00
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/s.h"
2020-06-15 14:18:57 +00:00
#include "libc/sysv/errfuns.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "libc/zip.internal.h"
2020-06-15 14:18:57 +00:00
/**
* @fileoverview Directory Streams for Linux+Mac+Windows+FreeBSD+OpenBSD.
*
* System interfaces for listing the contents of file system directories
* are famously incompatible across platforms. Most native projects that
* have been around a long time implement wrappers for this. Normally it
* will only be for DOS or Windows support. So this is the first time it
* has been done for five platforms, having a remarkably tiny footprint.
*/
int sys_getdents(unsigned, void *, unsigned, long *);
/**
* Directory stream object.
*/
2020-06-15 14:18:57 +00:00
struct dirstream {
int fd;
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
bool iszip;
long tell;
int64_t hand;
pthread_mutex_t lock;
struct dirent ent;
2020-06-15 14:18:57 +00:00
union {
struct {
struct Zipos *zipos;
uint64_t inode;
uint64_t offset;
uint64_t records;
struct ZiposUri prefix;
struct critbit0 found;
} zip;
2020-06-15 14:18:57 +00:00
struct {
unsigned buf_pos;
unsigned buf_end;
uint64_t buf[4096];
2020-06-15 14:18:57 +00:00
};
struct {
bool isdone;
struct NtWin32FindData windata;
char16_t name16[PATH_MAX];
uint32_t name16len;
2020-06-15 14:18:57 +00:00
};
};
};
/**
* FreeBSD getdents() and XNU getdirentries() ABI.
*/
struct dirent_bsd {
uint32_t d_fileno;
uint16_t d_reclen;
uint8_t d_type;
uint8_t d_namlen;
char d_name[256];
};
/**
* OpenBSD getdents() ABI.
*/
struct dirent_openbsd {
uint64_t d_fileno;
int64_t d_off;
uint16_t d_reclen;
uint8_t d_type;
uint8_t d_namlen;
uint8_t __zomg[4];
char d_name[256];
};
2021-02-05 17:44:54 +00:00
/**
* NetBSD getdents().
*/
struct dirent_netbsd {
uint64_t d_fileno;
uint16_t d_reclen;
uint16_t d_namlen;
uint8_t d_type;
char d_name[512];
};
static void lockdir(DIR *dir) {
if (__threaded) {
pthread_mutex_lock(&dir->lock);
}
}
static void unlockdir(DIR *dir) {
if (__threaded) {
pthread_mutex_unlock(&dir->lock);
2021-02-08 12:04:42 +00:00
}
}
static textwindows dontinline int fdopendir_nt(DIR *res, int fd) {
if (!__isfdkind(fd, kFdFile)) {
return ebadf();
}
res->name16len = GetFinalPathNameByHandle(
g_fds.p[fd].handle, res->name16, ARRAYLEN(res->name16),
kNtFileNameNormalized | kNtVolumeNameDos);
if (!res->name16len) {
return __winerr();
}
if (res->name16len + 2 + 1 > ARRAYLEN(res->name16)) {
return enametoolong();
2020-06-15 14:18:57 +00:00
}
if (res->name16len > 1 && res->name16[res->name16len - 1] != u'\\') {
res->name16[res->name16len++] = u'\\';
}
res->name16[res->name16len++] = u'*';
res->name16[res->name16len] = u'\0';
if ((res->hand = FindFirstFile(res->name16, &res->windata)) == -1) {
return __fix_enotdir(-1, res->name16);
}
return 0;
2020-06-15 14:18:57 +00:00
}
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
static textwindows uint8_t GetNtDirentType(struct NtWin32FindData *w) {
switch (w->dwFileType) {
case kNtFileTypeDisk:
return DT_BLK;
case kNtFileTypeChar:
return DT_CHR;
case kNtFileTypePipe:
return DT_FIFO;
default:
if (w->dwFileAttributes & kNtFileAttributeDirectory) {
return DT_DIR;
} else if (w->dwFileAttributes & kNtFileAttributeReparsePoint) {
return DT_LNK;
Add SSL to redbean Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
2021-06-24 19:31:26 +00:00
} else {
return DT_REG;
}
}
}
static textwindows dontinline struct dirent *readdir_nt(DIR *dir) {
if (dir->isdone) {
return NULL;
}
// join absolute path that's already normalized
uint64_t ino = 0;
char16_t jp[PATH_MAX];
size_t i = dir->name16len - 1; // foo\* -> foo\ (strip star)
memcpy(jp, dir->name16, i * sizeof(char16_t));
char16_t *p = dir->windata.cFileName;
if (p[0] == u'.' && p[1] == u'\0') {
// join("foo\\", ".") -> "foo\\"
} else if (p[0] == u'.' && p[1] == u'.' && p[2] == u'\0') {
if (i == 7 && //
jp[0] == '\\' && //
jp[1] == '\\' && //
jp[2] == '?' && //
jp[3] == '\\' && //
jp[5] == ':' && //
jp[6] == '\\') {
// e.g. \\?\C:\ stays the same
} else {
--i; // foo\bar\ -> foo\ (parent)
while (i && jp[i - 1] != '\\') --i;
}
} else {
while (*p) {
if (i + 1 < ARRAYLEN(jp)) {
jp[i++] = *p++;
} else {
// ignore errors and set inode to zero
goto GiveUpOnGettingInode;
}
}
}
jp[i] = u'\0';
// get inode such that it's consistent with stat()
// it's important that we not follow symlinks here
int64_t fh = CreateFile(jp, kNtFileReadAttributes, 0, 0, kNtOpenExisting,
kNtFileAttributeNormal | kNtFileFlagBackupSemantics |
kNtFileFlagOpenReparsePoint,
0);
if (fh != kNtInvalidHandleValue) {
struct NtByHandleFileInformation wst;
if (GetFileInformationByHandle(fh, &wst)) {
ino = (uint64_t)wst.nFileIndexHigh << 32 | wst.nFileIndexLow;
}
CloseHandle(fh);
} else {
// ignore errors and set inode to zero
STRACE("failed to get inode of path join(%#hs, %#hs) -> %#hs %m",
dir->name16, dir->windata.cFileName, jp);
}
GiveUpOnGettingInode:
// create result object
bzero(&dir->ent, sizeof(dir->ent));
dir->ent.d_ino = ino;
dir->ent.d_off = dir->tell++;
tprecode16to8(dir->ent.d_name, sizeof(dir->ent.d_name),
dir->windata.cFileName);
dir->ent.d_type = GetNtDirentType(&dir->windata);
dir->isdone = !FindNextFile(dir->hand, &dir->windata);
return &dir->ent;
}
/**
* Creates directory object for file descriptor.
*
* @param fd gets owned by this function, if it succeeds
* @return new directory object, which must be freed by closedir(),
* or NULL w/ errno
* @errors ENOMEM and fd is closed
*/
DIR *fdopendir(int fd) {
// sanity check file descriptor
struct stat st;
if (fstat(fd, &st) == -1) {
return 0;
}
if (!S_ISDIR(st.st_mode)) {
enotdir();
return 0;
}
if (IsLinux() && (__sys_fcntl(fd, F_GETFL) & O_PATH)) {
ebadf();
return 0;
}
// allocate directory iterator object
DIR *dir;
if (!(dir = calloc(1, sizeof(*dir)))) {
return NULL;
}
// on unix, file descriptor isn't required to be tracked
dir->fd = fd;
if (!__isfdkind(fd, kFdZip)) {
if (IsWindows()) {
if (!fdopendir_nt(dir, fd)) {
return dir;
} else {
free(dir);
return 0;
}
}
return dir;
}
dir->iszip = true;
// ensure open /zip/... file is a directory
struct ZiposHandle *h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle;
if (h->cfile != ZIPOS_SYNTHETIC_DIRECTORY &&
!S_ISDIR(GetZipCfileMode(h->zipos->map + h->cfile))) {
free(dir);
enotdir();
return 0;
}
// get path of this file descriptor and ensure trailing slash
size_t len;
const char *name;
if (h->cfile != ZIPOS_SYNTHETIC_DIRECTORY) {
len = ZIP_CFILE_NAMESIZE(h->zipos->map + h->cfile);
name = ZIP_CFILE_NAME(h->zipos->map + h->cfile);
} else {
len = h->size;
name = (const char *)h->data;
}
if (len + 2 > ZIPOS_PATH_MAX) {
free(dir);
enametoolong();
return 0;
}
if (len) memcpy(dir->zip.prefix.path, name, len);
if (len && dir->zip.prefix.path[len - 1] != '/') {
dir->zip.prefix.path[len++] = '/';
}
dir->zip.prefix.path[len] = 0;
dir->zip.prefix.len = len;
// setup state values for directory iterator
dir->zip.zipos = h->zipos;
dir->zip.offset = GetZipCdirOffset(h->zipos->cdir);
dir->zip.records = GetZipCdirRecords(h->zipos->cdir);
dir->zip.inode = __zipos_inode(h->zipos, h->cfile, dir->zip.prefix.path,
dir->zip.prefix.len);
return dir;
}
2020-06-15 14:18:57 +00:00
/**
* Opens directory, e.g.
*
2020-12-26 10:09:07 +00:00
* DIR *d;
* struct dirent *e;
* d = opendir(path);
2020-12-26 10:09:07 +00:00
* while ((e = readdir(d))) {
* printf("%s/%s\n", path, e->d_name);
* }
* closedir(d);
2020-06-15 14:18:57 +00:00
*
* @returns newly allocated DIR object, or NULL w/ errno
* @errors ENOENT, ENOTDIR, EACCES, EMFILE, ENFILE, ENOMEM
* @raise ECANCELED if thread was cancelled in masked mode
* @raise EINTR if we needed to block and a signal was delivered instead
* @cancellationpoint
* @see glob()
2020-06-15 14:18:57 +00:00
*/
DIR *opendir(const char *name) {
int rc;
if (_weaken(pthread_testcancel_np) &&
(rc = _weaken(pthread_testcancel_np)())) {
errno = rc;
return 0;
}
int fd;
if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_NOCTTY | O_CLOEXEC)) == -1) {
return 0;
2020-06-15 14:18:57 +00:00
}
DIR *res = fdopendir(fd);
if (!res) close(fd);
return res;
2020-06-15 14:18:57 +00:00
}
static struct dirent *readdir_zipos(DIR *dir) {
struct dirent *ent = 0;
while (!ent && dir->tell < dir->zip.records + 2) {
if (!dir->tell) {
ent = &dir->ent;
ent->d_off = dir->tell;
ent->d_ino = dir->zip.inode;
ent->d_type = DT_DIR;
ent->d_name[0] = '.';
ent->d_name[1] = 0;
} else if (dir->tell == 1) {
2021-02-05 17:44:54 +00:00
ent = &dir->ent;
ent->d_off = dir->tell;
ent->d_type = DT_DIR;
ent->d_name[0] = '.';
ent->d_name[1] = '.';
ent->d_name[2] = 0;
struct ZiposUri p;
p.len = dir->zip.prefix.len;
if (p.len) memcpy(p.path, dir->zip.prefix.path, p.len);
while (p.len && p.path[p.len - 1] == '/') --p.len;
while (p.len && p.path[p.len - 1] != '/') --p.len;
while (p.len && p.path[p.len - 1] == '/') --p.len;
p.path[p.len] = 0;
ent->d_ino = __zipos_inode(
dir->zip.zipos, __zipos_scan(dir->zip.zipos, &p), p.path, p.len);
} else {
const char *s = ZIP_CFILE_NAME(dir->zip.zipos->map + dir->zip.offset);
size_t n = ZIP_CFILE_NAMESIZE(dir->zip.zipos->map + dir->zip.offset);
if (n > dir->zip.prefix.len &&
!memcmp(dir->zip.prefix.path, s, dir->zip.prefix.len)) {
s += dir->zip.prefix.len;
n -= dir->zip.prefix.len;
const char *p = memchr(s, '/', n);
if (p) n = p - s;
if ((n = MIN(n, sizeof(ent->d_name) - 1)) &&
critbit0_emplace(&dir->zip.found, s, n) == 1) {
ent = &dir->ent;
ent->d_ino = dir->zip.offset;
ent->d_off = dir->tell;
ent->d_type =
S_ISDIR(GetZipCfileMode(dir->zip.zipos->map + dir->zip.offset))
? DT_DIR
: DT_REG;
memcpy(ent->d_name, s, n);
ent->d_name[n] = 0;
}
}
dir->zip.offset +=
ZIP_CFILE_HDRSIZE(dir->zip.zipos->map + dir->zip.offset);
}
dir->tell++;
}
return ent;
}
static struct dirent *readdir_unix(DIR *dir) {
if (dir->buf_pos >= dir->buf_end) {
long basep = dir->tell;
if (IsNetbsd() || IsFreebsd()) {
unsigned long seeky = lseek(dir->fd, 0, SEEK_CUR);
unassert(seeky <= INT_MAX);
dir->tell = seeky << 32;
}
int rc = sys_getdents(dir->fd, dir->buf, sizeof(dir->buf), &basep);
STRACE("sys_getdents(%d) → %d% m", dir->fd, rc);
if (!rc || rc == -1) {
return NULL;
}
dir->buf_pos = 0;
dir->buf_end = rc;
}
struct dirent *ent;
if (IsLinux()) {
ent = (struct dirent *)((char *)dir->buf + dir->buf_pos);
dir->buf_pos += ent->d_reclen;
dir->tell = ent->d_off;
} else if (IsOpenbsd()) {
struct dirent_openbsd *obsd =
(struct dirent_openbsd *)((char *)dir->buf + dir->buf_pos);
dir->buf_pos += obsd->d_reclen;
dir->tell = obsd->d_off;
ent = &dir->ent;
ent->d_ino = obsd->d_fileno;
ent->d_off = obsd->d_off;
ent->d_reclen = obsd->d_reclen;
ent->d_type = obsd->d_type;
memcpy(ent->d_name, obsd->d_name, obsd->d_namlen + 1);
} else if (IsNetbsd()) {
struct dirent_netbsd *nbsd =
(struct dirent_netbsd *)((char *)dir->buf + dir->buf_pos);
dir->buf_pos += nbsd->d_reclen;
ent = &dir->ent;
ent->d_ino = nbsd->d_fileno;
ent->d_off = (dir->tell += nbsd->d_reclen);
ent->d_reclen = nbsd->d_reclen;
ent->d_type = nbsd->d_type;
size_t n =
MIN(nbsd->d_namlen, MIN(sizeof(ent->d_name) - 1, sizeof(nbsd->d_name)));
memcpy(ent->d_name, nbsd->d_name, n);
ent->d_name[n] = 0;
2020-06-15 14:18:57 +00:00
} else {
struct dirent_bsd *bsd =
(struct dirent_bsd *)((char *)dir->buf + dir->buf_pos);
dir->buf_pos += bsd->d_reclen;
ent = &dir->ent;
ent->d_ino = bsd->d_fileno;
ent->d_off = dir->tell++;
ent->d_reclen = bsd->d_reclen;
ent->d_type = bsd->d_type;
memcpy(ent->d_name, bsd->d_name, bsd->d_namlen + 1);
}
if (ent) {
ent->d_reclen =
ROUNDUP(offsetof(struct dirent, d_name) + strlen(ent->d_name) + 1, 8);
}
return ent;
}
static struct dirent *readdir_impl(DIR *dir) {
if (dir->iszip) {
return readdir_zipos(dir);
} else if (IsWindows()) {
return readdir_nt(dir);
} else {
return readdir_unix(dir);
2020-06-15 14:18:57 +00:00
}
}
/**
* Reads next entry from directory stream.
*
* This API doesn't define any particular ordering.
*
* @param dir is the object opendir() or fdopendir() returned
* @return next entry or NULL on end or error, which can be
* differentiated by setting errno to 0 beforehand
*/
struct dirent *readdir(DIR *dir) {
struct dirent *e;
if (dir) {
lockdir(dir);
e = readdir_impl(dir);
unlockdir(dir);
} else {
efault();
e = 0;
}
return e;
}
/**
* Reads directory entry reentrantly.
*
* @param dir is the object opendir() or fdopendir() returned
* @param output is where directory entry is copied if not eof
* @param result will receive `output` pointer, or null on eof
* @return 0 on success, or errno on error
* @returnserrno
* @threadsafe
*/
errno_t readdir_r(DIR *dir, struct dirent *output, struct dirent **result) {
int err, olderr;
struct dirent *entry;
lockdir(dir);
olderr = errno;
errno = 0;
entry = readdir_impl(dir);
err = errno;
errno = olderr;
if (err) {
unlockdir(dir);
return err;
}
if (entry) {
if (entry->d_reclen) {
memcpy(output, entry, entry->d_reclen);
}
} else {
output = 0;
}
unlockdir(dir);
*result = output;
return 0;
}
2020-06-15 14:18:57 +00:00
/**
* Closes directory object returned by opendir().
* @return 0 on success or -1 w/ errno
*/
int closedir(DIR *dir) {
int rc = 0;
2020-06-15 14:18:57 +00:00
if (dir) {
if (dir->iszip) {
critbit0_clear(&dir->zip.found);
}
if (dir->fd != -1) {
rc |= close(dir->fd);
}
if (IsWindows() && !dir->iszip) {
if (!FindClose(dir->hand)) {
rc = __winerr();
}
2020-06-15 14:18:57 +00:00
}
free(dir);
}
return rc;
}
/**
* Returns offset into directory data.
* @threadsafe
2020-06-15 14:18:57 +00:00
*/
long telldir(DIR *dir) {
long rc;
lockdir(dir);
rc = dir->tell;
unlockdir(dir);
return rc;
2020-06-15 14:18:57 +00:00
}
/**
* Returns file descriptor associated with DIR object.
* @threadsafe
2020-06-15 14:18:57 +00:00
*/
int dirfd(DIR *dir) {
return dir->fd;
2020-06-15 14:18:57 +00:00
}
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 07:42:14 +00:00
/**
* Seeks to beginning of directory stream.
* @threadsafe
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 07:42:14 +00:00
*/
void rewinddir(DIR *dir) {
lockdir(dir);
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 07:42:14 +00:00
if (dir->iszip) {
critbit0_clear(&dir->zip.found);
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 07:42:14 +00:00
dir->tell = 0;
dir->zip.offset = GetZipCdirOffset(dir->zip.zipos->cdir);
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 07:42:14 +00:00
} else if (!IsWindows()) {
if (!lseek(dir->fd, 0, SEEK_SET)) {
dir->buf_pos = dir->buf_end = 0;
dir->tell = 0;
}
} else {
FindClose(dir->hand);
if ((dir->hand = FindFirstFile(dir->name16, &dir->windata)) != -1) {
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 07:42:14 +00:00
dir->isdone = false;
dir->tell = 0;
} else {
dir->isdone = true;
}
}
unlockdir(dir);
}
/**
* Seeks in directory stream.
* @threadsafe
*/
void seekdir(DIR *dir, long tell) {
lockdir(dir);
if (dir->iszip) {
critbit0_clear(&dir->zip.found);
dir->tell = 0;
dir->zip.offset = GetZipCdirOffset(dir->zip.zipos->cdir);
while (dir->tell < tell) {
if (!readdir_zipos(dir)) {
break;
}
}
} else if (IsNetbsd() || IsFreebsd()) {
dir->buf_pos = dir->buf_end = 0;
dir->tell = lseek(dir->fd, tell >> 32, SEEK_SET) << 32;
while (dir->tell < tell) {
if (!readdir_unix(dir)) {
break;
}
}
} else if (!IsWindows()) {
dir->tell = lseek(dir->fd, tell, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
} else {
dir->tell = 0;
dir->isdone = false;
FindClose(dir->hand);
if ((dir->hand = FindFirstFile(dir->name16, &dir->windata)) != -1) {
for (; dir->tell < tell; ++dir->tell) {
if (!FindNextFile(dir->hand, &dir->windata)) {
dir->isdone = true;
break;
}
}
} else {
dir->isdone = true;
}
}
unlockdir(dir);
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 07:42:14 +00:00
}
__weak_reference(readdir, readdir64);