Support dirfd relative iops on Windows

We always favor calling functions like openat(), fstatat(), etc. because
Linux, XNU, FreeBSD, and OpenBSD all elected to support them, while some
systems like Android love them so much, that they stopped supporting the
old interfaces.

This change ensures that when dirfd is actually a dirfd and not AT_FDCWD
we'll do the right thing on Windows NT. We use an API that's been around
since Vista to accomplish that.

This change also adds exponential backoff to chdir() on Windows since it
seems almost as flaky on Windows 7 as the rmdir() function.
This commit is contained in:
Justine Tunney 2021-01-30 01:49:43 -08:00
parent b8d26e2418
commit 417797d218
42 changed files with 361 additions and 241 deletions

View file

@ -16,33 +16,31 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/errno.h"
#include "libc/sysv/consts/at.h"
#include "libc/zipos/zipos.internal.h"
/**
* Performs stat() w/ features for threaded apps.
* Returns information about thing.
*
* @param dirfd can be AT_FDCWD or an open directory descriptor, and is
* ignored if pathname is absolute
* @param dirfd is normally AT_FDCWD but if it's an open directory and
* file is a relative path, then file becomes relative to dirfd
* @param st is where result is stored
* @param flags can have AT_{EMPTY_PATH,NO_AUTOMOUNT,SYMLINK_NOFOLLOW}
* @return 0 on success, or -1 w/ errno
* @see S_ISDIR(st.st_mode), S_ISREG()
* @asyncsignalsafe
*/
int fstatat(int dirfd, const char *pathname, struct stat *st, uint32_t flags) {
int olderr = errno;
int rc = fstatat$sysv(dirfd, pathname, st, flags);
if (rc != -1) {
stat2linux(st);
} else if (errno == ENOSYS && dirfd == AT_FDCWD) {
if (!flags) {
errno = olderr;
rc = stat(pathname, st);
} else if (flags == AT_SYMLINK_NOFOLLOW) {
errno = olderr;
rc = lstat(pathname, st);
}
int fstatat(int dirfd, const char *path, struct stat *st, uint32_t flags) {
struct ZiposUri zipname;
if (weaken(__zipos_stat) && weaken(__zipos_parseuri)(path, &zipname) != -1) {
return weaken(__zipos_stat)(&zipname, st);
} else if (!IsWindows()) {
return fstatat$sysv(dirfd, path, st, flags);
} else {
return fstatat$nt(dirfd, path, st, flags);
}
return rc;
}