From 71fd5402c3d52ec2a1342af8d57d034e13089f63 Mon Sep 17 00:00:00 2001 From: superp00t Date: Wed, 23 Aug 2023 18:56:06 -0400 Subject: [PATCH] feat(os): implement OsCreateDirectory - added missing 'recursive' option --- bc/file/File.cpp | 13 +++++++++++ bc/file/File.hpp | 2 ++ bc/os/File.cpp | 4 ++++ bc/os/File.hpp | 2 ++ bc/os/Path.hpp | 2 ++ bc/system/file/posix/Stacked.cpp | 40 +++++++++++++++++++------------- bc/system/file/win/Stacked.cpp | 34 ++++++++++++++------------- 7 files changed, 65 insertions(+), 32 deletions(-) diff --git a/bc/file/File.cpp b/bc/file/File.cpp index 72709be..cf6664f 100644 --- a/bc/file/File.cpp +++ b/bc/file/File.cpp @@ -263,6 +263,19 @@ bool Copy(const char* source, const char* destination, bool overwrite) { return manager->Do(Filesystem::Call::Copy, &parms); } +bool CreateDirectory(const char* path, bool recursive) { + auto manager = System_File::Stacked::Manager(); + if (!manager) { + return false; + } + + System_File::Stacked::FileParms parms = {}; + parms.filename = path; + parms.flag = recursive; + + return manager->Do(Filesystem::Call::CreateDirectory, &parms); +} + bool ProcessDirFast(const char* path, void* param, ProcessDirCallback callback, bool unkflag) { auto manager = System_File::Stacked::Manager(); if (!manager) { diff --git a/bc/file/File.hpp b/bc/file/File.hpp index ea22d6a..3545107 100644 --- a/bc/file/File.hpp +++ b/bc/file/File.hpp @@ -16,6 +16,8 @@ void AddToLastErrorStack(int32_t errorcode, const char* msg, int32_t param_3); bool Copy(const char* source, const char* destination, bool overwrite); +bool CreateDirectory(const char* path, bool recursive); + bool Delete(const char* path); bool Exists(const char* path); diff --git a/bc/os/File.cpp b/bc/os/File.cpp index 341929b..296ba46 100644 --- a/bc/os/File.cpp +++ b/bc/os/File.cpp @@ -158,3 +158,7 @@ int32_t OsGetCurrentDirectory(size_t pathLen, char* pathName) { return Blizzard::File::GetWorkingDirectory(pathName, pathLen); } + +int32_t OsCreateDirectory(const char* pathName, int32_t recursive) { + +} diff --git a/bc/os/File.hpp b/bc/os/File.hpp index 48fd8b3..f78fe88 100644 --- a/bc/os/File.hpp +++ b/bc/os/File.hpp @@ -68,4 +68,6 @@ int32_t OsSetCurrentDirectory(const char* pathName); int32_t OsGetCurrentDirectory(size_t pathLen, char* pathName); +int32_t OsCreateDirectory(const char* pathName, int32_t recursive); + #endif diff --git a/bc/os/Path.hpp b/bc/os/Path.hpp index 59483fe..9ffebbd 100644 --- a/bc/os/Path.hpp +++ b/bc/os/Path.hpp @@ -1,6 +1,8 @@ #ifndef BC_OS_PATH_HPP #define BC_OS_PATH_HPP +#include + void OsGetExePath(char* buffer, size_t chars); void OsGetExeName(char* buffer, size_t chars); diff --git a/bc/system/file/posix/Stacked.cpp b/bc/system/file/posix/Stacked.cpp index bc5e354..799376d 100644 --- a/bc/system/file/posix/Stacked.cpp +++ b/bc/system/file/posix/Stacked.cpp @@ -50,7 +50,7 @@ bool Open(FileParms* parms) { flags |= O_EXCL; } - fd = ::open(pathNative.Str(), flags, 511); + fd = ::open(pathNative.Str(), flags, 0777); } else { fd = ::open(pathNative.Str(), flags); } @@ -379,6 +379,8 @@ bool CreateDirectory(FileParms* parms) { return false; } + auto recursive = parms->flag != 0; + char tmp[BC_FILE_MAX_PATH] = {}; struct stat sb; @@ -399,28 +401,34 @@ bool CreateDirectory(FileParms* parms) { } } - // Loop through path and call mkdir on path elements - for (auto p = tmp + 1; *p != '\0'; p++) { - if (*p == '/') { - *p = 0; - // test path - if (::stat(tmp, &sb) != 0) { - // path does not exist, create directory - if (::mkdir(tmp, 511) < 0) { + if (!recursive) { + if (::mkdir(tmp, 0777) < 0) { + return false; + } + } else { + // Loop through path and call mkdir on path elements + for (auto p = tmp + 1; *p != '\0'; p++) { + if (*p == '/') { + *p = 0; + // test path + if (::stat(tmp, &sb) != 0) { + // path does not exist, create directory + if (::mkdir(tmp, 0777) < 0) { + return false; + } + } else if (!S_ISDIR(sb.st_mode)) { + // not a directory return false; } - } else if (!S_ISDIR(sb.st_mode)) { - // not a directory - return false; + *p = '/'; } - *p = '/'; } } // check remaining path existence if (::stat(tmp, &sb) != 0) { // path does not exist, create directory - if (::mkdir(tmp, 511) < 0) { + if (::mkdir(tmp, 0777) < 0) { return false; } } else if (!S_ISDIR(sb.st_mode)) { @@ -582,9 +590,9 @@ bool SetAttributes(FileParms* parms) { } if (attributes & BC_FILE_ATTRIBUTE_READONLY) { - status = ::chmod(path.Str(), 444); + status = ::chmod(path.Str(), info.st_mode & 0222); } else { - status = ::chmod(path.Str(), 511); + status = ::chmod(path.Str(), info.st_mode & 0777); } if (status != 0) { diff --git a/bc/system/file/win/Stacked.cpp b/bc/system/file/win/Stacked.cpp index 5ddf004..8afe483 100644 --- a/bc/system/file/win/Stacked.cpp +++ b/bc/system/file/win/Stacked.cpp @@ -374,32 +374,34 @@ bool MakeAbsolutePath(FileParms* parms) { } bool CreateDirectory(FileParms* parms) { - auto path = parms->filename; - + auto path = parms->filename; + auto recursive = parms->flag != 0; char temp[300] = {0}; auto p = path; size_t count = 0; - while (*p != '\0') { - if (*p == '\\' || *p == '/') { - count++; - int32_t len = p - path; - if (len == 0) { - len = 1; - } - String::Copy(temp, path, len); - temp[len] = '\0'; + if (recursive) { + while (*p != '\0') { + if (*p == '\\' || *p == '/') { + count++; + int32_t len = p - path; + if (len == 0) { + len = 1; + } + String::Copy(temp, path, len); + temp[len] = '\0'; - if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) { - if (!::CreateDirectory(temp, nullptr)) { - if (::GetLastError() != ERROR_ALREADY_EXISTS) { - return false; + if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) { + if (!::CreateDirectory(temp, nullptr)) { + if (::GetLastError() != ERROR_ALREADY_EXISTS) { + return false; + } } } } + p++; } - p++; } if (count == 0) {