Refactoring backups.
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
#include "server_env.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@@ -187,14 +187,14 @@ std::string get_local_service_env_path(const std::string &server_name, const std
|
||||
return (fs::path(service_path) / "template").string();
|
||||
}
|
||||
|
||||
std::string get_remote_service_backups_path(const std::string &server_name, const std::string &service_name)
|
||||
std::string get_remote_backups_path(const std::string &server_name)
|
||||
{
|
||||
if (server_name.empty() || service_name.empty())
|
||||
if (server_name.empty())
|
||||
return std::string();
|
||||
std::string service_path = get_remote_service_path(server_name, service_name);
|
||||
if (service_path.empty())
|
||||
std::string dropshell_path = get_remote_DROPSHELL_path(server_name);
|
||||
if (dropshell_path.empty())
|
||||
return std::string();
|
||||
return (fs::path(service_path) / "backups").string();
|
||||
return (fs::path(dropshell_path) / "backups").string();
|
||||
}
|
||||
|
||||
std::string get_remote_service_env_file(const std::string &server_name, const std::string &service_name)
|
||||
|
@@ -57,7 +57,7 @@ namespace dropshell {
|
||||
std::string get_remote_service_path(const std::string &server_name, const std::string &service_name);
|
||||
std::string get_remote_service_config_path(const std::string &server_name, const std::string &service_name);
|
||||
std::string get_remote_service_template_path(const std::string &server_name, const std::string &service_name);
|
||||
std::string get_remote_service_backups_path(const std::string &server_name, const std::string &service_name);
|
||||
std::string get_remote_backups_path(const std::string &server_name);
|
||||
|
||||
std::string get_remote_service_env_file(const std::string &server_name, const std::string &service_name);
|
||||
} // namespace dropshell
|
||||
|
@@ -157,4 +157,94 @@ void ensure_directories_exist(std::vector<std::string> directories)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
|
||||
void constructLps(const std::string &pat, std::vector<int> &lps) {
|
||||
|
||||
// len stores the length of longest prefix which
|
||||
// is also a suffix for the previous index
|
||||
int len = 0;
|
||||
|
||||
// lps[0] is always 0
|
||||
lps[0] = 0;
|
||||
|
||||
int i = 1;
|
||||
while (i < pat.length()) {
|
||||
|
||||
// If characters match, increment the size of lps
|
||||
if (pat[i] == pat[len]) {
|
||||
len++;
|
||||
lps[i] = len;
|
||||
i++;
|
||||
}
|
||||
|
||||
// If there is a mismatch
|
||||
else {
|
||||
if (len != 0) {
|
||||
|
||||
// Update len to the previous lps value
|
||||
// to avoid reduntant comparisons
|
||||
len = lps[len - 1];
|
||||
}
|
||||
else {
|
||||
|
||||
// If no matching prefix found, set lps[i] to 0
|
||||
lps[i] = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> search(const std::string &pat, const std::string &txt) {
|
||||
int n = txt.length();
|
||||
int m = pat.length();
|
||||
|
||||
std::vector<int> lps(m);
|
||||
std::vector<int> res;
|
||||
|
||||
constructLps(pat, lps);
|
||||
|
||||
// Pointers i and j, for traversing
|
||||
// the text and pattern
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
while (i < n) {
|
||||
|
||||
// If characters match, move both pointers forward
|
||||
if (txt[i] == pat[j]) {
|
||||
i++;
|
||||
j++;
|
||||
|
||||
// If the entire pattern is matched
|
||||
// store the start index in result
|
||||
if (j == m) {
|
||||
res.push_back(i - j);
|
||||
|
||||
// Use LPS of previous index to
|
||||
// skip unnecessary comparisons
|
||||
j = lps[j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// If there is a mismatch
|
||||
else {
|
||||
|
||||
// Use lps value of previous index
|
||||
// to avoid redundant comparisons
|
||||
if (j != 0)
|
||||
j = lps[j - 1];
|
||||
else
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int count_substring(const std::string &substring, const std::string &text) {
|
||||
std::vector<int> positions = search(substring, text);
|
||||
return positions.size();
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@@ -28,4 +28,8 @@ void recursive_copy(const std::string & source, const std::string & destination)
|
||||
|
||||
void ensure_directories_exist(std::vector<std::string> directories);
|
||||
|
||||
// KMP algorithm
|
||||
std::vector<int> search(const std::string &pat, const std::string &txt);
|
||||
int count_substring(const std::string &substring, const std::string &text);
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user