2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-06-24 19:31:26 +00:00
|
|
|
#include "libc/assert.h"
|
2022-08-07 00:18:40 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/dirent.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/stat.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/dce.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/errno.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2023-08-16 22:53:06 +00:00
|
|
|
#include "libc/limits.h"
|
2023-08-16 14:54:40 +00:00
|
|
|
#include "libc/macros.internal.h"
|
|
|
|
#include "libc/mem/critbit0.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2023-08-16 14:54:40 +00:00
|
|
|
#include "libc/nt/createfile.h"
|
|
|
|
#include "libc/nt/enum/accessmask.h"
|
|
|
|
#include "libc/nt/enum/creationdisposition.h"
|
2020-09-07 04:39:00 +00:00
|
|
|
#include "libc/nt/enum/fileflagandattributes.h"
|
2023-10-13 17:48:04 +00:00
|
|
|
#include "libc/nt/enum/filesharemode.h"
|
2020-09-07 04:39:00 +00:00
|
|
|
#include "libc/nt/enum/filetype.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/files.h"
|
2023-08-16 14:54:40 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/byhandlefileinformation.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/struct/win32finddata.h"
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/dt.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/sysv/consts/s.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2023-06-10 01:02:06 +00:00
|
|
|
#include "libc/zip.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-01-25 21:08:05 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Directory Streams for Linux+Mac+Windows+FreeBSD+OpenBSD.
|
|
|
|
*
|
|
|
|
* System interfaces for listing the contents of file system directories
|
|
|
|
* are famously incompatible across platforms. Most native projects that
|
|
|
|
* have been around a long time implement wrappers for this. Normally it
|
|
|
|
* will only be for DOS or Windows support. So this is the first time it
|
|
|
|
* has been done for five platforms, having a remarkably tiny footprint.
|
|
|
|
*/
|
2020-09-07 04:39:00 +00:00
|
|
|
|
2022-09-13 18:20:35 +00:00
|
|
|
int sys_getdents(unsigned, void *, unsigned, long *);
|
|
|
|
|
2021-01-25 21:08:05 +00:00
|
|
|
/**
|
|
|
|
* Directory stream object.
|
|
|
|
*/
|
2020-06-15 14:18:57 +00:00
|
|
|
struct dirstream {
|
2023-08-16 01:24:53 +00:00
|
|
|
int fd;
|
2021-06-24 19:31:26 +00:00
|
|
|
bool iszip;
|
2023-08-16 14:54:40 +00:00
|
|
|
long tell;
|
2023-08-16 01:24:53 +00:00
|
|
|
int64_t hand;
|
2022-08-07 00:18:40 +00:00
|
|
|
pthread_mutex_t lock;
|
2020-09-07 04:39:00 +00:00
|
|
|
struct dirent ent;
|
2020-06-15 14:18:57 +00:00
|
|
|
union {
|
2023-08-16 14:54:40 +00:00
|
|
|
struct {
|
|
|
|
struct Zipos *zipos;
|
|
|
|
uint64_t inode;
|
2023-10-03 21:40:03 +00:00
|
|
|
uint64_t offset;
|
|
|
|
uint64_t records;
|
2023-08-16 22:53:06 +00:00
|
|
|
struct ZiposUri prefix;
|
2023-08-16 14:54:40 +00:00
|
|
|
struct critbit0 found;
|
|
|
|
} zip;
|
2020-06-15 14:18:57 +00:00
|
|
|
struct {
|
|
|
|
unsigned buf_pos;
|
|
|
|
unsigned buf_end;
|
2023-08-16 22:53:06 +00:00
|
|
|
uint64_t buf[4096];
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
struct {
|
|
|
|
bool isdone;
|
|
|
|
struct NtWin32FindData windata;
|
2023-08-16 14:54:40 +00:00
|
|
|
char16_t name16[PATH_MAX];
|
|
|
|
uint32_t name16len;
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-25 21:08:05 +00:00
|
|
|
/**
|
|
|
|
* FreeBSD getdents() and XNU getdirentries() ABI.
|
|
|
|
*/
|
2021-02-04 03:35:29 +00:00
|
|
|
struct dirent_bsd {
|
2021-01-25 21:08:05 +00:00
|
|
|
uint32_t d_fileno;
|
|
|
|
uint16_t d_reclen;
|
|
|
|
uint8_t d_type;
|
|
|
|
uint8_t d_namlen;
|
|
|
|
char d_name[256];
|
|
|
|
};
|
|
|
|
|
2021-01-29 09:25:14 +00:00
|
|
|
/**
|
|
|
|
* OpenBSD getdents() ABI.
|
|
|
|
*/
|
2021-02-04 03:35:29 +00:00
|
|
|
struct dirent_openbsd {
|
2021-01-29 09:25:14 +00:00
|
|
|
uint64_t d_fileno;
|
|
|
|
int64_t d_off;
|
|
|
|
uint16_t d_reclen;
|
|
|
|
uint8_t d_type;
|
|
|
|
uint8_t d_namlen;
|
|
|
|
uint8_t __zomg[4];
|
|
|
|
char d_name[256];
|
|
|
|
};
|
|
|
|
|
2021-02-05 17:44:54 +00:00
|
|
|
/**
|
|
|
|
* NetBSD getdents().
|
|
|
|
*/
|
|
|
|
struct dirent_netbsd {
|
|
|
|
uint64_t d_fileno;
|
|
|
|
uint16_t d_reclen;
|
|
|
|
uint16_t d_namlen;
|
|
|
|
uint8_t d_type;
|
|
|
|
char d_name[512];
|
|
|
|
};
|
|
|
|
|
2023-08-16 22:53:06 +00:00
|
|
|
static void lockdir(DIR *dir) {
|
2023-09-12 15:58:57 +00:00
|
|
|
pthread_mutex_lock(&dir->lock);
|
2022-08-07 00:18:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 22:53:06 +00:00
|
|
|
static void unlockdir(DIR *dir) {
|
2023-09-12 15:58:57 +00:00
|
|
|
pthread_mutex_unlock(&dir->lock);
|
2021-02-08 12:04:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 14:54:40 +00:00
|
|
|
static textwindows dontinline int fdopendir_nt(DIR *res, int fd) {
|
|
|
|
if (!__isfdkind(fd, kFdFile)) {
|
|
|
|
return ebadf();
|
|
|
|
}
|
|
|
|
res->name16len = GetFinalPathNameByHandle(
|
|
|
|
g_fds.p[fd].handle, res->name16, ARRAYLEN(res->name16),
|
|
|
|
kNtFileNameNormalized | kNtVolumeNameDos);
|
|
|
|
if (!res->name16len) {
|
|
|
|
return __winerr();
|
|
|
|
}
|
|
|
|
if (res->name16len + 2 + 1 > ARRAYLEN(res->name16)) {
|
|
|
|
return enametoolong();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
if (res->name16len > 1 && res->name16[res->name16len - 1] != u'\\') {
|
|
|
|
res->name16[res->name16len++] = u'\\';
|
|
|
|
}
|
|
|
|
res->name16[res->name16len++] = u'*';
|
|
|
|
res->name16[res->name16len] = u'\0';
|
|
|
|
if ((res->hand = FindFirstFile(res->name16, &res->windata)) == -1) {
|
|
|
|
return __fix_enotdir(-1, res->name16);
|
|
|
|
}
|
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 19:31:26 +00:00
|
|
|
static textwindows uint8_t GetNtDirentType(struct NtWin32FindData *w) {
|
|
|
|
switch (w->dwFileType) {
|
|
|
|
case kNtFileTypeDisk:
|
|
|
|
return DT_BLK;
|
|
|
|
case kNtFileTypeChar:
|
|
|
|
return DT_CHR;
|
|
|
|
case kNtFileTypePipe:
|
|
|
|
return DT_FIFO;
|
|
|
|
default:
|
|
|
|
if (w->dwFileAttributes & kNtFileAttributeDirectory) {
|
|
|
|
return DT_DIR;
|
2021-08-18 21:21:30 +00:00
|
|
|
} else if (w->dwFileAttributes & kNtFileAttributeReparsePoint) {
|
|
|
|
return DT_LNK;
|
2021-06-24 19:31:26 +00:00
|
|
|
} else {
|
|
|
|
return DT_REG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 23:12:18 +00:00
|
|
|
static textwindows dontinline struct dirent *readdir_nt(DIR *dir) {
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
TryAgain:
|
2023-10-11 21:54:42 +00:00
|
|
|
while (!dir->isdone &&
|
|
|
|
(dir->windata.dwFileAttributes & kNtFileAttributeSystem)) {
|
|
|
|
dir->isdone = !FindNextFile(dir->hand, &dir->windata);
|
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
if (dir->isdone) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-08-16 22:53:06 +00:00
|
|
|
// join absolute path that's already normalized
|
2023-08-16 14:54:40 +00:00
|
|
|
uint64_t ino = 0;
|
2023-08-16 22:53:06 +00:00
|
|
|
char16_t jp[PATH_MAX];
|
|
|
|
size_t i = dir->name16len - 1; // foo\* -> foo\ (strip star)
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
bool pretend_this_file_doesnt_exist = false;
|
2023-08-16 22:53:06 +00:00
|
|
|
memcpy(jp, dir->name16, i * sizeof(char16_t));
|
2023-08-16 14:54:40 +00:00
|
|
|
char16_t *p = dir->windata.cFileName;
|
2023-08-16 22:53:06 +00:00
|
|
|
if (p[0] == u'.' && p[1] == u'\0') {
|
|
|
|
// join("foo\\", ".") -> "foo\\"
|
|
|
|
} else if (p[0] == u'.' && p[1] == u'.' && p[2] == u'\0') {
|
|
|
|
if (i == 7 && //
|
|
|
|
jp[0] == '\\' && //
|
|
|
|
jp[1] == '\\' && //
|
|
|
|
jp[2] == '?' && //
|
|
|
|
jp[3] == '\\' && //
|
|
|
|
jp[5] == ':' && //
|
|
|
|
jp[6] == '\\') {
|
|
|
|
// e.g. \\?\C:\ stays the same
|
2023-08-16 14:54:40 +00:00
|
|
|
} else {
|
2023-08-16 22:53:06 +00:00
|
|
|
--i; // foo\bar\ -> foo\ (parent)
|
|
|
|
while (i && jp[i - 1] != '\\') --i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*p) {
|
|
|
|
if (i + 1 < ARRAYLEN(jp)) {
|
|
|
|
jp[i++] = *p++;
|
|
|
|
} else {
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
pretend_this_file_doesnt_exist = true;
|
2023-08-16 22:53:06 +00:00
|
|
|
goto GiveUpOnGettingInode;
|
|
|
|
}
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
jp[i] = u'\0';
|
|
|
|
|
|
|
|
// get inode such that it's consistent with stat()
|
|
|
|
// it's important that we not follow symlinks here
|
2023-10-13 17:48:04 +00:00
|
|
|
int64_t fh =
|
|
|
|
CreateFile(jp, kNtFileReadAttributes,
|
|
|
|
kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0,
|
|
|
|
kNtOpenExisting,
|
|
|
|
kNtFileAttributeNormal | kNtFileFlagBackupSemantics |
|
|
|
|
kNtFileFlagOpenReparsePoint,
|
|
|
|
0);
|
2023-08-16 22:53:06 +00:00
|
|
|
if (fh != kNtInvalidHandleValue) {
|
2023-08-16 14:54:40 +00:00
|
|
|
struct NtByHandleFileInformation wst;
|
|
|
|
if (GetFileInformationByHandle(fh, &wst)) {
|
|
|
|
ino = (uint64_t)wst.nFileIndexHigh << 32 | wst.nFileIndexLow;
|
|
|
|
}
|
|
|
|
CloseHandle(fh);
|
2020-09-07 04:39:00 +00:00
|
|
|
} else {
|
2023-08-16 14:54:40 +00:00
|
|
|
// ignore errors and set inode to zero
|
2023-08-16 22:53:06 +00:00
|
|
|
STRACE("failed to get inode of path join(%#hs, %#hs) -> %#hs %m",
|
|
|
|
dir->name16, dir->windata.cFileName, jp);
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
pretend_this_file_doesnt_exist = true;
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
|
|
|
|
GiveUpOnGettingInode:
|
|
|
|
// create result object
|
|
|
|
bzero(&dir->ent, sizeof(dir->ent));
|
|
|
|
dir->ent.d_ino = ino;
|
|
|
|
dir->ent.d_off = dir->tell++;
|
|
|
|
tprecode16to8(dir->ent.d_name, sizeof(dir->ent.d_name),
|
|
|
|
dir->windata.cFileName);
|
|
|
|
dir->ent.d_type = GetNtDirentType(&dir->windata);
|
|
|
|
dir->isdone = !FindNextFile(dir->hand, &dir->windata);
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
if (pretend_this_file_doesnt_exist) goto TryAgain;
|
2023-08-16 14:54:40 +00:00
|
|
|
return &dir->ent;
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 01:24:53 +00:00
|
|
|
/**
|
|
|
|
* Creates directory object for file descriptor.
|
|
|
|
*
|
|
|
|
* @param fd gets owned by this function, if it succeeds
|
|
|
|
* @return new directory object, which must be freed by closedir(),
|
|
|
|
* or NULL w/ errno
|
|
|
|
* @errors ENOMEM and fd is closed
|
|
|
|
*/
|
|
|
|
DIR *fdopendir(int fd) {
|
|
|
|
|
2023-09-06 10:54:42 +00:00
|
|
|
// sanity check file descriptor
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
enotdir();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (IsLinux() && (__sys_fcntl(fd, F_GETFL) & O_PATH)) {
|
|
|
|
ebadf();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-16 01:24:53 +00:00
|
|
|
// allocate directory iterator object
|
|
|
|
DIR *dir;
|
|
|
|
if (!(dir = calloc(1, sizeof(*dir)))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// on unix, file descriptor isn't required to be tracked
|
|
|
|
dir->fd = fd;
|
|
|
|
if (!__isfdkind(fd, kFdZip)) {
|
|
|
|
if (IsWindows()) {
|
2023-08-16 14:54:40 +00:00
|
|
|
if (!fdopendir_nt(dir, fd)) {
|
|
|
|
return dir;
|
|
|
|
} else {
|
|
|
|
free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
dir->iszip = true;
|
|
|
|
|
|
|
|
// ensure open /zip/... file is a directory
|
|
|
|
struct ZiposHandle *h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle;
|
|
|
|
if (h->cfile != ZIPOS_SYNTHETIC_DIRECTORY &&
|
2023-10-03 21:40:03 +00:00
|
|
|
!S_ISDIR(GetZipCfileMode(h->zipos->map + h->cfile))) {
|
2023-08-16 01:24:53 +00:00
|
|
|
free(dir);
|
|
|
|
enotdir();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get path of this file descriptor and ensure trailing slash
|
|
|
|
size_t len;
|
2023-09-02 03:49:13 +00:00
|
|
|
const char *name;
|
2023-08-16 01:24:53 +00:00
|
|
|
if (h->cfile != ZIPOS_SYNTHETIC_DIRECTORY) {
|
2023-10-03 21:40:03 +00:00
|
|
|
len = ZIP_CFILE_NAMESIZE(h->zipos->map + h->cfile);
|
|
|
|
name = ZIP_CFILE_NAME(h->zipos->map + h->cfile);
|
2023-08-16 01:24:53 +00:00
|
|
|
} else {
|
|
|
|
len = h->size;
|
2023-09-02 03:49:13 +00:00
|
|
|
name = (const char *)h->data;
|
2023-08-16 01:24:53 +00:00
|
|
|
}
|
|
|
|
if (len + 2 > ZIPOS_PATH_MAX) {
|
|
|
|
free(dir);
|
|
|
|
enametoolong();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
if (len) memcpy(dir->zip.prefix.path, name, len);
|
|
|
|
if (len && dir->zip.prefix.path[len - 1] != '/') {
|
|
|
|
dir->zip.prefix.path[len++] = '/';
|
2023-08-16 01:24:53 +00:00
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
dir->zip.prefix.path[len] = 0;
|
|
|
|
dir->zip.prefix.len = len;
|
2023-08-16 01:24:53 +00:00
|
|
|
|
|
|
|
// setup state values for directory iterator
|
2023-08-16 14:54:40 +00:00
|
|
|
dir->zip.zipos = h->zipos;
|
2023-10-03 21:40:03 +00:00
|
|
|
dir->zip.offset = GetZipCdirOffset(h->zipos->cdir);
|
|
|
|
dir->zip.records = GetZipCdirRecords(h->zipos->cdir);
|
2023-08-16 22:53:06 +00:00
|
|
|
dir->zip.inode = __zipos_inode(h->zipos, h->cfile, dir->zip.prefix.path,
|
|
|
|
dir->zip.prefix.len);
|
2023-08-16 01:24:53 +00:00
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
2020-09-07 04:39:00 +00:00
|
|
|
* Opens directory, e.g.
|
|
|
|
*
|
2020-12-26 10:09:07 +00:00
|
|
|
* DIR *d;
|
|
|
|
* struct dirent *e;
|
2023-08-16 01:24:53 +00:00
|
|
|
* d = opendir(path);
|
2020-12-26 10:09:07 +00:00
|
|
|
* while ((e = readdir(d))) {
|
|
|
|
* printf("%s/%s\n", path, e->d_name);
|
|
|
|
* }
|
2023-08-16 01:24:53 +00:00
|
|
|
* closedir(d);
|
2020-06-15 14:18:57 +00:00
|
|
|
*
|
|
|
|
* @returns newly allocated DIR object, or NULL w/ errno
|
|
|
|
* @errors ENOENT, ENOTDIR, EACCES, EMFILE, ENFILE, ENOMEM
|
2022-11-06 02:49:41 +00:00
|
|
|
* @raise ECANCELED if thread was cancelled in masked mode
|
|
|
|
* @raise EINTR if we needed to block and a signal was delivered instead
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
* @cancelationpoint
|
2020-06-21 07:10:11 +00:00
|
|
|
* @see glob()
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
DIR *opendir(const char *name) {
|
2023-08-16 01:24:53 +00:00
|
|
|
int rc;
|
2022-11-04 08:04:43 +00:00
|
|
|
if (_weaken(pthread_testcancel_np) &&
|
|
|
|
(rc = _weaken(pthread_testcancel_np)())) {
|
|
|
|
errno = rc;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
int fd;
|
|
|
|
if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_NOCTTY | O_CLOEXEC)) == -1) {
|
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
DIR *res = fdopendir(fd);
|
|
|
|
if (!res) close(fd);
|
2022-03-19 10:37:00 +00:00
|
|
|
return res;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 14:54:40 +00:00
|
|
|
static struct dirent *readdir_zipos(DIR *dir) {
|
|
|
|
struct dirent *ent = 0;
|
|
|
|
while (!ent && dir->tell < dir->zip.records + 2) {
|
|
|
|
if (!dir->tell) {
|
2021-01-29 09:25:14 +00:00
|
|
|
ent = &dir->ent;
|
2023-08-16 14:54:40 +00:00
|
|
|
ent->d_off = dir->tell;
|
|
|
|
ent->d_ino = dir->zip.inode;
|
|
|
|
ent->d_type = DT_DIR;
|
|
|
|
ent->d_name[0] = '.';
|
|
|
|
ent->d_name[1] = 0;
|
|
|
|
} else if (dir->tell == 1) {
|
2021-02-05 17:44:54 +00:00
|
|
|
ent = &dir->ent;
|
2023-08-16 14:54:40 +00:00
|
|
|
ent->d_off = dir->tell;
|
|
|
|
ent->d_type = DT_DIR;
|
|
|
|
ent->d_name[0] = '.';
|
|
|
|
ent->d_name[1] = '.';
|
|
|
|
ent->d_name[2] = 0;
|
2023-08-16 22:53:06 +00:00
|
|
|
struct ZiposUri p;
|
|
|
|
p.len = dir->zip.prefix.len;
|
|
|
|
if (p.len) memcpy(p.path, dir->zip.prefix.path, p.len);
|
|
|
|
while (p.len && p.path[p.len - 1] == '/') --p.len;
|
|
|
|
while (p.len && p.path[p.len - 1] != '/') --p.len;
|
|
|
|
while (p.len && p.path[p.len - 1] == '/') --p.len;
|
|
|
|
p.path[p.len] = 0;
|
|
|
|
ent->d_ino = __zipos_inode(
|
2023-08-18 14:04:55 +00:00
|
|
|
dir->zip.zipos, __zipos_scan(dir->zip.zipos, &p), p.path, p.len);
|
2020-09-07 04:39:00 +00:00
|
|
|
} else {
|
2023-10-03 21:40:03 +00:00
|
|
|
const char *s = ZIP_CFILE_NAME(dir->zip.zipos->map + dir->zip.offset);
|
|
|
|
size_t n = ZIP_CFILE_NAMESIZE(dir->zip.zipos->map + dir->zip.offset);
|
2023-08-16 22:53:06 +00:00
|
|
|
if (n > dir->zip.prefix.len &&
|
|
|
|
!memcmp(dir->zip.prefix.path, s, dir->zip.prefix.len)) {
|
|
|
|
s += dir->zip.prefix.len;
|
|
|
|
n -= dir->zip.prefix.len;
|
2023-09-02 03:49:13 +00:00
|
|
|
const char *p = memchr(s, '/', n);
|
2023-09-19 05:17:56 +00:00
|
|
|
int d_type;
|
|
|
|
if (p) {
|
|
|
|
n = p - s;
|
|
|
|
d_type = DT_DIR;
|
2023-10-03 21:40:03 +00:00
|
|
|
} else if (S_ISDIR(GetZipCfileMode(dir->zip.zipos->map +
|
2023-09-19 05:17:56 +00:00
|
|
|
dir->zip.offset))) {
|
|
|
|
d_type = DT_DIR;
|
|
|
|
} else {
|
|
|
|
d_type = DT_REG;
|
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
if ((n = MIN(n, sizeof(ent->d_name) - 1)) &&
|
|
|
|
critbit0_emplace(&dir->zip.found, s, n) == 1) {
|
|
|
|
ent = &dir->ent;
|
|
|
|
ent->d_ino = dir->zip.offset;
|
|
|
|
ent->d_off = dir->tell;
|
2023-09-19 05:17:56 +00:00
|
|
|
ent->d_type = d_type;
|
2023-08-16 14:54:40 +00:00
|
|
|
memcpy(ent->d_name, s, n);
|
|
|
|
ent->d_name[n] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dir->zip.offset +=
|
2023-10-03 21:40:03 +00:00
|
|
|
ZIP_CFILE_HDRSIZE(dir->zip.zipos->map + dir->zip.offset);
|
2023-08-16 14:54:40 +00:00
|
|
|
}
|
|
|
|
dir->tell++;
|
|
|
|
}
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dirent *readdir_unix(DIR *dir) {
|
|
|
|
if (dir->buf_pos >= dir->buf_end) {
|
|
|
|
long basep = dir->tell;
|
2023-08-16 22:53:06 +00:00
|
|
|
if (IsNetbsd() || IsFreebsd()) {
|
|
|
|
unsigned long seeky = lseek(dir->fd, 0, SEEK_CUR);
|
|
|
|
unassert(seeky <= INT_MAX);
|
|
|
|
dir->tell = seeky << 32;
|
|
|
|
}
|
|
|
|
int rc = sys_getdents(dir->fd, dir->buf, sizeof(dir->buf), &basep);
|
2023-08-16 14:54:40 +00:00
|
|
|
STRACE("sys_getdents(%d) → %d% m", dir->fd, rc);
|
|
|
|
if (!rc || rc == -1) {
|
|
|
|
return NULL;
|
2020-09-07 04:39:00 +00:00
|
|
|
}
|
2023-08-16 14:54:40 +00:00
|
|
|
dir->buf_pos = 0;
|
|
|
|
dir->buf_end = rc;
|
|
|
|
}
|
|
|
|
struct dirent *ent;
|
|
|
|
if (IsLinux()) {
|
|
|
|
ent = (struct dirent *)((char *)dir->buf + dir->buf_pos);
|
|
|
|
dir->buf_pos += ent->d_reclen;
|
|
|
|
dir->tell = ent->d_off;
|
|
|
|
} else if (IsOpenbsd()) {
|
|
|
|
struct dirent_openbsd *obsd =
|
|
|
|
(struct dirent_openbsd *)((char *)dir->buf + dir->buf_pos);
|
|
|
|
dir->buf_pos += obsd->d_reclen;
|
|
|
|
dir->tell = obsd->d_off;
|
|
|
|
ent = &dir->ent;
|
|
|
|
ent->d_ino = obsd->d_fileno;
|
|
|
|
ent->d_off = obsd->d_off;
|
|
|
|
ent->d_reclen = obsd->d_reclen;
|
|
|
|
ent->d_type = obsd->d_type;
|
|
|
|
memcpy(ent->d_name, obsd->d_name, obsd->d_namlen + 1);
|
|
|
|
} else if (IsNetbsd()) {
|
|
|
|
struct dirent_netbsd *nbsd =
|
|
|
|
(struct dirent_netbsd *)((char *)dir->buf + dir->buf_pos);
|
|
|
|
dir->buf_pos += nbsd->d_reclen;
|
|
|
|
ent = &dir->ent;
|
|
|
|
ent->d_ino = nbsd->d_fileno;
|
|
|
|
ent->d_off = (dir->tell += nbsd->d_reclen);
|
|
|
|
ent->d_reclen = nbsd->d_reclen;
|
|
|
|
ent->d_type = nbsd->d_type;
|
|
|
|
size_t n =
|
|
|
|
MIN(nbsd->d_namlen, MIN(sizeof(ent->d_name) - 1, sizeof(nbsd->d_name)));
|
|
|
|
memcpy(ent->d_name, nbsd->d_name, n);
|
|
|
|
ent->d_name[n] = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2023-08-16 14:54:40 +00:00
|
|
|
struct dirent_bsd *bsd =
|
|
|
|
(struct dirent_bsd *)((char *)dir->buf + dir->buf_pos);
|
|
|
|
dir->buf_pos += bsd->d_reclen;
|
|
|
|
ent = &dir->ent;
|
|
|
|
ent->d_ino = bsd->d_fileno;
|
|
|
|
ent->d_off = dir->tell++;
|
|
|
|
ent->d_reclen = bsd->d_reclen;
|
|
|
|
ent->d_type = bsd->d_type;
|
|
|
|
memcpy(ent->d_name, bsd->d_name, bsd->d_namlen + 1);
|
|
|
|
}
|
|
|
|
if (ent) {
|
|
|
|
ent->d_reclen =
|
|
|
|
ROUNDUP(offsetof(struct dirent, d_name) + strlen(ent->d_name) + 1, 8);
|
|
|
|
}
|
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dirent *readdir_impl(DIR *dir) {
|
|
|
|
if (dir->iszip) {
|
|
|
|
return readdir_zipos(dir);
|
2023-08-16 22:53:06 +00:00
|
|
|
} else if (IsWindows()) {
|
2021-02-04 03:35:29 +00:00
|
|
|
return readdir_nt(dir);
|
2023-08-16 22:53:06 +00:00
|
|
|
} else {
|
|
|
|
return readdir_unix(dir);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 00:18:40 +00:00
|
|
|
/**
|
|
|
|
* Reads next entry from directory stream.
|
|
|
|
*
|
|
|
|
* This API doesn't define any particular ordering.
|
|
|
|
*
|
|
|
|
* @param dir is the object opendir() or fdopendir() returned
|
|
|
|
* @return next entry or NULL on end or error, which can be
|
|
|
|
* differentiated by setting errno to 0 beforehand
|
2023-10-03 02:25:19 +00:00
|
|
|
* @threadunsafe
|
2022-08-07 00:18:40 +00:00
|
|
|
*/
|
|
|
|
struct dirent *readdir(DIR *dir) {
|
|
|
|
struct dirent *e;
|
2023-08-16 01:24:53 +00:00
|
|
|
if (dir) {
|
2023-08-16 22:53:06 +00:00
|
|
|
lockdir(dir);
|
2023-08-16 01:24:53 +00:00
|
|
|
e = readdir_impl(dir);
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2023-08-16 01:24:53 +00:00
|
|
|
} else {
|
|
|
|
efault();
|
|
|
|
e = 0;
|
|
|
|
}
|
2022-08-07 00:18:40 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:52:41 +00:00
|
|
|
/**
|
|
|
|
* Reads directory entry reentrantly.
|
|
|
|
*
|
|
|
|
* @param dir is the object opendir() or fdopendir() returned
|
|
|
|
* @param output is where directory entry is copied if not eof
|
|
|
|
* @param result will receive `output` pointer, or null on eof
|
|
|
|
* @return 0 on success, or errno on error
|
|
|
|
* @returnserrno
|
|
|
|
*/
|
|
|
|
errno_t readdir_r(DIR *dir, struct dirent *output, struct dirent **result) {
|
|
|
|
int err, olderr;
|
|
|
|
struct dirent *entry;
|
2023-08-16 22:53:06 +00:00
|
|
|
lockdir(dir);
|
2022-10-11 00:52:41 +00:00
|
|
|
olderr = errno;
|
|
|
|
errno = 0;
|
|
|
|
entry = readdir_impl(dir);
|
|
|
|
err = errno;
|
|
|
|
errno = olderr;
|
|
|
|
if (err) {
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2022-10-11 00:52:41 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (entry) {
|
2023-07-30 01:44:15 +00:00
|
|
|
if (entry->d_reclen) {
|
|
|
|
memcpy(output, entry, entry->d_reclen);
|
|
|
|
}
|
2022-10-11 00:52:41 +00:00
|
|
|
} else {
|
|
|
|
output = 0;
|
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2022-10-11 00:52:41 +00:00
|
|
|
*result = output;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Closes directory object returned by opendir().
|
|
|
|
* @return 0 on success or -1 w/ errno
|
|
|
|
*/
|
|
|
|
int closedir(DIR *dir) {
|
2023-08-16 01:24:53 +00:00
|
|
|
int rc = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (dir) {
|
2023-08-16 14:54:40 +00:00
|
|
|
if (dir->iszip) {
|
|
|
|
critbit0_clear(&dir->zip.found);
|
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
if (dir->fd != -1) {
|
|
|
|
rc |= close(dir->fd);
|
|
|
|
}
|
|
|
|
if (IsWindows() && !dir->iszip) {
|
|
|
|
if (!FindClose(dir->hand)) {
|
|
|
|
rc = __winerr();
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
free(dir);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-07 04:39:00 +00:00
|
|
|
* Returns offset into directory data.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
long telldir(DIR *dir) {
|
2022-08-07 00:18:40 +00:00
|
|
|
long rc;
|
2023-08-16 22:53:06 +00:00
|
|
|
lockdir(dir);
|
2022-08-07 00:18:40 +00:00
|
|
|
rc = dir->tell;
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2022-08-07 00:18:40 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns file descriptor associated with DIR object.
|
|
|
|
*/
|
|
|
|
int dirfd(DIR *dir) {
|
2023-08-16 14:54:40 +00:00
|
|
|
return dir->fd;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-08-12 07:42:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeks to beginning of directory stream.
|
|
|
|
*/
|
|
|
|
void rewinddir(DIR *dir) {
|
2023-08-16 22:53:06 +00:00
|
|
|
lockdir(dir);
|
2021-08-12 07:42:14 +00:00
|
|
|
if (dir->iszip) {
|
2023-08-16 14:54:40 +00:00
|
|
|
critbit0_clear(&dir->zip.found);
|
2021-08-12 07:42:14 +00:00
|
|
|
dir->tell = 0;
|
2023-10-03 21:40:03 +00:00
|
|
|
dir->zip.offset = GetZipCdirOffset(dir->zip.zipos->cdir);
|
2021-08-12 07:42:14 +00:00
|
|
|
} else if (!IsWindows()) {
|
|
|
|
if (!lseek(dir->fd, 0, SEEK_SET)) {
|
|
|
|
dir->buf_pos = dir->buf_end = 0;
|
|
|
|
dir->tell = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-16 01:24:53 +00:00
|
|
|
FindClose(dir->hand);
|
2023-08-16 14:54:40 +00:00
|
|
|
if ((dir->hand = FindFirstFile(dir->name16, &dir->windata)) != -1) {
|
2021-08-12 07:42:14 +00:00
|
|
|
dir->isdone = false;
|
|
|
|
dir->tell = 0;
|
|
|
|
} else {
|
|
|
|
dir->isdone = true;
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2022-08-07 00:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seeks in directory stream.
|
|
|
|
*/
|
2023-08-16 14:54:40 +00:00
|
|
|
void seekdir(DIR *dir, long tell) {
|
2023-08-16 22:53:06 +00:00
|
|
|
lockdir(dir);
|
2022-08-07 00:18:40 +00:00
|
|
|
if (dir->iszip) {
|
2023-08-16 14:54:40 +00:00
|
|
|
critbit0_clear(&dir->zip.found);
|
|
|
|
dir->tell = 0;
|
2023-10-03 21:40:03 +00:00
|
|
|
dir->zip.offset = GetZipCdirOffset(dir->zip.zipos->cdir);
|
2023-08-16 14:54:40 +00:00
|
|
|
while (dir->tell < tell) {
|
|
|
|
if (!readdir_zipos(dir)) {
|
|
|
|
break;
|
2023-08-16 01:24:53 +00:00
|
|
|
}
|
2022-08-07 00:18:40 +00:00
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
} else if (IsNetbsd() || IsFreebsd()) {
|
|
|
|
dir->buf_pos = dir->buf_end = 0;
|
|
|
|
dir->tell = lseek(dir->fd, tell >> 32, SEEK_SET) << 32;
|
|
|
|
while (dir->tell < tell) {
|
|
|
|
if (!readdir_unix(dir)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
} else if (!IsWindows()) {
|
2023-08-16 14:54:40 +00:00
|
|
|
dir->tell = lseek(dir->fd, tell, SEEK_SET);
|
2022-08-07 00:18:40 +00:00
|
|
|
dir->buf_pos = dir->buf_end = 0;
|
2023-08-16 01:24:53 +00:00
|
|
|
} else {
|
2023-08-16 14:54:40 +00:00
|
|
|
dir->tell = 0;
|
2023-08-16 01:24:53 +00:00
|
|
|
dir->isdone = false;
|
|
|
|
FindClose(dir->hand);
|
2023-08-16 14:54:40 +00:00
|
|
|
if ((dir->hand = FindFirstFile(dir->name16, &dir->windata)) != -1) {
|
|
|
|
for (; dir->tell < tell; ++dir->tell) {
|
2023-08-16 01:24:53 +00:00
|
|
|
if (!FindNextFile(dir->hand, &dir->windata)) {
|
|
|
|
dir->isdone = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dir->isdone = true;
|
|
|
|
}
|
2022-08-07 00:18:40 +00:00
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
unlockdir(dir);
|
2021-08-12 07:42:14 +00:00
|
|
|
}
|
2023-07-24 15:31:54 +00:00
|
|
|
|
|
|
|
__weak_reference(readdir, readdir64);
|
2023-09-21 14:30:39 +00:00
|
|
|
__weak_reference(readdir_r, readdir_r64);
|