2024-04-09 01:44:15 -07:00
|
|
|
#include "ehs/io/Directory_LNX.h"
|
|
|
|
#include "ehs/Log.h"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2024-04-23 22:29:49 -07:00
|
|
|
#include <cerrno>
|
|
|
|
#include <sys/stat.h>
|
2024-04-09 01:44:15 -07:00
|
|
|
|
|
|
|
namespace ehs
|
|
|
|
{
|
|
|
|
Array<Str_8> Directory::GetAllFiles(const Str_8& dir)
|
|
|
|
{
|
|
|
|
Array<Str_8> result;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
if (!dir.Size())
|
|
|
|
{
|
2024-06-29 23:38:27 -07:00
|
|
|
EHS_LOG_INT(LogType::WARN, 1, "The given directory was empty.");
|
2024-04-23 22:29:49 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:44:15 -07:00
|
|
|
DIR* hdl = opendir(dir);
|
2024-04-23 22:29:49 -07:00
|
|
|
if (!hdl)
|
2024-04-09 01:44:15 -07:00
|
|
|
{
|
2024-06-29 23:38:27 -07:00
|
|
|
EHS_LOG_INT(LogType::ERR, 2, "Failed to open directory, \"" + dir + "\".");
|
2024-04-09 01:44:15 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirent* entry;
|
|
|
|
while ((entry = readdir(hdl)))
|
|
|
|
if (entry->d_type == DT_REG)
|
|
|
|
result.Push(entry->d_name);
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
if (closedir(hdl) == -1)
|
|
|
|
{
|
2024-06-29 23:38:27 -07:00
|
|
|
EHS_LOG_INT(LogType::ERR, 3, "Failed to close directory, \"" + dir + "\".");
|
2024-04-23 22:29:49 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
EHS_LOG_SUCCESS();
|
2024-04-09 01:44:15 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-04-23 22:29:49 -07:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2024-04-09 01:44:15 -07:00
|
|
|
}
|