2022-06-21 12:50:39 +00:00
|
|
|
#include "../../filesystem.hpp"
|
|
|
|
#include <cstring>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#endif
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
char FSLib::dir_divisor = '/';
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
FSLib::Directory::Directory(const string &path_) : dir_path(path_) {}
|
|
|
|
|
|
|
|
FSLib::Directory::Iterator::Iterator(const Directory &d_)
|
|
|
|
: d(opendir(d_.path())) {
|
|
|
|
if (!exists(d_.path()) || !isDirectory(d_.path())) {
|
|
|
|
throw std::runtime_error(std::string("Directory ") + d_.path() +
|
|
|
|
" either doesn't exist or isn't a directory");
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
current_entry = readdir(d);
|
2022-06-21 12:50:39 +00:00
|
|
|
|
|
|
|
// skip "." and ".."
|
2022-07-21 18:17:24 +00:00
|
|
|
if (current_entry != nullptr && (!strcmp(current_entry->d_name, ".") ||
|
|
|
|
!strcmp(current_entry->d_name, "..")))
|
|
|
|
++(*this);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
FSLib::Directory::Iterator::Iterator(const Directory &d_,
|
|
|
|
const struct dirent *current_entry_)
|
|
|
|
: d(opendir(d_.path())), current_entry(current_entry_) {}
|
2022-06-21 12:50:39 +00:00
|
|
|
|
|
|
|
FSLib::Directory::Iterator::~Iterator() {
|
2022-07-21 18:17:24 +00:00
|
|
|
if (d)
|
|
|
|
closedir(d);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::exists(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
struct stat path_stat;
|
2022-07-21 18:17:24 +00:00
|
|
|
return stat(path.c_str(), &path_stat) == 0;
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
string FSLib::canonical(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
char_t *canonical_path = new char_t[PATH_MAX];
|
2022-07-21 18:17:24 +00:00
|
|
|
auto failed = realpath(path.c_str(), canonical_path) == nullptr;
|
2022-06-21 12:50:39 +00:00
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
if (failed) {
|
2022-06-21 12:50:39 +00:00
|
|
|
delete[] canonical_path;
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
string canonical_string{ canonical_path };
|
|
|
|
delete[] canonical_path;
|
|
|
|
return canonical_string;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::isDirectory(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
struct stat path_stat;
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
if (stat(path.c_str(), &path_stat) != 0)
|
2022-06-21 12:50:39 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
return S_ISDIR(path_stat.st_mode);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::rename(const string &file_a, const string &file_b) {
|
|
|
|
return ::rename(file_a.c_str(), file_b.c_str()) == 0;
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO do windows version
|
|
|
|
bool deleteRecursive(const string &dir) {
|
2022-07-21 18:17:24 +00:00
|
|
|
for (const auto &file : FSLib::Directory(dir)) {
|
2022-06-21 12:50:39 +00:00
|
|
|
auto path = dir + "/" + file;
|
2022-07-21 18:17:24 +00:00
|
|
|
if (FSLib::isDirectory(path)) {
|
|
|
|
if (!deleteRecursive(path)) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-21 18:17:24 +00:00
|
|
|
} else if (unlink(path.c_str()) != 0) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rmdir(dir.c_str()) == 0;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::deleteFile(const string &file) {
|
2022-06-21 12:50:39 +00:00
|
|
|
// TODO log
|
2022-07-21 18:17:24 +00:00
|
|
|
auto canon = canonical(file);
|
|
|
|
if (canon.empty()) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
if (isDirectory(canon)) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return deleteRecursive(canon);
|
|
|
|
}
|
|
|
|
return unlink(canon.c_str()) == 0;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::createDirectoryFull(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
uint64_t pos{};
|
|
|
|
// go through all directories leading to the last one
|
|
|
|
// and create them if they don't exist
|
|
|
|
do {
|
|
|
|
// get partial directory path
|
2022-07-21 18:17:24 +00:00
|
|
|
pos = path.find_first_of("/", pos);
|
|
|
|
if (pos > 0) {
|
|
|
|
auto dirname = path.substr(0, pos);
|
2022-06-21 12:50:39 +00:00
|
|
|
// create it if it doesn't exist
|
2022-07-21 18:17:24 +00:00
|
|
|
if (!FSLib::exists(dirname)) {
|
|
|
|
if (mkdir(dirname.c_str(),
|
|
|
|
S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
|
2022-06-21 12:50:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos++;
|
2022-07-21 18:17:24 +00:00
|
|
|
} while (pos < path.length() && pos != 0);
|
2022-06-21 12:50:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
2022-07-21 18:17:24 +00:00
|
|
|
return Iterator(*this, nullptr);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
2022-07-21 18:17:24 +00:00
|
|
|
return Iterator(*this, nullptr);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char_t const *FSLib::Directory::Iterator::operator*() const {
|
|
|
|
return current_entry->d_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
2022-07-21 18:17:24 +00:00
|
|
|
if (current_entry == nullptr)
|
2022-06-21 12:50:39 +00:00
|
|
|
return *this;
|
2022-07-21 18:17:24 +00:00
|
|
|
current_entry = readdir(d);
|
2022-06-21 12:50:39 +00:00
|
|
|
// skip . and ..
|
2022-07-21 18:17:24 +00:00
|
|
|
if (current_entry != nullptr && (!strcmp(current_entry->d_name, ".") ||
|
|
|
|
!strcmp(current_entry->d_name, "..")))
|
2022-06-21 12:50:39 +00:00
|
|
|
return operator++();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) const {
|
2022-06-21 12:50:39 +00:00
|
|
|
return i_other.current_entry == current_entry;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
string FSLib::getContainingDirectory(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
auto pos = path.find_last_of('/');
|
2022-07-21 18:17:24 +00:00
|
|
|
if (pos == string::npos) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
return path.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
string FSLib::getFileName(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
auto pos = path.find_last_of('/');
|
2022-07-21 18:17:24 +00:00
|
|
|
if (pos == string::npos) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return path;
|
|
|
|
}
|
2022-07-21 18:17:24 +00:00
|
|
|
return path.substr(pos + 1);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
string FSLib::getFileExtension(const string &path) {
|
2022-06-21 12:50:39 +00:00
|
|
|
auto pos = path.find_last_of('.');
|
2022-07-21 18:17:24 +00:00
|
|
|
if (pos == string::npos) {
|
2022-06-21 12:50:39 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return path.substr(pos + 1);
|
|
|
|
}
|