2019-01-03 18:01:43 +00:00
|
|
|
#include "filesystem.hpp"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <linux/limits.h>
|
|
|
|
|
|
|
|
bool FSLib::exists(const std::string &path) {
|
2019-01-04 19:19:08 +00:00
|
|
|
struct stat path_stat;
|
|
|
|
return stat( path.c_str(), &path_stat ) == 0;
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FSLib::isDirectory(const std::string &path) {
|
2019-01-04 19:19:08 +00:00
|
|
|
struct stat path_stat;
|
|
|
|
stat( path.c_str(), &path_stat );
|
|
|
|
return S_ISDIR(path_stat.st_mode);
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FSLib::canonical(const std::string &path) {
|
2019-01-04 19:19:08 +00:00
|
|
|
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
|
2019-01-06 19:40:16 +00:00
|
|
|
if( realpath(path.c_str(), canonical_path) == nullptr ) {
|
|
|
|
free(canonical_path);
|
2019-01-04 19:19:08 +00:00
|
|
|
return "";
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
2019-01-04 19:19:08 +00:00
|
|
|
std::string canonical_string{canonical_path};
|
|
|
|
free(canonical_path);
|
|
|
|
return canonical_string;
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FSLib::rename(const std::string &file_a, const std::string &file_b) {
|
2019-01-04 19:19:08 +00:00
|
|
|
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 19:40:16 +00:00
|
|
|
FSLib::Directory::iterator FSLib::Directory::begin() {
|
|
|
|
return Iterator(dir_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::begin() const {
|
|
|
|
return Iterator(dir_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cbegin() const {
|
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
|
|
|
return Iterator(dir_path, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
|
|
|
return Iterator(dir_path, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cend() const {
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *FSLib::Directory::path() const {
|
|
|
|
return dir_path.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FSLib::Directory::Iterator::operator*() {
|
|
|
|
return current_entry->d_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
|
|
|
if( current_entry == nullptr )
|
|
|
|
return *this;
|
|
|
|
current_entry = readdir(d);
|
|
|
|
if( current_entry != nullptr && (!strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..")) )
|
|
|
|
operator++();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::Iterator FSLib::Directory::Iterator::operator++(int) {
|
|
|
|
Iterator ret(*this);
|
|
|
|
operator++();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) {
|
|
|
|
return i_other.current_entry == current_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FSLib::Directory::Iterator::operator!=(const Iterator &i_other) {
|
|
|
|
return i_other.current_entry != current_entry;
|
|
|
|
}
|