Fix issues for latest GCC toolchain

This commit is contained in:
Justine Tunney 2023-10-11 14:54:42 -07:00
parent 5cb9b2658c
commit 3b086af91b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
22 changed files with 148 additions and 166 deletions

View file

@ -24,20 +24,13 @@
#include "libc/str/str.h"
static textwindows bool SubpathExistsThatsNotDirectory(char16_t *path) {
int e;
char16_t *p;
uint32_t attrs;
e = errno;
while ((p = strrchr16(path, '\\'))) {
*p = u'\0';
if ((attrs = GetFileAttributes(path)) != -1u) {
if (attrs & kNtFileAttributeDirectory) {
return false;
} else {
return true;
}
} else {
errno = e;
if ((attrs = GetFileAttributes(path)) != -1u &&
!(attrs & kNtFileAttributeDirectory)) {
return true;
}
}
return false;

View file

@ -21,31 +21,40 @@
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/stat.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/nt/errors.h"
#include "libc/nt/runtime.h"
#include "libc/sysv/consts/at.h"
textwindows int sys_fstatat_nt(int dirfd, const char *path, struct stat *st,
int flags) {
int rc;
int rc, e;
int64_t fh;
uint32_t dwDesiredAccess;
uint16_t path16[PATH_MAX];
if (__mkntpathat(dirfd, path, 0, path16) == -1) return -1;
BLOCK_SIGNALS;
e = errno;
dwDesiredAccess = kNtFileGenericRead;
TryAgain:
if ((fh = CreateFile(
path16, kNtFileGenericRead,
kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0,
kNtOpenExisting,
path16, dwDesiredAccess, 0, 0, kNtOpenExisting,
kNtFileAttributeNormal | kNtFileFlagBackupSemantics |
((flags & AT_SYMLINK_NOFOLLOW) ? kNtFileFlagOpenReparsePoint
: 0),
0)) != -1) {
rc = st ? sys_fstat_nt(fh, st) : 0;
CloseHandle(fh);
} else if (dwDesiredAccess == kNtFileGenericRead &&
GetLastError() == kNtErrorSharingViolation) {
dwDesiredAccess = kNtFileReadAttributes;
errno = e;
goto TryAgain;
} else {
rc = __winerr();
}

View file

@ -26,15 +26,12 @@
* Returns true if file exists and is a directory on Windows NT.
*/
bool isdirectory_nt(const char *path) {
int e;
uint32_t x;
char16_t path16[PATH_MAX];
e = errno;
if (__mkntpath(path, path16) == -1) return -1;
if ((x = GetFileAttributes(path16)) != -1u) {
return !!(x & kNtFileAttributeDirectory);
} else {
errno = e;
return false;
}
}

View file

@ -26,15 +26,12 @@
* Returns true if file exists and is a regular file on Windows NT.
*/
bool isregularfile_nt(const char *path) {
int e;
uint32_t x;
char16_t path16[PATH_MAX];
e = errno;
if (__mkntpath(path, path16) == -1) return -1;
if ((x = GetFileAttributes(path16)) != -1u) {
return !(x & (kNtFileAttributeDirectory | kNtFileAttributeReparsePoint));
} else {
errno = e;
return false;
}
}

View file

@ -16,9 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/errno.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/files.h"
@ -26,15 +24,12 @@
* Returns true if file exists and is a symbolic link on Windows NT.
*/
bool issymlink_nt(const char *path) {
int e;
uint32_t x;
char16_t path16[PATH_MAX];
e = errno;
if (__mkntpath(path, path16) == -1) return -1;
if ((x = GetFileAttributes(path16)) != -1u) {
return !!(x & kNtFileAttributeReparsePoint);
} else {
errno = e;
return false;
}
}

View file

@ -27,8 +27,6 @@
#include "libc/sysv/consts/at.h"
#include "libc/sysv/errfuns.h"
__msabi extern typeof(GetFileAttributes) *const __imp_GetFileAttributesW;
static int __mkntpathat_impl(int dirfd, const char *path, int flags,
char16_t file[hasatleast PATH_MAX]) {
char16_t dir[PATH_MAX];
@ -71,7 +69,7 @@ int __mkntpathat(int dirfd, const char *path, int flags,
if (len > 1 && !(len == 3 && file[1] == ':')) {
file[--len] = 0;
}
if ((fattr = __imp_GetFileAttributesW(file)) != -1u &&
if ((fattr = GetFileAttributes(file)) != -1u &&
!(fattr & kNtFileAttributeReparsePoint) &&
!(fattr & kNtFileAttributeDirectory)) {
return enotdir();

View file

@ -57,7 +57,7 @@ static textwindows int64_t sys_open_nt_impl(int dirfd, const char *path,
// you can't open symlinks; use readlink
// this flag only applies to the final path component
// if O_NOFOLLOW_ANY is passed (-1 on NT) it'll be rejected later
uint32_t fattr = __imp_GetFileAttributesW(path16);
uint32_t fattr = GetFileAttributes(path16);
if (flags & O_NOFOLLOW) {
if (fattr != -1u && (fattr & kNtFileAttributeReparsePoint)) {
return eloop();

View file

@ -26,7 +26,6 @@
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#include "libc/vga/vga.internal.h"
#ifdef __x86_64__
ssize_t sys_readv_metal(int fd, const struct iovec *iov, int iovlen) {

View file

@ -22,7 +22,6 @@
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
textwindows ssize_t sys_readv_nt(int fd, const struct iovec *iov, int iovlen) {

View file

@ -50,4 +50,4 @@ ssize_t sys_readv_serial(int fd, const struct iovec *iov, int iovlen) {
}
}
#endif
#endif /* __x86_64__ */

View file

@ -28,9 +28,6 @@
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
__msabi extern typeof(GetFileAttributes) *const __imp_GetFileAttributesW;
__msabi extern typeof(RemoveDirectory) *const __imp_RemoveDirectoryW;
static textwindows bool StripTrailingSlash(char16_t *path) {
size_t n = strlen16(path);
bool had_trailing_slash = false;
@ -67,8 +64,8 @@ textwindows int sys_renameat_nt(int olddirfd, const char *oldpath, int newdirfd,
// test for some known error conditions ahead of time
// the enotdir check can't be done reactively
// ideally we should resolve symlinks first
uint32_t oldattr = __imp_GetFileAttributesW(M.oldpath16);
uint32_t newattr = __imp_GetFileAttributesW(M.newpath16);
uint32_t oldattr = GetFileAttributes(M.oldpath16);
uint32_t newattr = GetFileAttributes(M.newpath16);
if ((old_must_be_dir && oldattr != -1u &&
!(oldattr & kNtFileAttributeDirectory)) ||
(new_must_be_dir && newattr != -1u &&
@ -85,7 +82,7 @@ textwindows int sys_renameat_nt(int olddirfd, const char *oldpath, int newdirfd,
} else if ((oldattr & kNtFileAttributeDirectory) &&
(newattr & kNtFileAttributeDirectory)) {
// both old and new are directories
if (!__imp_RemoveDirectoryW(M.newpath16) &&
if (!RemoveDirectory(M.newpath16) &&
GetLastError() == kNtErrorDirNotEmpty) {
return enotempty();
}

View file

@ -34,8 +34,6 @@
#include "libc/runtime/stack.h"
#include "libc/sysv/errfuns.h"
__msabi extern typeof(GetFileAttributes) *const __imp_GetFileAttributesW;
static struct {
_Atomic(uint32_t) once;
_Bool allowed;
@ -72,7 +70,7 @@ textwindows int sys_symlinkat_nt(const char *target, int newdirfd,
if ((targetlen = __mkntpath(target, M.target16)) == -1) return -1;
// determine if we need directory flag
if ((attrs = __imp_GetFileAttributesW(M.target16)) != -1u) {
if ((attrs = GetFileAttributes(M.target16)) != -1u) {
if (attrs & kNtFileAttributeDirectory) {
flags = kNtSymbolicLinkFlagDirectory;
} else {

View file

@ -55,8 +55,7 @@ static textwindows bool IsDirectorySymlink(const char16_t *path) {
}
static textwindows int sys_rmdir_nt(const char16_t *path) {
int e, ms;
e = errno;
int ms;
for (ms = 1;; ms *= 2) {
if (RemoveDirectory(path)) {
return 0;
@ -67,14 +66,13 @@ static textwindows int sys_rmdir_nt(const char16_t *path) {
// Alternative is use Microsoft internal APIs.
// Never could have imagined it'd be this bad.
if (GetLastError() == kNtErrorDirNotEmpty && ms <= 2048) {
errno = e;
Sleep(ms);
continue;
} else {
break;
}
}
return -1;
return __winerr();
}
static textwindows int sys_unlink_nt(const char16_t *path) {