mergerfs/src/mfm_dup.cpp

72 lines
1.9 KiB
C++
Raw Normal View History

2025-09-01 01:51:33 -05:00
#include "mfm_options.hpp"
2025-09-01 13:41:00 -05:00
#include "str.hpp"
2025-09-01 15:39:00 -05:00
#include "fs_clonepath.hpp"
2025-09-01 01:51:33 -05:00
#include "fmt/core.h"
2025-09-01 13:41:00 -05:00
#include <filesystem>
2025-09-01 15:41:14 -05:00
namespace stdfs = std::filesystem;
2025-09-01 01:51:33 -05:00
namespace mfm
{
void dup(const Opts::Dup &opts);
}
void
mfm::dup(const Opts::Dup &opts_)
{
2025-09-01 15:32:46 -05:00
std::vector<std::filesystem::path> srcpaths;
2025-09-01 14:09:25 -05:00
2025-09-01 14:15:22 -05:00
// TODO: expand if mergerfs mount
2025-09-01 15:32:49 -05:00
srcpaths = opts_.paths;
2025-09-01 14:09:25 -05:00
2025-09-01 15:33:01 -05:00
for(const auto &srcpath : srcpaths)
2025-09-01 01:51:33 -05:00
{
2025-09-01 15:41:45 -05:00
if(stdfs::is_regular_file(srcpath))
2025-09-01 14:10:27 -05:00
{
}
2025-09-01 15:41:45 -05:00
else if(stdfs::is_directory(srcpath))
2025-09-01 01:51:33 -05:00
{
2025-09-01 15:41:45 -05:00
auto dir_opts = (stdfs::directory_options::follow_directory_symlink |
stdfs::directory_options::skip_permission_denied);
2025-09-01 13:43:53 -05:00
2025-09-01 15:41:45 -05:00
for(const stdfs::directory_entry &de :
stdfs::recursive_directory_iterator(srcpath,dir_opts))
2025-09-01 13:45:37 -05:00
{
2025-09-01 14:10:27 -05:00
if(!str::startswith(de.path().filename().string(),".dup_"))
continue;
for(const auto &de :
2025-09-01 15:41:45 -05:00
stdfs::directory_iterator(de.path().parent_path()))
2025-09-01 14:10:27 -05:00
{
2025-09-01 18:39:53 -05:00
auto relpath = stdfs::relative(de.path(),srcpath);
2025-09-01 15:35:05 -05:00
2025-09-01 15:33:14 -05:00
for(const auto &dstpath : srcpaths)
2025-09-01 15:32:46 -05:00
{
2025-09-01 15:33:22 -05:00
if(dstpath == srcpath)
2025-09-01 15:32:46 -05:00
continue;
2025-09-01 16:46:36 -05:00
fmt::println("copy {} to {} : {} : {}",
2025-09-01 15:44:06 -05:00
srcpath.string(),
dstpath.string(),
2025-09-01 16:46:36 -05:00
relpath.parent_path().string(),
relpath.filename().string());
2025-09-01 16:25:46 -05:00
fs::clonepath(srcpath,
dstpath,
relpath.parent_path().string());
2025-09-01 15:32:46 -05:00
}
2025-09-01 14:10:27 -05:00
}
2025-09-01 13:45:37 -05:00
}
2025-09-01 01:51:33 -05:00
}
2025-09-01 14:10:27 -05:00
else
{
2025-09-01 14:14:39 -05:00
//throw std::runtime_error("invalid file type");
2025-09-01 15:34:13 -05:00
fmt::println("invalid file type: {}",srcpath.string());
2025-09-01 14:10:27 -05:00
}
2025-09-01 01:51:33 -05:00
}
}