summaryrefslogtreecommitdiff
path: root/src/fileio.cpp
diff options
context:
space:
mode:
authorCharles Pigott <charlespigott@googlemail.com>2020-02-15 12:30:16 +0000
committerNiels Martin Hansen <nielsm@indvikleren.dk>2020-02-19 11:28:42 +0100
commit4bc78835e8fbf320bdae976708e64bab55f9be1a (patch)
tree8c9e9c04ee9605d905275b6c7e59276b65caa97f /src/fileio.cpp
parent5c19668fdb3c1b77439bde1968a4075ac02a2b79 (diff)
downloadopenttd-4bc78835e8fbf320bdae976708e64bab55f9be1a.tar.xz
Fix #6399: Create parent directories if they don't already exist
Diffstat (limited to 'src/fileio.cpp')
-rw-r--r--src/fileio.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index dc3813bca..5797c592b 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -527,10 +527,24 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir,
/**
* Create a directory with the given name
+ * If the parent directory does not exist, it will try to create that as well.
* @param name the new name of the directory
*/
void FioCreateDirectory(const char *name)
{
+ char dirname[MAX_PATH];
+ strecpy(dirname, name, lastof(dirname));
+ char *p = strrchr(dirname, PATHSEPCHAR);
+ if (p != nullptr) {
+ *p = '\0';
+ DIR *dir = ttd_opendir(dirname);
+ if (dir == nullptr) {
+ FioCreateDirectory(dirname); // Try creating the parent directory, if we couldn't open it
+ } else {
+ closedir(dir);
+ }
+ }
+
/* Ignore directory creation errors; they'll surface later on, and most
* of the time they are 'directory already exists' errors anyhow. */
#if defined(_WIN32)