summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-28 08:12:40 -0700
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-04-08 16:29:46 +0200
commit2e9261416474666d0305419aae5713e6408ef1c7 (patch)
tree1c9b6101a05e1813286d4aa4036ddc43cc663d77 /src
parentc4b8167a69c46fe89a3c792f97adab7d2f9fb40c (diff)
downloadmana-2e9261416474666d0305419aae5713e6408ef1c7.tar.gz
mana-2e9261416474666d0305419aae5713e6408ef1c7.tar.bz2
mana-2e9261416474666d0305419aae5713e6408ef1c7.tar.xz
mana-2e9261416474666d0305419aae5713e6408ef1c7.zip
Use C++17 to implement mkdir_r
Should be a lot less fragile and might also help to resolve encoding issues.
Diffstat (limited to 'src')
-rw-r--r--src/utils/mkdir.cpp75
-rw-r--r--src/utils/mkdir.h2
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 <climits>
#include <cstring>
#include <cerrno>
-
-#if defined _WIN32
-#include <windows.h>
-#endif
-
-#include <sys/stat.h>
+#include <filesystem>
+#include <iostream>
#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.
*