From 2e9261416474666d0305419aae5713e6408ef1c7 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 28 Mar 2024 08:12:40 -0700 Subject: Use C++17 to implement mkdir_r Should be a lot less fragile and might also help to resolve encoding issues. --- src/utils/mkdir.cpp | 75 ++++++----------------------------------------------- src/utils/mkdir.h | 2 +- 2 files changed, 9 insertions(+), 68 deletions(-) diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp index c17b5102..c2165710 100644 --- a/src/utils/mkdir.cpp +++ b/src/utils/mkdir.cpp @@ -1,6 +1,6 @@ /* * The Mana Client - * Copyright (C) 2010-2012 The Mana Developers + * Copyright (C) 2010-2024 The Mana Developers * * This file is part of The Mana Client. * @@ -21,12 +21,8 @@ #include #include #include - -#if defined _WIN32 -#include -#endif - -#include +#include +#include #ifdef MKDIR_TEST // compile with -DMKDIR_TEST to get a standalone binary @@ -38,67 +34,12 @@ /// Create a directory, making leading components first if necessary int mkdir_r(const char *pathname) { - size_t len = strlen(pathname); - char tmp[len+2]; - char *p; - - strcpy(tmp, pathname); - - // terminate the pathname with '/' - if (tmp[len-1] != '/') { - tmp[len] = '/'; - tmp[len+1] = '\0'; - } - - for (p=tmp; *p; p++) { -#if defined _WIN32 - if (*p == '/' || *p == '\\') -#else - if (*p == '/') -#endif - { - *p = '\0'; - // ignore a slash at the beginning of a path - if (strlen(tmp) == 0){ - *p = '/'; - continue; - } + try { + std::filesystem::create_directories(pathname); + return 0; + } catch (const std::exception&) {} - // check if the name already exists, but not as directory - struct stat statbuf; - if (!stat(tmp, &statbuf)) - { - if (S_ISDIR(statbuf.st_mode)) - { - *p = '/'; - continue; - } - else - return -1; - } - -#if defined _WIN32 - if (!CreateDirectory(tmp, 0)) -#else - if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) -#endif - { -#if defined _WIN32 - // hack, hack. just assume that x: might be a drive - // letter, and try again - if (!(strlen(tmp) == 2 && - !strcmp(tmp + 1, ":"))) -#endif - return -1; - } - -#ifdef MKDIR_TEST - printf("%s\n", tmp); -#endif - *p = '/'; - } - } - return 0; + return -1; } #ifdef MKDIR_TEST diff --git a/src/utils/mkdir.h b/src/utils/mkdir.h index 5929b1d9..3bd76145 100644 --- a/src/utils/mkdir.h +++ b/src/utils/mkdir.h @@ -1,6 +1,6 @@ /* * The Mana Client - * Copyright (C) 2010-2012 The Mana Developers + * Copyright (C) 2010-2024 The Mana Developers * * This file is part of The Mana Client. * -- cgit v1.2.3-70-g09d2