cellar/src/fs.cpp

66 lines
1.8 KiB
C++
Raw Normal View History

2017-03-12 22:32:46 -07:00
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "fs.hpp"
#include "output.hpp"
2017-03-12 22:32:46 -07:00
using namespace std;
2017-03-27 17:53:28 -07:00
using namespace boost;
2017-03-12 22:32:46 -07:00
vector<string> cellar::fs::listdir(string path) {
vector<string> result;
2017-03-27 17:53:28 -07:00
filesystem::path cwd(path);
filesystem::directory_iterator iter_end;
for (filesystem::directory_iterator iter_cwd(cwd); iter_cwd != iter_end; ++iter_cwd) {
string item = iter_cwd->path().filename().native();
result.push_back(item);
2017-03-12 22:32:46 -07:00
}
return result;
}
2017-03-27 17:53:28 -07:00
bool cellar::fs::recursive_copy(string src, string dst) {
if (!filesystem::exists(dst)) {
bool success = filesystem::create_directory(dst);
if (!success) { return false; }
}
for (string itemrel : cellar::fs::listdir(src)) {
string itemabs = src + "/" + itemrel;
string targetabs = dst + "/" + itemrel;
auto itemstat = filesystem::symlink_status(itemabs);
if (filesystem::is_directory(itemstat)) { recursive_copy(itemabs, targetabs); }
else if (filesystem::is_symlink(itemstat)) {
auto symlinkpath = filesystem::read_symlink(itemabs);
filesystem::create_symlink(symlinkpath, targetabs);
}
else { filesystem::copy(itemabs, targetabs); }
}
return true;
}
bool cellar::fs::recursive_remove(string target) {
if (!filesystem::exists(target)) { return false; }
for (string itemrel : cellar::fs::listdir(target)) {
string itemabs = target + "/" + itemrel;
auto itemstat = filesystem::symlink_status(itemabs);
if (filesystem::is_directory(itemstat)) { recursive_remove(itemabs); }
else { filesystem::remove(itemabs); }
}
filesystem::remove(target);
return true;
}