Commit ec1cb75e by songxinkai

add boost/filesystem/auto_scp.cpp

parent 6a2ec4d8
// g++ auto_scp.cpp -I /usr/include/filesystem -L /usr/lib/x86_64-linux-gnu/ -lboost_filesystem -lboost_system
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
using namespace std;
int get_filenames(const std::string dir, std::vector<std::string>& filenames, bool recursive){
fs::path path(dir);
if (!fs::exists(path)){
return -1;
}
fs::directory_iterator end_iter;
for (fs::directory_iterator iter(path); iter!=end_iter; ++iter){
if (fs::is_regular_file(iter->status())){
filenames.push_back(iter->path().string());
}
if (recursive && fs::is_directory(iter->status())){
get_filenames(iter->path().string(), filenames, recursive);
}
}
return filenames.size();
}
int main(){
string ip="10.2.5.146";
string username = "root";
string passwd = "novacation";
string remote_dir = "/root/tmp/";
string dir_name("data_dir");
string backup_dir("backup_dir");
vector<string> file_vec;
string s;
while(true) {
vector<string>().swap(file_vec);
get_filenames(dir_name, file_vec, false);
for (int i = 0; i < file_vec.size(); ++i){
if (file_vec[i].find(".finish") == string::npos){
continue;
}
string file_finish = file_vec[i];
string file_bin = file_vec[i].substr(0, file_finish.find(".finish"));
s = "sshpass -p \"" + passwd + "\" scp -o StrictHostKeyChecking=no " + file_bin + " " + username + "@" + ip + ":" + remote_dir;
if (system(s.c_str())){
cout << "scp " << file_bin << " fail." << endl;
continue;
}
s = "sshpass -p \"" + passwd + "\" scp -o StrictHostKeyChecking=no " + file_finish + " " + username + "@" + ip + ":" + remote_dir;
system(s.c_str());
s = "mv " + file_bin + " " + backup_dir;
system(s.c_str());
s = "mv " + file_finish + " " + backup_dir;
system(s.c_str());
}
}
return 0;
}
c
\ No newline at end of file
/root/.cache/bazel/_bazel_root/10b47e0969b5a5941f6ac84b9ec5df3b/execroot/__main__/bazel-out/k8-fastbuild/bin
\ No newline at end of file
/root/.cache/bazel/_bazel_root/10b47e0969b5a5941f6ac84b9ec5df3b/execroot/__main__
\ No newline at end of file
/root/.cache/bazel/_bazel_root/10b47e0969b5a5941f6ac84b9ec5df3b/execroot/__main__/bazel-out/k8-fastbuild/genfiles
\ No newline at end of file
/root/.cache/bazel/_bazel_root/10b47e0969b5a5941f6ac84b9ec5df3b/execroot/__main__/bazel-out
\ No newline at end of file
/root/.cache/bazel/_bazel_root/10b47e0969b5a5941f6ac84b9ec5df3b/execroot/__main__/bazel-out/k8-fastbuild/testlogs
\ No newline at end of file
a
\ No newline at end of file
...@@ -33,8 +33,7 @@ int main (){ ...@@ -33,8 +33,7 @@ int main (){
return 0; return 0;
} }
int get_filenames(const std::string& dir, std::vector<std::string>& filenames) int get_filenames(const std::string& dir, std::vector<std::string>& filenames, bool recursive){
{
fs::path path(dir); fs::path path(dir);
if (!fs::exists(path)){ if (!fs::exists(path)){
return -1; return -1;
...@@ -44,9 +43,8 @@ int get_filenames(const std::string& dir, std::vector<std::string>& filenames) ...@@ -44,9 +43,8 @@ int get_filenames(const std::string& dir, std::vector<std::string>& filenames)
if (fs::is_regular_file(iter->status())){ if (fs::is_regular_file(iter->status())){
filenames.push_back(iter->path().string()); filenames.push_back(iter->path().string());
} }
if (recursive && fs::is_directory(iter->status())){
if (fs::is_directory(iter->status())){ get_filenames(iter->path().string(), filenames, recursive);
get_filenames(iter->path().string(), filenames);
} }
} }
return filenames.size(); return filenames.size();
......
...@@ -8,8 +8,10 @@ using namespace std; ...@@ -8,8 +8,10 @@ using namespace std;
int main(){ int main(){
stringstream ss; stringstream ss;
string s = "c."; string s = "c.";
system(("touch " + s + "out").c_str()); int a = system(("touch " + s + "out").c_str());
ss << "mv " << "c.out " << "b.out"; cout << a << endl;
system(ss.str().c_str()); ss << "mv " << "d.out " << "b.out";
a = system(ss.str().c_str());
cout << a << endl;
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment