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);
}
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) {

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 CreateDirectory(const char* path, bool recursive);
bool Delete(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);
}
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 OsCreateDirectory(const char* pathName, int32_t recursive);
#endif

View file

@ -1,6 +1,8 @@
#ifndef BC_OS_PATH_HPP
#define BC_OS_PATH_HPP
#include <cstddef>
void OsGetExePath(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;
}
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) {

View file

@ -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) {