mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Improve zip read-only filesystem
readdir() will now always yield an inode that's consistent with stat() on ZipOS and Windows in general. More APIs have been updated to return the appropriate error code when inappropriately trying to do ops, like sockets, with a zip file descriptor. The path normalization algorithms are now fully fleshed out. Some socket APIs have been fixed so they'll raise EBADF vs. ENOTSOCK appropriately. Lastly seekdir() will now work properly on NetBSD and FreeBSD (not sure why anyone would even use it)
This commit is contained in:
parent
dc6c67256f
commit
b76b2be2d0
47 changed files with 644 additions and 269 deletions
|
@ -104,7 +104,6 @@ __msabi static inline char16_t *MyCommandLine(void) {
|
|||
|
||||
// this ensures close(1) won't accidentally close(2) for example
|
||||
__msabi static textwindows void DeduplicateStdioHandles(void) {
|
||||
int64_t h1, h2, h3, proc;
|
||||
for (long i = 0; i < 3; ++i) {
|
||||
int64_t h1 = __imp_GetStdHandle(kNtConsoleHandles[i]);
|
||||
for (long j = i + 1; j < 3; ++j) {
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/runtime/zipos.internal.h"
|
||||
|
||||
/**
|
||||
|
@ -25,5 +26,7 @@
|
|||
* @asyncsignalsafe
|
||||
*/
|
||||
int __zipos_fstat(struct ZiposHandle *h, struct stat *st) {
|
||||
return __zipos_stat_impl(h->zipos, h->cfile, st);
|
||||
if (__zipos_stat_impl(h->zipos, h->cfile, st)) return -1;
|
||||
st->st_ino = __zipos_inode(h->zipos, h->cfile, h->data, h->size);
|
||||
return 0;
|
||||
}
|
||||
|
|
42
libc/runtime/zipos-inode.c
Normal file
42
libc/runtime/zipos-inode.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*-*- 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 2023 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/assert.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/runtime/zipos.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/zip.internal.h"
|
||||
|
||||
static uint64_t __zipos_fnv(const char *s, int len) {
|
||||
uint64_t hash = 0xcbf29ce484222325;
|
||||
for (int i = 0; i < len; i++) {
|
||||
hash *= 0x100000001b3;
|
||||
hash ^= (unsigned char)s[i];
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint64_t __zipos_inode(struct Zipos *zipos, int64_t cfile, //
|
||||
const void *name, size_t namelen) {
|
||||
unassert(cfile >= 0);
|
||||
if (cfile == ZIPOS_SYNTHETIC_DIRECTORY) {
|
||||
if (namelen && ((char *)name)[namelen - 1] == '/') --namelen;
|
||||
cfile = INT64_MIN | __zipos_fnv(name, namelen);
|
||||
}
|
||||
return cfile;
|
||||
}
|
|
@ -18,63 +18,45 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/runtime/zipos.internal.h"
|
||||
|
||||
static size_t __zipos_trimpath(char *s, int *isabs) {
|
||||
char *p = s, *q = s;
|
||||
for (; *q; ++q) {
|
||||
if (*q == '/') {
|
||||
while (q[1] == '/') ++q;
|
||||
if (q[1] == '.' && (q[2] == '/' || q[2] == '\0')) {
|
||||
++q;
|
||||
} else {
|
||||
*p++ = '/';
|
||||
}
|
||||
// normalizes zip filesystem path w/ overlapping strlcpy() style api
|
||||
// zip paths look like relative paths, but they're actually absolute
|
||||
// with respect to the archive; so similar to how /../etc would mean
|
||||
// /etc, we'd translate that here to "etc". when storing assets in a
|
||||
// zip archive, callers should append trailing slash for directories
|
||||
// returns strlen of 𝑑; returns 𝑛 when insufficient buffer available
|
||||
// nul terminator is guaranteed if n>0. it's fine if 𝑑 and 𝑠 overlap
|
||||
// test vectors for this algorithm in: test/libc/stdio/zipdir_test.c
|
||||
size_t __zipos_normpath(char *d, const char *s, size_t n) {
|
||||
char *p, *e;
|
||||
for (p = d, e = d + n; p < e && *s; ++s) {
|
||||
if ((p == d || p[-1] == '/') && *s == '/') {
|
||||
// matched "^/" or "//"
|
||||
} else if ((p == d || p[-1] == '/') && //
|
||||
s[0] == '.' && //
|
||||
(!s[1] || s[1] == '/')) {
|
||||
// matched "/./" or "^.$" or "^./" or "/.$"
|
||||
s += !!s[1];
|
||||
} else if ((p == d || p[-1] == '/') && //
|
||||
s[0] == '.' && //
|
||||
s[1] == '.' && //
|
||||
(!s[2] || s[2] == '/')) {
|
||||
// matched "/../" or "^..$" or "^../" or "/..$"
|
||||
while (p > d && p[-1] == '/') --p;
|
||||
while (p > d && p[-1] != '/') --p;
|
||||
} else {
|
||||
*p++ = *q;
|
||||
*p++ = *s;
|
||||
}
|
||||
}
|
||||
if (s < p && p[-1] == '.' && p[-2] == '.' && (p - 2 == s || p[-3] == '/')) {
|
||||
*p++ = '/';
|
||||
// if we didn't overflow
|
||||
if (p < e) {
|
||||
// trim trailing slashes and add nul terminator
|
||||
while (p > d && p[-1] == '/') --p;
|
||||
*p = '\0';
|
||||
} else {
|
||||
// force nul-terminator to exist if possible
|
||||
if (p > d) {
|
||||
p[-1] = '\0';
|
||||
}
|
||||
}
|
||||
*p = '\0';
|
||||
if (isabs) {
|
||||
*isabs = *s == '/';
|
||||
}
|
||||
return p - s;
|
||||
}
|
||||
|
||||
size_t __zipos_normpath(char *s) {
|
||||
int isabs;
|
||||
char *p = s, *q = s;
|
||||
__zipos_trimpath(s, &isabs);
|
||||
if (!*s) return 0;
|
||||
for (; *q != '\0'; ++q) {
|
||||
if (q[0] == '/' && q[1] == '.' && q[2] == '.' &&
|
||||
(q[3] == '/' || q[3] == '\0')) {
|
||||
char *ep = p;
|
||||
while (s < ep && *--ep != '/') donothing;
|
||||
if (ep != p &&
|
||||
(p[-1] != '.' || p[-2] != '.' || (s < p - 3 && p[-3] != '/'))) {
|
||||
p = ep;
|
||||
q += 2;
|
||||
continue;
|
||||
} else if (ep == s && isabs) {
|
||||
q += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (q[0] != '/' || p != s || isabs) {
|
||||
*p++ = *q;
|
||||
}
|
||||
}
|
||||
if (p == s) {
|
||||
*p++ = isabs ? '/' : '.';
|
||||
}
|
||||
if (p == s + 1 && s[0] == '.') {
|
||||
*p++ = '/';
|
||||
}
|
||||
while (p - s > 1 && p[-1] == '/') {
|
||||
--p;
|
||||
}
|
||||
*p = '\0';
|
||||
return p - s;
|
||||
return p - d;
|
||||
}
|
||||
|
|
|
@ -199,6 +199,11 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, int flags,
|
|||
|
||||
static int __zipos_open_impl(struct ZiposUri *name, int flags) {
|
||||
struct Zipos *zipos;
|
||||
if ((flags & O_CREAT) || //
|
||||
(flags & O_TRUNC) || //
|
||||
(flags & O_ACCMODE) != O_RDONLY) {
|
||||
return erofs();
|
||||
}
|
||||
if (!(zipos = __zipos_get())) {
|
||||
return enoexec();
|
||||
}
|
||||
|
@ -206,19 +211,14 @@ static int __zipos_open_impl(struct ZiposUri *name, int flags) {
|
|||
if ((cf = __zipos_find(zipos, name)) == -1) {
|
||||
return enoent();
|
||||
}
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (flags & O_TRUNC)) {
|
||||
return erofs();
|
||||
}
|
||||
if (flags & O_EXCL) {
|
||||
return eexist();
|
||||
}
|
||||
if (cf != ZIPOS_SYNTHETIC_DIRECTORY) {
|
||||
int mode = GetZipCfileMode(zipos->map + cf);
|
||||
#if 0
|
||||
if ((flags & O_DIRECTORY) && !S_ISDIR(mode)) {
|
||||
return enotdir();
|
||||
}
|
||||
#endif
|
||||
if (!(mode & 0444)) {
|
||||
return eacces();
|
||||
}
|
||||
|
|
|
@ -29,8 +29,9 @@ ssize_t __zipos_parseuri(const char *uri, struct ZiposUri *out) {
|
|||
uri[2] == 'i' && //
|
||||
uri[3] == 'p' && //
|
||||
(!uri[4] || uri[4] == '/')) &&
|
||||
strlcpy(out->path, uri + 4 + !!uri[4], ZIPOS_PATH_MAX) < ZIPOS_PATH_MAX) {
|
||||
return (out->len = __zipos_normpath(out->path));
|
||||
(len = __zipos_normpath(out->path, uri + 4 + !!uri[4],
|
||||
sizeof(out->path))) < sizeof(out->path)) {
|
||||
return (out->len = len);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
int __zipos_stat_impl(struct Zipos *zipos, size_t cf, struct stat *st) {
|
||||
size_t lf;
|
||||
bzero(st, sizeof(*st));
|
||||
st->st_ino = cf;
|
||||
st->st_nlink = 1;
|
||||
st->st_dev = zipos->dev;
|
||||
st->st_blksize = FRAMESIZE;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/runtime/zipos.internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
|
@ -30,5 +31,7 @@ int __zipos_stat(struct ZiposUri *name, struct stat *st) {
|
|||
struct Zipos *zipos;
|
||||
if (!(zipos = __zipos_get())) return enoexec();
|
||||
if ((cf = __zipos_find(zipos, name)) == -1) return enoent();
|
||||
return __zipos_stat_impl(zipos, cf, st);
|
||||
if (__zipos_stat_impl(zipos, cf, st)) return -1;
|
||||
st->st_ino = __zipos_inode(zipos, cf, name->path, name->len);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -41,11 +41,12 @@ struct Zipos {
|
|||
int __zipos_close(int);
|
||||
void __zipos_lock(void);
|
||||
void __zipos_unlock(void);
|
||||
size_t __zipos_normpath(char *);
|
||||
struct Zipos *__zipos_get(void) pureconst;
|
||||
void __zipos_free(struct ZiposHandle *);
|
||||
struct Zipos *__zipos_get(void) pureconst;
|
||||
size_t __zipos_normpath(char *, const char *, size_t);
|
||||
ssize_t __zipos_parseuri(const char *, struct ZiposUri *);
|
||||
ssize_t __zipos_find(struct Zipos *, struct ZiposUri *);
|
||||
uint64_t __zipos_inode(struct Zipos *, int64_t, const void *, size_t);
|
||||
int __zipos_open(struct ZiposUri *, int);
|
||||
int __zipos_access(struct ZiposUri *, int);
|
||||
int __zipos_stat(struct ZiposUri *, struct stat *);
|
||||
|
@ -53,8 +54,6 @@ int __zipos_fstat(struct ZiposHandle *, struct stat *);
|
|||
int __zipos_stat_impl(struct Zipos *, size_t, struct stat *);
|
||||
ssize_t __zipos_read(struct ZiposHandle *, const struct iovec *, size_t,
|
||||
ssize_t);
|
||||
ssize_t __zipos_write(struct ZiposHandle *, const struct iovec *, size_t,
|
||||
ssize_t);
|
||||
int64_t __zipos_lseek(struct ZiposHandle *, int64_t, unsigned);
|
||||
int __zipos_fcntl(int, int, uintptr_t);
|
||||
int __zipos_notat(int, const char *);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue