Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const char *test_names[] = {
"da_foreach",
"temp_aligned_alloc",
"temp_path_comps",
"mkdir_collision",
"temp_running_executable_path",
"no_echo",
"cmd_run_dont_reset",
Expand Down
12 changes: 12 additions & 0 deletions nob.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
# define _WINCON_
# include <windows.h>
# include <direct.h>
# include <sys/stat.h>
# include <io.h>
# include <shellapi.h>
#else
Expand Down Expand Up @@ -1192,6 +1193,17 @@ NOBDEF bool nob_mkdir_if_not_exists(const char *path)
#endif
if (result < 0) {
if (errno == EEXIST) {
#ifdef _WIN32
struct _stat st;
bool is_dir = _stat(path, &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFDIR;
#else
struct stat st;
bool is_dir = stat(path, &st) == 0 && S_ISDIR(st.st_mode);
#endif
if (!is_dir) {
nob_log(NOB_ERROR, "could not use `%s` as a directory", path);
return false;
}
#ifndef NOB_NO_ECHO
nob_log(NOB_INFO, "directory `%s` already exists", path);
#endif // NOB_NO_ECHO
Expand Down
13 changes: 13 additions & 0 deletions tests/mkdir_collision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "shared.h"

int main(void)
{
if (!mkdir_if_not_exists("dir")) return 1;
if (!mkdir_if_not_exists("dir/")) return 1;
if (!write_entire_file("file", NULL, 0)) return 1;
printf("mkdir over file: %s\n", mkdir_if_not_exists("file") ? "success" : "failure");
#ifndef _WIN32
if (symlink("dir", "dir-link") == 0 && !mkdir_if_not_exists("dir-link")) return 1;
#endif
return 0;
}
1 change: 1 addition & 0 deletions tests/mkdir_collision.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mkdir over file: failure
1 change: 1 addition & 0 deletions tests/mkdir_collision.win32.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mkdir over file: failure