tv_rename/filesystem.cpp

30 lines
826 B
C++
Raw Normal View History

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));
if( realpath(path.c_str(), canonical_path) == nullptr )
return "";
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
}