67 lines
1.7 KiB
C
67 lines
1.7 KiB
C
#include "internal.h"
|
|
|
|
bool dir_exists(cstr path){
|
|
if(path == NULL || path[0] == 0)
|
|
return true;
|
|
|
|
if(path[0]=='.'){
|
|
if(path[1]==0 || (path[1]==path_sep && path[1]==0))
|
|
return true; // dir . or ./ always exists
|
|
// else if(path[1]=='.' && path[2]==path_sep)
|
|
//TODO path_resolve because windows doesnt recognize .\ pattern
|
|
}
|
|
#if TLIBC_FS_USE_WINDOWS_H
|
|
DWORD dwAttrib = GetFileAttributes(path);
|
|
return (bool)(
|
|
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
|
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory
|
|
#else
|
|
struct stat stats;
|
|
i32 rez=stat(path, &stats);
|
|
return (bool)(
|
|
(rez!=-1) && // file exists
|
|
(S_ISDIR(stats.st_mode))); // file is a directory
|
|
#endif
|
|
}
|
|
|
|
Result(bool) dir_create(cstr path){
|
|
Deferral(4);
|
|
|
|
if (path == NULL || path[0] == 0 || dir_exists(path)){
|
|
Return RESULT_VALUE(i, false);
|
|
}
|
|
|
|
try_void(dir_createParent(path));
|
|
|
|
#if TLIBC_FS_USE_WINDOWS_H
|
|
if(!CreateDirectory(path, NULL))
|
|
#else
|
|
if(mkdir(path, 0777) == -1)
|
|
#endif
|
|
{
|
|
char* errno_s = strerror_malloc(errno);
|
|
ResultVar(void) err = RESULT_ERROR_FMT(
|
|
"Can't create dicectory '%s': %s",
|
|
path, errno_s);
|
|
free(errno_s);
|
|
Return err;
|
|
}
|
|
|
|
Return RESULT_VALUE(i, true);
|
|
}
|
|
|
|
Result(bool) dir_createParent(cstr path){
|
|
Deferral(4);
|
|
|
|
str parent_dir_str = path_dirname(str_from_cstr((void*)path));
|
|
if(parent_dir_str.len == 0){
|
|
Return RESULT_VALUE(i, false);
|
|
}
|
|
|
|
char* parent_dir_cstr = str_copy(parent_dir_str).data;
|
|
Defer(free(parent_dir_cstr));
|
|
|
|
try(bool result, i, dir_create(parent_dir_cstr));
|
|
Return RESULT_VALUE(i, result);
|
|
}
|