mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
Emulate ENOTDIR better
This commit is contained in:
parent
b76b2be2d0
commit
8d1c81ac9f
35 changed files with 80 additions and 50 deletions
|
@ -27,7 +27,6 @@
|
|||
#include "libc/fmt/magnumstrs.internal.h"
|
||||
#include "libc/intrin/_getenv.internal.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "ape/sections.internal.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
|
|
@ -40,7 +40,7 @@ int __zipos_access(struct ZiposUri *name, int amode) {
|
|||
|
||||
ssize_t cf;
|
||||
if ((cf = __zipos_find(z, name)) == -1) {
|
||||
return enoent();
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mode = GetZipCfileMode(z->map + cf);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/cmpxchg.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/promises.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
|
|
@ -21,8 +21,7 @@
|
|||
// 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
|
||||
// /etc, we'd translate that here to "etc", and "etc/" slash is kept
|
||||
// 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
|
||||
|
@ -49,8 +48,6 @@ size_t __zipos_normpath(char *d, const char *s, size_t n) {
|
|||
}
|
||||
// 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
|
||||
|
|
|
@ -209,7 +209,7 @@ static int __zipos_open_impl(struct ZiposUri *name, int flags) {
|
|||
}
|
||||
ssize_t cf;
|
||||
if ((cf = __zipos_find(zipos, name)) == -1) {
|
||||
return enoent();
|
||||
return -1;
|
||||
}
|
||||
if (flags & O_EXCL) {
|
||||
return eexist();
|
||||
|
|
|
@ -23,14 +23,14 @@
|
|||
/**
|
||||
* Reads file metadata from αcτµαlly pδrταblε εxεcµταblε object store.
|
||||
*
|
||||
* @param uri is obtained via __zipos_parseuri()
|
||||
* @param name is obtained via __zipos_parseuri()
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int __zipos_stat(struct ZiposUri *name, struct stat *st) {
|
||||
ssize_t cf;
|
||||
struct Zipos *zipos;
|
||||
if (!(zipos = __zipos_get())) return enoexec();
|
||||
if ((cf = __zipos_find(zipos, name)) == -1) return enoent();
|
||||
if ((cf = __zipos_find(zipos, name)) == -1) return -1;
|
||||
if (__zipos_stat_impl(zipos, cf, st)) return -1;
|
||||
st->st_ino = __zipos_inode(zipos, cf, name->path, name->len);
|
||||
return 0;
|
||||
|
|
|
@ -44,8 +44,8 @@ void __zipos_unlock(void);
|
|||
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 *);
|
||||
ssize_t __zipos_parseuri(const char *, 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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue