From 5f2bbce080e0dc5c0d5c65e0b31ae984f9f0d691 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 23 Sep 2021 19:54:32 +0200 Subject: [PATCH] Manager: Add file pulling function --- src/dotfile.cpp | 50 +++++++++++++++++++++++++++++++++++++++++-------- src/dotfile.h | 6 +++++- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 1d0a564..5196fb9 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -71,11 +71,11 @@ void Dotfile::add(const std::vector& targets) sync( targets, homePaths, systemPaths, - [](std::string* paths, std::string homePath, size_t homeSize) { + [](std::string* paths, const std::string& homePath, const std::string& homeDirectory) { paths[0] = homePath; - paths[1] = homePath.substr(homeSize + 1); + paths[1] = homePath.substr(homeDirectory.size() + 1); }, - [](std::string* paths, std::string systemPath) { + [](std::string* paths, const std::string& systemPath) { paths[0] = systemPath; paths[1] = systemPath.substr(1); }); @@ -98,11 +98,45 @@ void Dotfile::list(const std::vector& targets) }); } +void Dotfile::pull(const std::vector& targets) +{ + std::vector dotfiles; + std::vector homeFiles; + std::vector systemFiles; + + // Separate home and system targets + forEachDotfile(targets, [&](const std::filesystem::directory_entry& path, size_t index, size_t) { + dotfiles.push_back(path.path().string()); + if (isSystemTarget(path.path().string())) { + systemFiles.push_back(index); + } + else { + homeFiles.push_back(index); + } + }); + + size_t workingDirectory = s_workingDirectory.string().size(); + sync( + dotfiles, homeFiles, systemFiles, + [&](std::string* paths, const std::string& homeFile, const std::string& homeDirectory) { + // homeFile = /home//dotfiles/ + // copy: /home// -> /home//dotfiles/ + paths[0] = homeDirectory + homeFile.substr(workingDirectory); + paths[1] = homeFile; + }, + [&](std::string* paths, const std::string& systemFile) { + // systemFile = /home//dotfiles/ + // copy: -> /home//dotfiles/ + paths[0] = systemFile.substr(workingDirectory); + paths[1] = systemFile; + }); +} + // ----------------------------------------- void Dotfile::sync(const std::vector& files, const std::vector& homeIndices, const std::vector& systemIndices, - const std::function& generateHomePaths, - const std::function& generateSystemPaths) + const std::function& generateHomePaths, + const std::function& generateSystemPaths) { bool root = !geteuid() ? true : false; if (!systemIndices.empty() && !root) { @@ -160,10 +194,10 @@ void Dotfile::sync(const std::vector& files, const std::vector/ - std::string home = "/home/" + std::string(user->pw_name); + std::string homeDirectory = "/home/" + std::string(user->pw_name); for (size_t i : homeIndices) { std::string paths[2]; - generateHomePaths(paths, files.at(i), home.size()); + generateHomePaths(paths, files.at(i), homeDirectory); copy(paths[0], paths[1], true); } // / @@ -174,7 +208,7 @@ void Dotfile::sync(const std::vector& files, const std::vector& targets, const std::function& callback) +void Dotfile::forEachDotfile(const std::vector& targets, const std::function& callback) { size_t workingDirectory = s_workingDirectory.string().size(); diff --git a/src/dotfile.h b/src/dotfile.h index f722e2f..6fb79c9 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -33,13 +33,17 @@ public: void add(const std::vector& targets = {}); void list(const std::vector& targets = {}); + void pull(const std::vector& targets = {}); static void setWorkingDirectory(std::filesystem::path directory) { s_workingDirectory = directory; } static void setSystemDirectories(const std::vector& systemDirectories) { s_systemDirectories = systemDirectories; } static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } private: - void forEachDotfile(const std::vector& targets, const std::function& callback); + void sync(const std::vector& files, const std::vector& homeIndices, const std::vector& systemIndices, + const std::function& generateHomePaths, + const std::function& generateSystemPaths); + void forEachDotfile(const std::vector& targets, const std::function& callback); bool filter(const std::filesystem::path& path); bool include(const std::filesystem::path& path, const std::vector& targets); bool isSystemTarget(const std::string& target);