mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Rewrite ZipOS
This reduces the virtual memory usage of Emacs for me by 30%. We now have a simpler implementation that uses read(), rather mmap()ing the whole executable.
This commit is contained in:
parent
ff77f2a6af
commit
b01282e23e
21 changed files with 408 additions and 421 deletions
|
@ -24,11 +24,11 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/zip.internal.h"
|
||||
|
||||
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);
|
||||
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);
|
||||
if ((len == zsize || (len + 1 == zsize && zname[len] == '/')) &&
|
||||
!memcmp(name->path, zname, len)) {
|
||||
return cfile;
|
||||
|
@ -40,7 +40,7 @@ static ssize_t __zipos_match(struct Zipos *z, struct ZiposUri *name, int len,
|
|||
}
|
||||
}
|
||||
|
||||
ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
|
||||
int __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
|
||||
|
||||
// strip trailing slash from search name
|
||||
int len = name->len;
|
||||
|
@ -55,12 +55,12 @@ ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
|
|||
|
||||
// binary search for leftmost name in central directory
|
||||
int l = 0;
|
||||
int r = zipos->records;
|
||||
int r = zipos->cnt;
|
||||
while (l < r) {
|
||||
int m = (l & r) + ((l ^ r) >> 1); // floor((a+b)/2)
|
||||
const char *xp = ZIP_CFILE_NAME(zipos->map + zipos->index[m]);
|
||||
const char *xp = ZIP_CFILE_NAME(zipos->cdir + zipos->index[m]);
|
||||
const char *yp = name->path;
|
||||
int xn = ZIP_CFILE_NAMESIZE(zipos->map + zipos->index[m]);
|
||||
int xn = ZIP_CFILE_NAMESIZE(zipos->cdir + zipos->index[m]);
|
||||
int yn = len;
|
||||
int n = MIN(xn, yn);
|
||||
int c;
|
||||
|
@ -78,25 +78,25 @@ ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
|
|||
}
|
||||
}
|
||||
|
||||
if (l < zipos->records) {
|
||||
if (l < zipos->cnt) {
|
||||
int dx;
|
||||
size_t cfile = zipos->index[l];
|
||||
const char *zname = ZIP_CFILE_NAME(zipos->map + cfile);
|
||||
int zsize = ZIP_CFILE_NAMESIZE(zipos->map + cfile);
|
||||
int cfile = zipos->index[l];
|
||||
const char *zname = ZIP_CFILE_NAME(zipos->cdir + cfile);
|
||||
int zsize = ZIP_CFILE_NAMESIZE(zipos->cdir + 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->records; l += dx) {
|
||||
ssize_t cf;
|
||||
for (l += dx; 0 <= l && l < zipos->cnt; l += dx) {
|
||||
int cf;
|
||||
if ((cf = __zipos_match(zipos, name, len, l)) != -1) {
|
||||
return cf;
|
||||
}
|
||||
cfile = zipos->index[l];
|
||||
zname = ZIP_CFILE_NAME(zipos->map + cfile);
|
||||
zsize = ZIP_CFILE_NAMESIZE(zipos->map + cfile);
|
||||
zname = ZIP_CFILE_NAME(zipos->cdir + cfile);
|
||||
zsize = ZIP_CFILE_NAMESIZE(zipos->cdir + cfile);
|
||||
if (zsize < len || (len && zname[len - 1] != name->path[len - 1])) {
|
||||
break;
|
||||
}
|
||||
|
@ -112,8 +112,8 @@ ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
|
|||
}
|
||||
|
||||
// support code for open(), stat(), and access()
|
||||
ssize_t __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
|
||||
ssize_t cf;
|
||||
int __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
|
||||
int cf;
|
||||
if ((cf = __zipos_scan(zipos, name)) == -1) {
|
||||
// test if parent component exists that isn't a directory
|
||||
char *p;
|
||||
|
@ -121,7 +121,7 @@ ssize_t __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->map + cf))) {
|
||||
!S_ISDIR(GetZipCfileMode(zipos->cdir + cf))) {
|
||||
return enotdir();
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ ssize_t __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->map + cf))) {
|
||||
!S_ISDIR(GetZipCfileMode(zipos->cdir + cf))) {
|
||||
return enotdir();
|
||||
}
|
||||
return cf;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue