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

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
@ -44,10 +45,14 @@ static textwindows bool SubpathExistsThatsNotDirectory(char16_t *path) {
textwindows dontinline int64_t __fix_enotdir3(int64_t rc, char16_t *path1,
char16_t *path2) {
if (rc == -1 && errno == kNtErrorPathNotFound) {
if ((!path1 || !SubpathExistsThatsNotDirectory(path1)) &&
(!path2 || !SubpathExistsThatsNotDirectory(path2))) {
errno = kNtErrorFileNotFound;
if (rc == -1 && (errno == kNtErrorPathNotFound || // Windows returns ENOTDIR
errno == kNtErrorInvalidName)) { // e.g. has trailing slash
bool isdir = false;
if ((!path1 || !(isdir |= SubpathExistsThatsNotDirectory(path1))) &&
(!path2 || !(isdir |= SubpathExistsThatsNotDirectory(path2)))) {
errno = kNtErrorFileNotFound; // ENOENT
} else if (isdir) {
errno = kNtErrorPathNotFound; // ENOTDIR
}
}
return rc;