Uppss, se me olvido actualizar los ficheros que están en los dos sitios pero con distinta fecha.
Edited: porras, como se jode la indentacion del código XD
Código:
#include <iostream>
#include <set>
#include <string>
#include <boost/filesystem.hpp>
std::set<boost::filesystem::path> get_files(constboost::filesystem::path &path)
{
std::set<boost::filesystem::path> files;
boost::filesystem::recursive_directory_iterator it(path);//, boost::filesystem::skip_permission_denied);
boost::filesystem::recursive_directory_iterator end;
while ( it != end )
{
files.insert(boost::filesystem::relative(it->path(), path));
try
{
++it;
} catch (boost::filesystem::filesystem_error e)
{
// para que no pete el .Spotlight-V100
it.disable_recursion_pending();
++it;
}
}
std::cout << files.size() << " files at " << path.string() << std::endl;
return files;
}
bool never(constboost::filesystem::path &a, constboost::filesystem::path &b)
{
returnfalse;
}
bool always(constboost::filesystem::path &a, constboost::filesystem::path &b)
{
returntrue;
}
// return date(a) > date(b)
bool newer(constboost::filesystem::path &a, constboost::filesystem::path &b)
{
if ( boost::filesystem::is_regular_file(a) && boost::filesystem::is_regular_file(b) )
returnboost::filesystem::last_write_time(a) > boost::filesystem::last_write_time(b);
else
returnfalse;
}
// return a - b
std::set<boost::filesystem::path> sub(conststd::set<boost::filesystem::path> &a, std::set<boost::filesystem::path> &b)
{
std::set<boost::filesystem::path> tmp;
for ( auto &path : a )
{
if ( b.find(path) == b.end() )
tmp.insert(path);
}
return tmp;
}
std::set<boost::filesystem::path> cmp(constboost::filesystem::path &abase, conststd::set<boost::filesystem::path> &a
, const boost::filesystem::path &bbase, std::set<boost::filesystem::path> &b
, bool(*fn)(const boost::filesystem::path &a, const boost::filesystem::path &b) = never)
{
std::set<boost::filesystem::path> tmp;
for ( auto &path : a )
{
auto it = b.find(path);
if ( it == b.end() || fn(abase / path, bbase / *it) )
tmp.insert(path);
}
return tmp;
}
int main(int argc, const char * argv[])
{
if ( argc < 3 )
{
std::cout << "dsync SRC DST\nby swap d0 9/4/2022\n";
return1;
}
boost::system::error_code error;
auto srcPath = boost::filesystem::canonical(argv[1], boost::filesystem::current_path().string(), error);
auto dstPath = boost::filesystem::canonical(argv[2], boost::filesystem::current_path().string(), error);
if ( !boost::filesystem::exists(srcPath) )
{
std::cout << "Source directory not found: " << srcPath.string() << std::endl;;
return1;
}
if ( !boost::filesystem::exists(dstPath) )
{
std::cout << "Destination directory not found: " << dstPath.string() << std::endl;;
return1;
}
auto srcFiles = get_files(srcPath);
auto dstFiles = get_files(dstPath);
auto copyFiles = sub(srcFiles, dstFiles);
auto updateFiles = cmp(srcPath, srcFiles, dstPath, dstFiles, newer);
auto deleteFiles = sub(dstFiles, srcFiles);
// delete files
for ( auto &file : deleteFiles )
{
std::cout << "Deleting " << (dstPath / file).string() << std::endl;
try
{
boost::filesystem::remove(dstPath / file);
}
catch (...)
{
// para el maldito .Spotlight-V100
std::cout << "Can't delete " << (dstPath / file).string() << std::endl;
}
}
// copy files
for ( auto &file : copyFiles )
{
std::cout << "Copying " << (srcPath / file).string() << std::endl;
boost::filesystem::copy(dstPath /file, dstPath / file);
}
// update files
for ( auto &file : updateFiles )
{
std::cout << "Updating " << (srcPath / file).string() << std::endl;
boost::filesystem::copy_file(srcPath /file, dstPath / file, boost::filesystem::copy_option::overwrite_if_exists);
}
return0;
}
Marcadores