2017-03-12 22:32:46 -07:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
#include "cellar.hpp"
|
2017-03-12 22:32:46 -07:00
|
|
|
#include "fs.hpp"
|
2017-03-23 18:51:11 -07:00
|
|
|
#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-27 22:14:41 -07:00
|
|
|
using namespace cellar;
|
|
|
|
using namespace fs;
|
2017-03-12 22:32:46 -07:00
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
// these are not necessarily the final function signatures for callbacks
|
|
|
|
// this only exists so i don't have to come back and do the ductwork for
|
|
|
|
// callback functionality later
|
|
|
|
void cbnull_copy(string src, string dst) { return; }
|
|
|
|
void cbnull_remove(string src) { return; }
|
|
|
|
|
|
|
|
vector<string> fs::listdir(string path) {
|
2017-03-12 22:32:46 -07:00
|
|
|
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 22:14:41 -07:00
|
|
|
bool fs::recursive_copy(string src, string dst, CopyCallbackFunc callback) {
|
2017-03-27 17:53:28 -07:00
|
|
|
if (!filesystem::exists(dst)) {
|
2017-03-27 22:14:41 -07:00
|
|
|
if (dryrun) { output::statement("mkdir: " + dst); }
|
|
|
|
else {
|
|
|
|
bool success = filesystem::create_directory(dst);
|
|
|
|
if (!success) { return false; }
|
|
|
|
}
|
2017-03-27 17:53:28 -07:00
|
|
|
}
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
for (string itemrel : listdir(src)) {
|
2017-03-27 17:53:28 -07:00
|
|
|
string itemabs = src + "/" + itemrel;
|
|
|
|
string targetabs = dst + "/" + itemrel;
|
|
|
|
|
|
|
|
auto itemstat = filesystem::symlink_status(itemabs);
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
if (filesystem::is_directory(itemstat)) { recursive_copy(itemabs, targetabs, callback); }
|
2017-03-27 17:53:28 -07:00
|
|
|
else if (filesystem::is_symlink(itemstat)) {
|
|
|
|
auto symlinkpath = filesystem::read_symlink(itemabs);
|
2017-03-27 22:14:41 -07:00
|
|
|
if (dryrun) { output::statement("symlink: " + symlinkpath.native() + " => " + targetabs); }
|
|
|
|
else { filesystem::create_symlink(symlinkpath, targetabs); }
|
2017-03-27 17:53:28 -07:00
|
|
|
}
|
2017-03-27 22:14:41 -07:00
|
|
|
else {
|
|
|
|
if (dryrun) { output::statement("copy: " + itemabs + " => " + targetabs); }
|
|
|
|
else { filesystem::copy(itemabs, targetabs); }
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(itemabs, targetabs);
|
2017-03-27 17:53:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-27 21:03:35 -07:00
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
bool fs::recursive_copy(string src, string dst) { return recursive_copy(src, dst, cbnull_copy); }
|
|
|
|
|
|
|
|
bool fs::recursive_remove(string target, RemoveCallbackFunc callback) {
|
2017-03-27 21:03:35 -07:00
|
|
|
if (!filesystem::exists(target)) { return false; }
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
for (string itemrel : listdir(target)) {
|
2017-03-27 21:03:35 -07:00
|
|
|
string itemabs = target + "/" + itemrel;
|
|
|
|
|
|
|
|
auto itemstat = filesystem::symlink_status(itemabs);
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
if (filesystem::is_directory(itemstat)) { recursive_remove(itemabs, callback); }
|
|
|
|
else {
|
|
|
|
if (dryrun) { output::error("rm: " + itemabs); }
|
|
|
|
else { filesystem::remove(itemabs); }
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(itemabs);
|
2017-03-27 21:03:35 -07:00
|
|
|
}
|
|
|
|
|
2017-03-27 22:14:41 -07:00
|
|
|
if (dryrun) { output::error("rm: " + target); }
|
|
|
|
else { filesystem::remove(target); }
|
2017-03-27 21:03:35 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-27 22:14:41 -07:00
|
|
|
|
|
|
|
bool fs::recursive_remove(string target) { return recursive_remove(target, cbnull_remove); }
|