Use native API instead of filesystem

This commit is contained in:
Double Sine 2019-12-30 21:57:00 +08:00
parent 5dba642a1f
commit 00e80bfe88
No known key found for this signature in database
GPG Key ID: 44460E4F43EA8633

View File

@ -3,7 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
#include <setjmp.h> #include <setjmp.h>
#include <filesystem> #include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "ExceptionSystem.hpp" #include "ExceptionSystem.hpp"
static jmp_buf g_jmbuf; static jmp_buf g_jmbuf;
@ -105,55 +107,60 @@ namespace nkg::Misc {
[[nodiscard]] [[nodiscard]]
bool FsIsExist(std::string_view szPath) { bool FsIsExist(std::string_view szPath) {
std::error_code ec; struct stat s = {};
if (std::filesystem::exists(szPath, ec)) { if (stat(szPath.data(), &s) == 0) {
return true; return true;
} else { } else {
if (ec) { if (errno == ENOENT) {
throw ARL::SystemError(__FILE__, __LINE__, ec.value(), "std::filesystem::exists failed.");
} else {
return false; return false;
} else {
throw ARL::SystemError(__FILE__, __LINE__, errno, "stat failed.");
} }
} }
} }
[[nodiscard]] [[nodiscard]]
bool FsIsFile(std::string_view szPath) { bool FsIsFile(std::string_view szPath) {
std::error_code ec; struct stat s = {};
if (std::filesystem::is_regular_file(szPath, ec)) { if (stat(szPath.data(), &s) == 0) {
return true; return S_ISREG(s.st_mode);
} else {
if (ec) {
throw ARL::SystemError(__FILE__, __LINE__, ec.value(), "std::filesystem::is_regular_file failed.");
} else { } else {
if (errno == ENOENT) {
return false; return false;
} else {
throw ARL::SystemError(__FILE__, __LINE__, errno, "stat failed.");
} }
} }
} }
[[nodiscard]] [[nodiscard]]
bool FsIsDirectory(std::string_view szPath) { bool FsIsDirectory(std::string_view szPath) {
std::error_code ec; struct stat s = {};
if (std::filesystem::is_directory(szPath, ec)) { if (stat(szPath.data(), &s) == 0) {
return true; return S_ISDIR(s.st_mode);
} else {
if (ec) {
throw ARL::SystemError(__FILE__, __LINE__, ec.value(), "std::filesystem::is_directory failed.");
} else { } else {
if (errno == ENOENT) {
return false; return false;
} else {
throw ARL::SystemError(__FILE__, __LINE__, errno, "stat failed.");
} }
} }
} }
[[nodiscard]] [[nodiscard]]
std::string FsCurrentWorkingDirectory() { std::string FsCurrentWorkingDirectory() {
std::error_code ec; std::string path(256, '\x00');
std::string path = std::filesystem::current_path(ec); while (getcwd(path.data(), path.size()) == 0) {
if (ec) { if (errno == ERANGE) {
throw ARL::SystemError(__FILE__, __LINE__, ec.value(), "std::filesystem::current_path failed."); path.resize(path.size() * 2);
} else { } else {
return path; throw ARL::SystemError(__FILE__, __LINE__, errno, "getcwd failed.");
} }
}
path.resize(strlen(path.data()));
return path;
} }
} }