#include "ehs/io/Directory_LNX.h" #include "ehs/Log.h" #include #include #include namespace ehs { Array Directory::GetAllFiles(const Str_8& dir) { Array 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 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(); } }