84 lines
1.6 KiB
C++
84 lines
1.6 KiB
C++
#include "ehs/io/Directory_LNX.h"
|
|
#include "ehs/Log.h"
|
|
|
|
#include <dirent.h>
|
|
#include <cerrno>
|
|
#include <sys/stat.h>
|
|
|
|
namespace ehs
|
|
{
|
|
Array<Str_8> Directory::GetAllFiles(const Str_8& dir)
|
|
{
|
|
Array<Str_8> result;
|
|
|
|
if (!dir.Size())
|
|
{
|
|
EHS_LOG_INT(LogType::WARN, 1, "The given directory was empty.");
|
|
return result;
|
|
}
|
|
|
|
DIR* hdl = opendir(dir);
|
|
if (!hdl)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 2, "Failed to open directory, \"" + dir + "\".");
|
|
return result;
|
|
}
|
|
|
|
dirent* entry;
|
|
while ((entry = readdir(hdl)))
|
|
if (entry->d_type == DT_REG)
|
|
result.Push(entry->d_name);
|
|
|
|
if (closedir(hdl) == -1)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 3, "Failed to close directory, \"" + dir + "\".");
|
|
return result;
|
|
}
|
|
|
|
EHS_LOG_SUCCESS();
|
|
|
|
return result;
|
|
}
|
|
|
|
void Directory::CreateRecursive(Str_8 dir)
|
|
{
|
|
dir = dir.ReplaceAll("\\", "/");
|
|
|
|
const Vector<Str_8> dirs = dir.Split("/");
|
|
|
|
for (UInt_64 i = 0; i < dirs.Size(); ++i)
|
|
{
|
|
const Str_8 final = (Str_8&&)dirs[i];
|
|
for (UInt_64 x = 0; x < i; ++x)
|
|
dirs[i] += dirs[x] + "/";
|
|
|
|
dirs[i] += final;
|
|
|
|
if (mkdir(dirs[i], S_IRWXU | S_IRWXG | S_IRWXO) == -1)
|
|
{
|
|
if (const SInt_32 code = errno; code != EEXIST)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 1, "Failed to create directory, \"" + dirs[i] + "\" with error #" + Str_8::FromNum(code) + ".");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
EHS_LOG_SUCCESS();
|
|
}
|
|
|
|
void Directory::Create(const Str_8 &dir)
|
|
{
|
|
if (mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
|
|
{
|
|
if (const SInt_32 code = errno; code != EEXIST)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 1, "Failed to create directory with error #" + Str_8::FromNum(code) + ".");
|
|
return;
|
|
}
|
|
}
|
|
|
|
EHS_LOG_SUCCESS();
|
|
}
|
|
}
|