feat(os): implement OsCreateDirectory - added missing 'recursive' option

This commit is contained in:
phaneron 2023-08-23 18:56:06 -04:00
parent b4e14cf4d5
commit 71fd5402c3
7 changed files with 65 additions and 32 deletions

View file

@ -263,6 +263,19 @@ bool Copy(const char* source, const char* destination, bool overwrite) {
return manager->Do(Filesystem::Call::Copy, &parms); 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) { bool ProcessDirFast(const char* path, void* param, ProcessDirCallback callback, bool unkflag) {
auto manager = System_File::Stacked::Manager(); auto manager = System_File::Stacked::Manager();
if (!manager) { if (!manager) {

View file

@ -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 Copy(const char* source, const char* destination, bool overwrite);
bool CreateDirectory(const char* path, bool recursive);
bool Delete(const char* path); bool Delete(const char* path);
bool Exists(const char* path); bool Exists(const char* path);

View file

@ -158,3 +158,7 @@ int32_t OsGetCurrentDirectory(size_t pathLen, char* pathName) {
return Blizzard::File::GetWorkingDirectory(pathName, pathLen); return Blizzard::File::GetWorkingDirectory(pathName, pathLen);
} }
int32_t OsCreateDirectory(const char* pathName, int32_t recursive) {
}

View file

@ -68,4 +68,6 @@ int32_t OsSetCurrentDirectory(const char* pathName);
int32_t OsGetCurrentDirectory(size_t pathLen, char* pathName); int32_t OsGetCurrentDirectory(size_t pathLen, char* pathName);
int32_t OsCreateDirectory(const char* pathName, int32_t recursive);
#endif #endif

View file

@ -1,6 +1,8 @@
#ifndef BC_OS_PATH_HPP #ifndef BC_OS_PATH_HPP
#define BC_OS_PATH_HPP #define BC_OS_PATH_HPP
#include <cstddef>
void OsGetExePath(char* buffer, size_t chars); void OsGetExePath(char* buffer, size_t chars);
void OsGetExeName(char* buffer, size_t chars); void OsGetExeName(char* buffer, size_t chars);

View file

@ -50,7 +50,7 @@ bool Open(FileParms* parms) {
flags |= O_EXCL; flags |= O_EXCL;
} }
fd = ::open(pathNative.Str(), flags, 511); fd = ::open(pathNative.Str(), flags, 0777);
} else { } else {
fd = ::open(pathNative.Str(), flags); fd = ::open(pathNative.Str(), flags);
} }
@ -379,6 +379,8 @@ bool CreateDirectory(FileParms* parms) {
return false; return false;
} }
auto recursive = parms->flag != 0;
char tmp[BC_FILE_MAX_PATH] = {}; char tmp[BC_FILE_MAX_PATH] = {};
struct stat sb; struct stat sb;
@ -399,28 +401,34 @@ bool CreateDirectory(FileParms* parms) {
} }
} }
// Loop through path and call mkdir on path elements if (!recursive) {
for (auto p = tmp + 1; *p != '\0'; p++) { if (::mkdir(tmp, 0777) < 0) {
if (*p == '/') { return false;
*p = 0; }
// test path } else {
if (::stat(tmp, &sb) != 0) { // Loop through path and call mkdir on path elements
// path does not exist, create directory for (auto p = tmp + 1; *p != '\0'; p++) {
if (::mkdir(tmp, 511) < 0) { 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; return false;
} }
} else if (!S_ISDIR(sb.st_mode)) { *p = '/';
// not a directory
return false;
} }
*p = '/';
} }
} }
// check remaining path existence // check remaining path existence
if (::stat(tmp, &sb) != 0) { if (::stat(tmp, &sb) != 0) {
// path does not exist, create directory // path does not exist, create directory
if (::mkdir(tmp, 511) < 0) { if (::mkdir(tmp, 0777) < 0) {
return false; return false;
} }
} else if (!S_ISDIR(sb.st_mode)) { } else if (!S_ISDIR(sb.st_mode)) {
@ -582,9 +590,9 @@ bool SetAttributes(FileParms* parms) {
} }
if (attributes & BC_FILE_ATTRIBUTE_READONLY) { if (attributes & BC_FILE_ATTRIBUTE_READONLY) {
status = ::chmod(path.Str(), 444); status = ::chmod(path.Str(), info.st_mode & 0222);
} else { } else {
status = ::chmod(path.Str(), 511); status = ::chmod(path.Str(), info.st_mode & 0777);
} }
if (status != 0) { if (status != 0) {

View file

@ -374,32 +374,34 @@ bool MakeAbsolutePath(FileParms* parms) {
} }
bool CreateDirectory(FileParms* parms) { bool CreateDirectory(FileParms* parms) {
auto path = parms->filename; auto path = parms->filename;
auto recursive = parms->flag != 0;
char temp[300] = {0}; char temp[300] = {0};
auto p = path; auto p = path;
size_t count = 0; size_t count = 0;
while (*p != '\0') { if (recursive) {
if (*p == '\\' || *p == '/') { while (*p != '\0') {
count++; if (*p == '\\' || *p == '/') {
int32_t len = p - path; count++;
if (len == 0) { int32_t len = p - path;
len = 1; if (len == 0) {
} len = 1;
String::Copy(temp, path, len); }
temp[len] = '\0'; String::Copy(temp, path, len);
temp[len] = '\0';
if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) { if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) {
if (!::CreateDirectory(temp, nullptr)) { if (!::CreateDirectory(temp, nullptr)) {
if (::GetLastError() != ERROR_ALREADY_EXISTS) { if (::GetLastError() != ERROR_ALREADY_EXISTS) {
return false; return false;
}
} }
} }
} }
p++;
} }
p++;
} }
if (count == 0) { if (count == 0) {