Skip to content

Commit

Permalink
Fix remove() directory on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jul 29, 2024
1 parent e18fe1e commit 18964e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions libc/calls/unlinkat.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ int unlinkat(int dirfd, const char *path, int flags) {

// POSIX.1 says unlink(directory) raises EPERM but on Linux
// it always raises EISDIR, which is so much less ambiguous
if (!IsLinux() && rc == -1 && !flags && errno == EPERM) {
int e = errno;
if (!IsLinux() && rc == -1 && !flags && (e == EPERM || e == EACCES)) {
struct stat st;
if (!fstatat(dirfd, path, &st, 0) && S_ISDIR(st.st_mode)) {
errno = EISDIR;
} else {
errno = EPERM;
errno = e;
}
}

Expand Down
14 changes: 10 additions & 4 deletions libc/intrin/ubsan.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/intrin/ubsan.h"
#include "libc/calls/calls.h"
#include "libc/intrin/describebacktrace.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/pushpop.h"
#include "libc/intrin/strace.h"
Expand All @@ -31,6 +32,7 @@
#include "libc/nt/runtime.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
Expand Down Expand Up @@ -240,16 +242,20 @@ __wur static __ubsan_die_f *__ubsan_die(void) {

static void __ubsan_warning(const struct UbsanSourceLocation *loc,
const char *description) {
kprintf("%s:%d: %subsan warning: %s is undefined behavior%s\n", loc->file,
loc->line, SUBTLE, description, RESET);
kprintf("%s:%d: %subsan warning: %s is undefined behavior%s\n"
"cosmoaddr2line %s %s\n",
loc->file, loc->line, SUBTLE, description, RESET, __argv[0],
DescribeBacktrace(__builtin_frame_address(0)));
if (__ubsan_strict)
__ubsan_die()();
}

__wur __ubsan_die_f *__ubsan_abort(const struct UbsanSourceLocation *loc,
const char *description) {
kprintf("\n%s:%d: %subsan error%s: %s (tid %d)\n", loc->file, loc->line, RED2,
RESET, description, gettid());
kprintf("\n%s:%d: %subsan error%s: %s (tid %d)\n"
"cosmoaddr2line %s %s\n",
loc->file, loc->line, RED2, RESET, description, gettid(), __argv[0],
DescribeBacktrace(__builtin_frame_address(0)));
return __ubsan_die();
}

Expand Down
5 changes: 5 additions & 0 deletions test/libc/calls/mkdir_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ TEST(mkdir, testPathIsFile_EEXIST) {
EXPECT_SYS(EEXIST, -1, mkdir("yo/yo/yo", 0755));
}

TEST(mkdir, remove) {
EXPECT_SYS(0, 0, mkdir("yo", 0777));
EXPECT_SYS(0, 0, remove("yo"));
}

TEST(mkdir, testPathIsDirectory_EEXIST) {
EXPECT_SYS(0, 0, mkdir("yo", 0755));
EXPECT_SYS(0, 0, mkdir("yo/yo", 0755));
Expand Down

0 comments on commit 18964e5

Please sign in to comment.