Revert "Rewrite ZipOS"

This reverts commit b01282e23e. Some tests
are broken. It's not clear how it'll impact metal yet. Let's revisit the
memory optimization benefits of this change again sometime soon.
This commit is contained in:
Justine Tunney 2023-10-03 14:40:03 -07:00
parent ee8a861635
commit ff250a0c10
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
21 changed files with 420 additions and 407 deletions

View file

@ -24,11 +24,11 @@
#include "libc/sysv/errfuns.h"
#include "libc/zip.internal.h"
static int __zipos_match(struct Zipos *z, struct ZiposUri *name, int len,
int i) {
int cfile = z->index[i];
const char *zname = ZIP_CFILE_NAME(z->cdir + cfile);
int zsize = ZIP_CFILE_NAMESIZE(z->cdir + cfile);
static ssize_t __zipos_match(struct Zipos *z, struct ZiposUri *name, int len,
int i) {
size_t cfile = z->index[i];
const char *zname = ZIP_CFILE_NAME(z->map + cfile);
int zsize = ZIP_CFILE_NAMESIZE(z->map + cfile);
if ((len == zsize || (len + 1 == zsize && zname[len] == '/')) &&
!memcmp(name->path, zname, len)) {
return cfile;
@ -40,7 +40,7 @@ static int __zipos_match(struct Zipos *z, struct ZiposUri *name, int len,
}
}
int __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
// strip trailing slash from search name
int len = name->len;
@ -55,12 +55,12 @@ int __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
// binary search for leftmost name in central directory
int l = 0;
int r = zipos->cnt;
int r = zipos->records;
while (l < r) {
int m = (l & r) + ((l ^ r) >> 1); // floor((a+b)/2)
const char *xp = ZIP_CFILE_NAME(zipos->cdir + zipos->index[m]);
const char *xp = ZIP_CFILE_NAME(zipos->map + zipos->index[m]);
const char *yp = name->path;
int xn = ZIP_CFILE_NAMESIZE(zipos->cdir + zipos->index[m]);
int xn = ZIP_CFILE_NAMESIZE(zipos->map + zipos->index[m]);
int yn = len;
int n = MIN(xn, yn);
int c;
@ -78,25 +78,25 @@ int __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
}
}
if (l < zipos->cnt) {
if (l < zipos->records) {
int dx;
int cfile = zipos->index[l];
const char *zname = ZIP_CFILE_NAME(zipos->cdir + cfile);
int zsize = ZIP_CFILE_NAMESIZE(zipos->cdir + cfile);
size_t cfile = zipos->index[l];
const char *zname = ZIP_CFILE_NAME(zipos->map + cfile);
int zsize = ZIP_CFILE_NAMESIZE(zipos->map + cfile);
if (zsize > len && (dx = '/' - (zname[len] & 255))) {
// since the index is asciibetical, we need to specially handle
// the case where, when searching for a directory, regular files
// exist whose names share the same prefix as the directory name.
dx = dx > +1 ? +1 : dx;
dx = dx < -1 ? -1 : dx;
for (l += dx; 0 <= l && l < zipos->cnt; l += dx) {
int cf;
for (l += dx; 0 <= l && l < zipos->records; l += dx) {
ssize_t cf;
if ((cf = __zipos_match(zipos, name, len, l)) != -1) {
return cf;
}
cfile = zipos->index[l];
zname = ZIP_CFILE_NAME(zipos->cdir + cfile);
zsize = ZIP_CFILE_NAMESIZE(zipos->cdir + cfile);
zname = ZIP_CFILE_NAME(zipos->map + cfile);
zsize = ZIP_CFILE_NAMESIZE(zipos->map + cfile);
if (zsize < len || (len && zname[len - 1] != name->path[len - 1])) {
break;
}
@ -112,8 +112,8 @@ int __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
}
// support code for open(), stat(), and access()
int __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
int cf;
ssize_t __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
ssize_t cf;
if ((cf = __zipos_scan(zipos, name)) == -1) {
// test if parent component exists that isn't a directory
char *p;
@ -121,7 +121,7 @@ int __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
name->path[name->len = p - name->path] = 0;
if ((cf = __zipos_scan(zipos, name)) != -1 &&
cf != ZIPOS_SYNTHETIC_DIRECTORY &&
!S_ISDIR(GetZipCfileMode(zipos->cdir + cf))) {
!S_ISDIR(GetZipCfileMode(zipos->map + cf))) {
return enotdir();
}
}
@ -130,7 +130,7 @@ int __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
// test if we're opening "foo/" and "foo" isn't a directory
if (cf != ZIPOS_SYNTHETIC_DIRECTORY && //
name->len && name->path[name->len - 1] == '/' &&
!S_ISDIR(GetZipCfileMode(zipos->cdir + cf))) {
!S_ISDIR(GetZipCfileMode(zipos->map + cf))) {
return enotdir();
}
return cf;