Add epoll and do more release readiness changes

This change also pays off some of the remaining technical debt with
stdio, file descriptors, and memory managemnt polyfills.
This commit is contained in:
Justine Tunney 2020-11-28 12:01:51 -08:00
parent a9ea949df8
commit 3e4fd4b0ad
271 changed files with 5706 additions and 1365 deletions

View file

@ -20,17 +20,23 @@
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/mem/mem.h"
#include "libc/zipos/zipos.h"
#include "libc/nt/runtime.h"
#include "libc/zipos/zipos.internal.h"
/**
* Closes compressed object.
*
* @param fd is vetted by close()
*/
int __zipos_close(struct ZiposHandle *h) {
if (h) {
free(h->map);
free(h);
int __zipos_close(int fd) {
struct ZiposHandle *h;
h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle;
if (!IsWindows()) {
close$sysv(fd);
} else {
CloseHandle(h->handle);
}
free(h->freeme);
free(h);
return 0;
}

View file

@ -23,7 +23,7 @@
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
ssize_t __zipos_find(struct Zipos *zipos, const struct ZiposUri *name) {
size_t i, cf;

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
/**
* Reads file metadata from αcτµαlly pδrταblε εxεcµταblε object store.

View file

@ -26,7 +26,7 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
/**
* Returns pointer to zip central directory of current executable.
@ -38,25 +38,18 @@ struct Zipos *__zipos_get(void) {
size_t mapsize;
uint8_t *cdir;
void *map;
int fd;
if (!once) {
if (ZIP_CDIR_MAGIC(__zip_end) == kZipCdirHdrMagic) {
zipos.map = _base;
zipos.cdir = __zip_end;
} else {
exe = (const char *)getauxval(AT_EXECFN);
if ((fd = open(exe, O_RDONLY)) != -1) {
if ((mapsize = getfiledescriptorsize(fd)) != SIZE_MAX &&
(map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED, fd, 0)) !=
MAP_FAILED) {
if ((cdir = zipfindcentraldir(map, mapsize))) {
zipos.map = map;
zipos.cdir = cdir;
} else {
munmap(map, mapsize);
}
exe = (const char *)getauxval(AT_EXECFN);
if ((zipos.fd = open(exe, O_RDONLY)) != -1) {
if ((mapsize = getfiledescriptorsize(zipos.fd)) != SIZE_MAX &&
(map = mmap(NULL, mapsize, PROT_READ, MAP_SHARED, zipos.fd, 0)) !=
MAP_FAILED) {
if ((cdir = zipfindcentraldir(map, mapsize))) {
zipos.map = map;
zipos.cdir = cdir;
} else {
munmap(map, mapsize);
}
close(fd);
}
}
once = true;

View file

@ -24,6 +24,7 @@
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/stat.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
@ -37,7 +38,7 @@
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
#include "third_party/zlib/zlib.h"
static int __zipos_inflate_fast(struct ZiposHandle *h, uint8_t *data,
@ -48,7 +49,7 @@ static int __zipos_inflate_fast(struct ZiposHandle *h, uint8_t *data,
zs.next_in = data;
zs.avail_in = size;
zs.total_in = size;
zs.next_out = h->mem;
zs.next_out = h->freeme;
zs.avail_out = h->size;
zs.total_out = h->size;
zs.zfree = Z_NULL;
@ -80,7 +81,7 @@ static int __zipos_inflate_fast(struct ZiposHandle *h, uint8_t *data,
static int __zipos_inflate_tiny(struct ZiposHandle *h, uint8_t *data,
size_t size) {
struct DeflateState ds;
return undeflate(h->mem, h->size, data, size, &ds) != -1 ? 0 : eio();
return undeflate(h->freeme, h->size, data, size, &ds) != -1 ? 0 : eio();
}
static int __zipos_load(struct Zipos *zipos, size_t cf, unsigned flags,
@ -93,39 +94,37 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, unsigned flags,
assert(ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf) == kZipCompressionNone ||
ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf) ==
kZipCompressionDeflate);
if ((fd = createfd()) == -1) return -1;
if (!(h = calloc(1, sizeof(*h)))) return -1;
h->cfile = cf;
if ((h->size = ZIP_LFILE_UNCOMPRESSEDSIZE(zipos->map + lf))) {
if (ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf)) {
assert(ZIP_LFILE_COMPRESSEDSIZE(zipos->map + lf));
h->mapsize = h->size;
if ((h->map = malloc(h->mapsize)) != MAP_FAILED) {
h->mem = h->map;
if ((IsTiny() ? __zipos_inflate_tiny : __zipos_inflate_fast)(
h, ZIP_LFILE_CONTENT(zipos->map + lf),
ZIP_LFILE_COMPRESSEDSIZE(zipos->map + lf)) == -1) {
fd = -1;
}
} else {
fd = -1;
if ((h->freeme = malloc(h->size)) &&
(IsTiny() ? __zipos_inflate_tiny : __zipos_inflate_fast)(
h, ZIP_LFILE_CONTENT(zipos->map + lf),
ZIP_LFILE_COMPRESSEDSIZE(zipos->map + lf)) != -1) {
h->mem = h->freeme;
}
} else {
h->mem = ZIP_LFILE_CONTENT(zipos->map + lf);
}
}
if (!IsTiny() && fd != -1 &&
if (!IsTiny() && h->mem &&
crc32_z(0, h->mem, h->size) != ZIP_LFILE_CRC32(zipos->map + lf)) {
fd = eio();
errno = EIO;
h->mem = NULL;
}
if (fd != -1) {
if (h->mem && (fd = __ensurefds(dup(zipos->fd))) != -1) {
h->handle = g_fds.p[fd].handle;
g_fds.p[fd].kind = kFdZip;
g_fds.p[fd].handle = (intptr_t)h;
g_fds.p[fd].flags = flags;
return fd;
} else {
__zipos_close(h);
free(h->freeme);
free(h);
return -1;
}
return fd;
}
/**

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/str/str.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
const char kZiposSchemePrefix[4] = "zip:";

View file

@ -22,7 +22,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/str/str.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
/**
* Reads data from zip store object.

View file

@ -24,7 +24,7 @@
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
int __zipos_stat_impl(struct Zipos *zipos, size_t cf, struct stat *st) {
if (zipos && st) {

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/sysv/errfuns.h"
#include "libc/zipos/zipos.h"
#include "libc/zipos/zipos.internal.h"
/**
* Reads file metadata from αcτµαlly pδrταblε εxεcµταblε object store.

View file

@ -7,6 +7,7 @@ struct stat;
struct iovec;
struct Zipos {
int fd;
uint8_t *map;
uint8_t *cdir;
};
@ -17,28 +18,28 @@ struct ZiposUri {
};
struct ZiposHandle {
uint8_t *mem; /* deflated file base */
size_t size; /* byte length of file */
uint8_t *mem; /* uncompressed file memory */
size_t size; /* byte length of file memory */
size_t pos; /* read/write byte offset state */
uint32_t cfile; /* central directory entry rva */
uint8_t *map;
size_t mapsize;
int64_t handle;
uint8_t *freeme;
};
extern const char kZiposSchemePrefix[4];
struct Zipos *__zipos_get(void);
ssize_t __zipos_parseuri(const char *, struct ZiposUri *);
int __zipos_close(int) hidden;
struct Zipos *__zipos_get(void) hidden;
ssize_t __zipos_parseuri(const char *, struct ZiposUri *) hidden;
ssize_t __zipos_find(struct Zipos *, const struct ZiposUri *);
int __zipos_close(struct ZiposHandle *);
int __zipos_open(const struct ZiposUri *, unsigned, int);
int __zipos_stat(const struct ZiposUri *, struct stat *);
int __zipos_fstat(const struct ZiposHandle *, struct stat *);
int __zipos_stat_impl(struct Zipos *, size_t, struct stat *);
int __zipos_open(const struct ZiposUri *, unsigned, int) hidden;
int __zipos_stat(const struct ZiposUri *, struct stat *) hidden;
int __zipos_fstat(const struct ZiposHandle *, struct stat *) hidden;
int __zipos_stat_impl(struct Zipos *, size_t, struct stat *) hidden;
ssize_t __zipos_read(struct ZiposHandle *, const struct iovec *, size_t,
ssize_t);
ssize_t) hidden;
ssize_t __zipos_write(struct ZiposHandle *, const struct iovec *, size_t,
ssize_t);
ssize_t) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -35,6 +35,8 @@ LIBC_ZIPOS_A_DIRECTDEPS = \
LIBC_SYSV \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV_CALLS \
LIBC_NT_KERNELBASE \
THIRD_PARTY_ZLIB
LIBC_ZIPOS_A_DEPS := \