allow os.mkdir to recursively create a folder path.

This commit is contained in:
Tom van Dijck 2015-07-14 11:31:19 -07:00
parent 600ecf4cbd
commit bcba1a2e39
2 changed files with 35 additions and 32 deletions

View File

@ -8,26 +8,49 @@
#include "premake.h"
int os_mkdir(lua_State* L)
int do_mkdir(const char* path)
{
int z;
const char* path = luaL_checkstring(L, 1);
char sub_path[2048];
int i, z;
size_t path_length = strlen(path);
for (i = 0; i < path_length; ++i)
{
if (path[i] == '/' || path[i] == '\\')
{
memcpy(sub_path, path, i);
sub_path[i] = '\0';
#if PLATFORM_WINDOWS
z = CreateDirectory(path, NULL);
z = CreateDirectory(sub_path, NULL);
#else
z = (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
z = (mkdir(sub_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
#endif
if (!z)
{
return FALSE;
}
}
}
return TRUE;
}
int os_mkdir(lua_State* L)
{
const char* path = luaL_checkstring(L, 1);
int z = do_mkdir(path);
if (!z)
{
lua_pushnil(L);
lua_pushfstring(L, "unable to create directory '%s'", path);
return 2;
}
else
{
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 1);
return 1;
}

View File

@ -86,27 +86,7 @@ static int write_link(const char* filename, const char* bytes, size_t count)
#endif
}
static void r_mkdir(const char* full_path)
{
size_t path_length = strlen(full_path);
size_t i;
for (i = 0; i < path_length; ++i)
{
if (full_path[i] == '/' || full_path[i] == '\\')
{
char* sub_path = (char*)malloc(i + 1);
memcpy(sub_path, full_path, i);
sub_path[i] = 0;
#if PLATFORM_WINDOWS
_mkdir(sub_path);
#else
mkdir(sub_path, 0755);
#endif
free(sub_path);
}
}
}
extern int do_mkdir(const char* path);
static void parse_path(const char* full_name, char* filename, char* directory)
{
@ -171,7 +151,7 @@ static int extract(const char* src, const char* destination)
sprintf(appended_full_name, "%s/%s", destination, full_name);
parse_path(appended_full_name, filename, directory);
r_mkdir(directory);
do_mkdir(directory);
// is this a symbolic link?
if (is_symlink(opsys, attrib))