From 15043e77769c3ba60fca3cb229d624e8269b2f91 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 25 Mar 2018 09:25:46 +0100 Subject: [PATCH] Create save directories recursively. --- src/plat/unix/unixInit.c | 45 +++++++++++++++++++++++++++++++++----- src/plat/win32/win32Init.c | 45 +++++++++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/plat/unix/unixInit.c b/src/plat/unix/unixInit.c index 2a0c5ce..a6118ec 100644 --- a/src/plat/unix/unixInit.c +++ b/src/plat/unix/unixInit.c @@ -19,6 +19,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "unixInit.h" + +static void mkpath(const char *path); void createSaveFolder(void) { @@ -39,16 +41,49 @@ void createSaveFolder(void) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) { sprintf(dir, "%s/.local/share/blobwarsAttrition/%d", userHome, i); - if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) - { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); - exit(1); - } + + mkpath(dir); } sprintf(app.saveDir, "%s/.local/share/blobwarsAttrition", userHome); } +static void mkpath(const char *path) +{ + char dir[MAX_FILENAME_LENGTH]; + int i, rootPath; + + strcpy(dir, ""); + + rootPath = 1; + + for (i = 0 ; i < strlen(path) ; i++) + { + if (path[i] == '/') + { + if (!rootPath) + { + if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } + } + + rootPath = 0; + } + + dir[i] = path[i]; + dir[i + 1] = '\0'; + } + + if (mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } +} + void createScreenshotFolder(void) { mkdir("/tmp/blobwarsAttrition", S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH); diff --git a/src/plat/win32/win32Init.c b/src/plat/win32/win32Init.c index 3729a13..6a42913 100644 --- a/src/plat/win32/win32Init.c +++ b/src/plat/win32/win32Init.c @@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "win32Init.h" +static void mkpath(const char *path); + void createSaveFolder(void) { char *userHome; @@ -39,16 +41,49 @@ void createSaveFolder(void) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) { sprintf(dir, "%s\\blobwarsAttrition\\%d", userHome, i); - if (mkdir(dir) != 0 && errno != EEXIST) - { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); - exit(1); - } + + mkpath(dir); } sprintf(app.saveDir, "%s\\blobwarsAttrition", userHome); } +void mkpath(const char *path) +{ + char dir[MAX_FILENAME_LENGTH]; + int i, rootPath; + + strcpy(dir, ""); + + rootPath = 1; + + for (i = 0 ; i < strlen(path) ; i++) + { + if (path[i] == '\\' || i == strlen(path) - 1) + { + if (!rootPath) + { + if (mkdir(dir) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } + } + + rootPath = 0; + } + + dir[i] = path[i]; + dir[i + 1] = '\0'; + } + + if (mkdir(dir) != 0 && errno != EEXIST) + { + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Failed to create save dir '%s'.", dir); + exit(1); + } +} + void createScreenshotFolder(void) { dev.screenshotFolder = "./";