Emulate ENOTDIR better

This commit is contained in:
Justine Tunney 2023-08-16 20:11:19 -07:00
parent b76b2be2d0
commit 8d1c81ac9f
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
35 changed files with 80 additions and 50 deletions

View file

@ -1,7 +1,7 @@
/*-*- 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
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
@ -17,10 +17,17 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/zipos.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/errfuns.h"
#include "libc/zip.internal.h"
ssize_t __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
if (!name->len) {
static ssize_t __zipos_scan(struct Zipos *zipos, struct ZiposUri *name) {
size_t len = name->len;
if (len && name->path[len - 1] == '/') {
--len;
}
if (!len) {
return ZIPOS_SYNTHETIC_DIRECTORY;
}
bool found_subfile = false;
@ -29,12 +36,11 @@ ssize_t __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
for (size_t i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(zipos->map + c)) {
const char *zname = ZIP_CFILE_NAME(zipos->map + c);
size_t zsize = ZIP_CFILE_NAMESIZE(zipos->map + c);
if ((name->len == zsize ||
(name->len + 1 == zsize && zname[name->len] == '/')) &&
!memcmp(name->path, zname, name->len)) {
if ((len == zsize || (len + 1 == zsize && zname[len] == '/')) &&
!memcmp(name->path, zname, len)) {
return c;
} else if (name->len + 1 < zsize && zname[name->len] == '/' &&
!memcmp(name->path, zname, name->len)) {
} else if (len + 1 < zsize && zname[len] == '/' &&
!memcmp(name->path, zname, len)) {
found_subfile = true;
}
}
@ -43,3 +49,28 @@ ssize_t __zipos_find(struct Zipos *zipos, struct ZiposUri *name) {
}
return -1;
}
// support code for open(), stat(), and access()
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;
while ((p = memrchr(name->path, '/', name->len))) {
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))) {
return enotdir();
}
}
return enoent();
}
// 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))) {
return enotdir();
}
return cf;
}